diff options
author | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
---|---|---|
committer | Stefan Malzner <stefan@adlk.io> | 2017-10-13 12:29:40 +0200 |
commit | 58cda9cc7fb79ca9df6746de7f9662bc08dc156a (patch) | |
tree | 1211600c2a5d3b5f81c435c6896618111a611720 /src/stores/NewsStore.js | |
download | ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.gz ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.tar.zst ferdium-app-58cda9cc7fb79ca9df6746de7f9662bc08dc156a.zip |
initial commit
Diffstat (limited to 'src/stores/NewsStore.js')
-rw-r--r-- | src/stores/NewsStore.js | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/stores/NewsStore.js b/src/stores/NewsStore.js new file mode 100644 index 000000000..e5091834f --- /dev/null +++ b/src/stores/NewsStore.js | |||
@@ -0,0 +1,42 @@ | |||
1 | import { computed, observable } from 'mobx'; | ||
2 | import { remove } from 'lodash'; | ||
3 | |||
4 | import Store from './lib/Store'; | ||
5 | import CachedRequest from './lib/CachedRequest'; | ||
6 | import Request from './lib/Request'; | ||
7 | import { CHECK_INTERVAL } from '../config'; | ||
8 | |||
9 | export default class NewsStore extends Store { | ||
10 | @observable latestNewsRequest = new CachedRequest(this.api.news, 'latest'); | ||
11 | @observable hideNewsRequest = new Request(this.api.news, 'hide'); | ||
12 | |||
13 | constructor(...args) { | ||
14 | super(...args); | ||
15 | |||
16 | // Register action handlers | ||
17 | this.actions.news.hide.listen(this._hide.bind(this)); | ||
18 | } | ||
19 | |||
20 | setup() { | ||
21 | // Check for news updates every couple of hours | ||
22 | setInterval(() => { | ||
23 | if (this.latestNewsRequest.wasExecuted && this.stores.user.isLoggedIn) { | ||
24 | this.latestNewsRequest.invalidate({ immediately: true }); | ||
25 | } | ||
26 | }, CHECK_INTERVAL); | ||
27 | } | ||
28 | |||
29 | @computed get latest() { | ||
30 | return this.latestNewsRequest.execute().result || []; | ||
31 | } | ||
32 | |||
33 | // Actions | ||
34 | _hide({ newsId }) { | ||
35 | this.hideNewsRequest.execute(newsId); | ||
36 | |||
37 | this.latestNewsRequest.invalidate().patch((result) => { | ||
38 | // TODO: check if we can use mobx.array remove | ||
39 | remove(result, n => n.id === newsId); | ||
40 | }); | ||
41 | } | ||
42 | } | ||