aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/settings/navigation/SettingsNavigation.js
blob: f7786f4e83bc22676ea6937bc7f610ee8460b2eb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, intlShape } from 'react-intl';
import { inject, observer } from 'mobx-react';
import { ProBadge } from '@meetfranz/ui';
import { RouterStore } from 'mobx-react-router';

import { LOCAL_SERVER, LIVE_API } from '../../../config';
import Link from '../../ui/Link';
import { workspaceStore } from '../../../features/workspaces';
import UIStore from '../../../stores/UIStore';
import SettingsStore from '../../../stores/SettingsStore';
import UserStore from '../../../stores/UserStore';
import { serviceLimitStore } from '../../../features/serviceLimit';

const messages = defineMessages({
  availableServices: {
    id: 'settings.navigation.availableServices',
    defaultMessage: '!!!Available services',
  },
  yourServices: {
    id: 'settings.navigation.yourServices',
    defaultMessage: '!!!Your services',
  },
  yourWorkspaces: {
    id: 'settings.navigation.yourWorkspaces',
    defaultMessage: '!!!Your workspaces',
  },
  account: {
    id: 'settings.navigation.account',
    defaultMessage: '!!!Account',
  },
  team: {
    id: 'settings.navigation.team',
    defaultMessage: '!!!Manage Team',
  },
  settings: {
    id: 'settings.navigation.settings',
    defaultMessage: '!!!Settings',
  },
  supportFerdi: {
    id: 'settings.navigation.supportFerdi',
    defaultMessage: '!!!About Ferdi',
  },
  logout: {
    id: 'settings.navigation.logout',
    defaultMessage: '!!!Logout',
  },
});

export default @inject('stores', 'actions') @observer class SettingsNavigation extends Component {
  static propTypes = {
    stores: PropTypes.shape({
      ui: PropTypes.instanceOf(UIStore).isRequired,
      user: PropTypes.instanceOf(UserStore).isRequired,
      settings: PropTypes.instanceOf(SettingsStore).isRequired,
      router: PropTypes.instanceOf(RouterStore).isRequired,
    }).isRequired,
    actions: PropTypes.shape({
      settings: PropTypes.shape({
        update: PropTypes.func.isRequired,
      }).isRequired,
    }).isRequired,
    serviceCount: PropTypes.number.isRequired,
    workspaceCount: PropTypes.number.isRequired,
  };

  static contextTypes = {
    intl: intlShape,
  };

  handleLoginLogout() {
    const isLoggedIn = Boolean(localStorage.getItem('authToken'));
    const isUsingWithoutAccount = this.props.stores.settings.app.server === LOCAL_SERVER;

    if (isLoggedIn) {
      // Remove current auth token
      localStorage.removeItem('authToken');

      if (isUsingWithoutAccount) {
        // Reset server back to Ferdi API
        this.props.actions.settings.update({
          type: 'app',
          data: {
            server: LIVE_API,
          },
        });
      }
      this.props.stores.user.isLoggingOut = true;
    }

    this.props.stores.router.push(isLoggedIn ? '/auth/logout' : '/auth/welcome');

    if (isLoggedIn) {
      // Reload Ferdi, otherwise many settings won't sync correctly with the server
      // after logging into another account
      window.location.reload();
    }
  }

  render() {
    const { serviceCount, workspaceCount, stores } = this.props;
    const { isDarkThemeActive } = stores.ui;
    const { router, user } = stores;
    const { intl } = this.context;
    const isLoggedIn = Boolean(localStorage.getItem('authToken'));
    const isUsingWithoutAccount = stores.settings.app.server === LOCAL_SERVER;
    const isUsingFranzServer = stores.settings.app.server === 'https://api.franzinfra.com';

    return (
      <div className="settings-navigation">
        <Link
          to="/settings/recipes"
          className="settings-navigation__link"
          activeClassName="is-active"
        >
          {intl.formatMessage(messages.availableServices)}
        </Link>
        <Link
          to="/settings/services"
          className="settings-navigation__link"
          activeClassName="is-active"
          disabled={!isLoggedIn}
        >
          {intl.formatMessage(messages.yourServices)}
          {' '}
          <span className="badge">
            {serviceCount}
            {serviceLimitStore.serviceLimit !== 0 && (
              `/${serviceLimitStore.serviceLimit}`
            )}
          </span>
        </Link>
        {workspaceStore.isFeatureEnabled ? (
          <Link
            to="/settings/workspaces"
            className="settings-navigation__link"
            activeClassName="is-active"
            disabled={!isLoggedIn}
          >
            {intl.formatMessage(messages.yourWorkspaces)}
            {' '}
            {workspaceStore.isPremiumUpgradeRequired ? (
              <ProBadge inverted={!isDarkThemeActive && workspaceStore.isSettingsRouteActive} />
            ) : (
              <span className="badge">{workspaceCount}</span>
            )}
          </Link>
        ) : null}
        {!isUsingWithoutAccount && (
          <Link
            to="/settings/user"
            className="settings-navigation__link"
            activeClassName="is-active"
            disabled={!isLoggedIn}
          >
            {intl.formatMessage(messages.account)}
          </Link>
        )}
        {isUsingFranzServer && (
          <Link
            to="/settings/team"
            className="settings-navigation__link"
            activeClassName="is-active"
            disabled={!isLoggedIn}
          >
            {intl.formatMessage(messages.team)}
            {!user.data.isPremium && (
              <ProBadge inverted={!isDarkThemeActive && router.location.pathname === '/settings/team'} />
            )}
          </Link>
        )}
        <Link
          to="/settings/app"
          className="settings-navigation__link"
          activeClassName="is-active"
        >
          {intl.formatMessage(messages.settings)}
        </Link>
        <Link
          to="/settings/support"
          className="settings-navigation__link"
          activeClassName="is-active"
        >
          {intl.formatMessage(messages.supportFerdi)}
        </Link>
        <span className="settings-navigation__expander" />
        <button
          type="button"
          to={isLoggedIn ? '/auth/logout' : '/auth/welcome'}
          className="settings-navigation__link"
          onClick={this.handleLoginLogout.bind(this)}
        >
          { isLoggedIn && !isUsingWithoutAccount ? intl.formatMessage(messages.logout) : 'Login'}
        </button>
      </div>
    );
  }
}