diff options
author | Stefan Malzner <stefan@adlk.io> | 2019-07-30 11:41:54 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2019-07-30 11:41:54 +0200 |
commit | d7ed456a7b6f73e046ba3a2ef38eb21f82f06ca4 (patch) | |
tree | f24f4f39ae38bdfdd581f451fc3e2e79f5670df5 /src/features/todos/store.js | |
parent | Fix position of todos app (diff) | |
download | ferdium-app-d7ed456a7b6f73e046ba3a2ef38eb21f82f06ca4.tar.gz ferdium-app-d7ed456a7b6f73e046ba3a2ef38eb21f82f06ca4.tar.zst ferdium-app-d7ed456a7b6f73e046ba3a2ef38eb21f82f06ca4.zip |
Make todo layer resizable
Diffstat (limited to 'src/features/todos/store.js')
-rw-r--r-- | src/features/todos/store.js | 86 |
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 @@ | |||
1 | import { | ||
2 | computed, | ||
3 | action, | ||
4 | observable, | ||
5 | } from 'mobx'; | ||
6 | import localStorage from 'mobx-localstorage'; | ||
7 | |||
8 | import { todoActions } from './actions'; | ||
9 | import { FeatureStore } from '../utils/FeatureStore'; | ||
10 | import { createReactions } from '../../stores/lib/Reaction'; | ||
11 | import { createActionBindings } from '../utils/ActionBinding'; | ||
12 | import { DEFAULT_TODOS_WIDTH, TODOS_MIN_WIDTH } from '.'; | ||
13 | |||
14 | const debug = require('debug')('Franz:feature:todos:store'); | ||
15 | |||
16 | export 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 | } | ||