aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/FeaturesStore.js
blob: 0dff2fda289909957310eda9a7c55d72ee49ea8c (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
import {
  computed,
  observable,
  runInAction,
} from 'mobx';

import Store from './lib/Store';
import CachedRequest from './lib/CachedRequest';

import serviceProxy from '../features/serviceProxy';
import basicAuth from '../features/basicAuth';
import workspaces from '../features/workspaces';
import quickSwitch from '../features/quickSwitch';
import nightlyBuilds from '../features/nightlyBuilds';
import publishDebugInfo from '../features/publishDebugInfo';
import announcements from '../features/announcements';
import settingsWS from '../features/settingsWS';
import communityRecipes from '../features/communityRecipes';
import todos from '../features/todos';
import appearance from '../features/appearance';

import { DEFAULT_FEATURES_CONFIG } from '../config';

export default class FeaturesStore extends Store {
  @observable defaultFeaturesRequest = new CachedRequest(this.api.features, 'default');

  @observable featuresRequest = new CachedRequest(this.api.features, 'features');

  @observable features = ({ ...DEFAULT_FEATURES_CONFIG });

  async setup() {
    this.registerReactions([
      this._updateFeatures,
      this._monitorLoginStatus.bind(this),
    ]);

    await this.featuresRequest._promise;
    setTimeout(this._setupFeatures.bind(this), 1);
  }

  @computed get anonymousFeatures() {
    return this.defaultFeaturesRequest.execute().result || DEFAULT_FEATURES_CONFIG;
  }

  _updateFeatures = () => {
    const features = { ...DEFAULT_FEATURES_CONFIG };
    if (this.stores.user.isLoggedIn) {
      let requestResult = {};
      try {
        requestResult = this.featuresRequest.execute().result;
      } catch (e) {} // eslint-disable-line no-empty
      Object.assign(features, requestResult);
    }
    runInAction('FeaturesStore::_updateFeatures', () => {
      this.features = features;
    });
  };

  _monitorLoginStatus() {
    if (this.stores.user.isLoggedIn) {
      this.featuresRequest.invalidate({ immediately: true });
    } else {
      this.defaultFeaturesRequest.execute();
      this.defaultFeaturesRequest.invalidate({ immediately: true });
    }
  }

  _setupFeatures() {
    serviceProxy(this.stores, this.actions);
    basicAuth(this.stores, this.actions);
    workspaces(this.stores, this.actions);
    quickSwitch(this.stores, this.actions);
    nightlyBuilds(this.stores, this.actions);
    publishDebugInfo(this.stores, this.actions);
    announcements(this.stores, this.actions);
    settingsWS(this.stores, this.actions);
    communityRecipes(this.stores, this.actions);
    todos(this.stores, this.actions);
    appearance(this.stores, this.actions);
  }
}