aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/todos/store.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/todos/store.js')
-rw-r--r--src/features/todos/store.js86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/features/todos/store.js b/src/features/todos/store.js
new file mode 100644
index 000000000..e7e13b37f
--- /dev/null
+++ b/src/features/todos/store.js
@@ -0,0 +1,86 @@
1import {
2 computed,
3 action,
4 observable,
5} from 'mobx';
6import localStorage from 'mobx-localstorage';
7
8import { todoActions } from './actions';
9import { FeatureStore } from '../utils/FeatureStore';
10import { createReactions } from '../../stores/lib/Reaction';
11import { createActionBindings } from '../utils/ActionBinding';
12import { DEFAULT_TODOS_WIDTH, TODOS_MIN_WIDTH } from '.';
13
14const debug = require('debug')('Franz:feature:todos:store');
15
16export default class TodoStore extends FeatureStore {
17 @observable isFeatureEnabled = false;
18
19 @observable isFeatureActive = false;
20
21 @computed get width() {
22 const width = this.settings.width || DEFAULT_TODOS_WIDTH;
23
24 return width < TODOS_MIN_WIDTH ? TODOS_MIN_WIDTH : width;
25 }
26
27 @computed get settings() {
28 return localStorage.getItem('todos') || {};
29 }
30
31 // ========== PUBLIC API ========= //
32
33 @action start(stores, actions) {
34 debug('TodoStore::start');
35 this.stores = stores;
36 this.actions = actions;
37
38 // ACTIONS
39
40 this._registerActions(createActionBindings([
41 [todoActions.resize, this._resize],
42 ]));
43
44 // REACTIONS
45
46 this._allReactions = createReactions([
47 this._setFeatureEnabledReaction,
48 ]);
49
50 this._registerReactions(this._allReactions);
51
52 this.isFeatureActive = true;
53 }
54
55 @action stop() {
56 super.stop();
57 debug('TodoStore::stop');
58 this.reset();
59 this.isFeatureActive = false;
60 }
61
62 // ========== PRIVATE METHODS ========= //
63
64 _updateSettings = (changes) => {
65 localStorage.setItem('todos', {
66 ...this.settings,
67 ...changes,
68 });
69 };
70
71 // Actions
72
73 @action _resize = ({ width }) => {
74 this._updateSettings({
75 width,
76 });
77 };
78
79 // Reactions
80
81 _setFeatureEnabledReaction = () => {
82 const { isTodosEnabled } = this.stores.features.features;
83
84 this.isFeatureEnabled = isTodosEnabled;
85 };
86}