diff options
Diffstat (limited to 'src/stores/SettingsStore.js')
-rw-r--r-- | src/stores/SettingsStore.js | 26 |
1 files changed, 10 insertions, 16 deletions
diff --git a/src/stores/SettingsStore.js b/src/stores/SettingsStore.js index da99a720f..b7d803398 100644 --- a/src/stores/SettingsStore.js +++ b/src/stores/SettingsStore.js | |||
@@ -1,17 +1,12 @@ | |||
1 | import { ipcRenderer } from 'electron'; | 1 | import { ipcRenderer } from 'electron'; |
2 | import { action, computed, observable, extendObservable } from 'mobx'; | 2 | import { action, computed } from 'mobx'; |
3 | import localStorage from 'mobx-localstorage'; | ||
3 | 4 | ||
4 | import Store from './lib/Store'; | 5 | import Store from './lib/Store'; |
5 | import Request from './lib/Request'; | ||
6 | import CachedRequest from './lib/CachedRequest'; | ||
7 | import { gaEvent } from '../lib/analytics'; | 6 | import { gaEvent } from '../lib/analytics'; |
8 | import SettingsModel from '../models/Settings'; | 7 | import SettingsModel from '../models/Settings'; |
9 | 8 | ||
10 | export default class SettingsStore extends Store { | 9 | export default class SettingsStore extends Store { |
11 | @observable allSettingsRequest = new CachedRequest(this.api.local, 'getSettings'); | ||
12 | @observable updateSettingsRequest = new Request(this.api.local, 'updateSettings'); | ||
13 | @observable removeSettingsKeyRequest = new Request(this.api.local, 'removeKey'); | ||
14 | |||
15 | constructor(...args) { | 10 | constructor(...args) { |
16 | super(...args); | 11 | super(...args); |
17 | 12 | ||
@@ -21,20 +16,16 @@ export default class SettingsStore extends Store { | |||
21 | } | 16 | } |
22 | 17 | ||
23 | setup() { | 18 | setup() { |
24 | this.allSettingsRequest.execute(); | ||
25 | this._shareSettingsWithMainProcess(); | 19 | this._shareSettingsWithMainProcess(); |
26 | } | 20 | } |
27 | 21 | ||
28 | @computed get all() { | 22 | @computed get all() { |
29 | return new SettingsModel(this.allSettingsRequest.result); | 23 | return new SettingsModel(localStorage.getItem('app') || {}); |
30 | } | 24 | } |
31 | 25 | ||
32 | @action async _update({ settings }) { | 26 | @action async _update({ settings }) { |
33 | await this.updateSettingsRequest.execute(settings)._promise; | 27 | const appSettings = this.all; |
34 | await this.allSettingsRequest.patch((result) => { | 28 | localStorage.setItem('app', Object.assign(appSettings, settings)); |
35 | if (!result) return; | ||
36 | extendObservable(result, settings); | ||
37 | }); | ||
38 | 29 | ||
39 | // We need a little hack to wait until everything is patched | 30 | // We need a little hack to wait until everything is patched |
40 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); | 31 | setTimeout(() => this._shareSettingsWithMainProcess(), 0); |
@@ -43,8 +34,11 @@ export default class SettingsStore extends Store { | |||
43 | } | 34 | } |
44 | 35 | ||
45 | @action async _remove({ key }) { | 36 | @action async _remove({ key }) { |
46 | await this.removeSettingsKeyRequest.execute(key); | 37 | const appSettings = this.all; |
47 | await this.allSettingsRequest.invalidate({ immediately: true }); | 38 | if (Object.hasOwnProperty.call(appSettings, key)) { |
39 | delete appSettings[key]; | ||
40 | localStorage.setItem('app', appSettings); | ||
41 | } | ||
48 | 42 | ||
49 | this._shareSettingsWithMainProcess(); | 43 | this._shareSettingsWithMainProcess(); |
50 | } | 44 | } |