aboutsummaryrefslogtreecommitdiffstats
path: root/docs/example-feature/store.js
blob: 9fc86de36bf0ddda3364ebb2e5dda4eb0f2e329c (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
import { action, observable, reaction } from 'mobx';
import Store from '../../src/stores/lib/Store';
import Request from '../../src/stores/lib/Request';

const debug = require('debug')('Ferdi:feature:EXAMPLE_FEATURE:store');

export class ExampleFeatureStore extends Store {
  @observable getNameRequest = new Request(this.api, 'getName');

  constructor(stores, api, actions, state) {
    super(stores, api, actions);
    this.state = state;
  }

  setup() {
    debug('fetching name from api');
    this.getNameRequest.execute();

    // Update the name on the state when the request resolved
    reaction(
      () => (
        this.getNameRequest.result
      ),
      (name) => {
        this._setName(name);
      },
    );
  }

  @action _setName = (name) => {
    debug('setting name', name);
    this.state.name = name;
  };
}

export default ExampleFeatureStore;