diff options
Diffstat (limited to 'src/stores/NewsStore.js')
-rw-r--r-- | src/stores/NewsStore.js | 55 |
1 files changed, 0 insertions, 55 deletions
diff --git a/src/stores/NewsStore.js b/src/stores/NewsStore.js deleted file mode 100644 index 66a17cb29..000000000 --- a/src/stores/NewsStore.js +++ /dev/null | |||
@@ -1,55 +0,0 @@ | |||
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 | |||
12 | @observable hideNewsRequest = new Request(this.api.news, 'hide'); | ||
13 | |||
14 | constructor(...args) { | ||
15 | super(...args); | ||
16 | |||
17 | // Register action handlers | ||
18 | this.actions.news.hide.listen(this._hide.bind(this)); | ||
19 | this.actions.user.logout.listen(this._resetNewsRequest.bind(this)); | ||
20 | } | ||
21 | |||
22 | setup() { | ||
23 | // Check for news updates every couple of hours | ||
24 | setInterval(() => { | ||
25 | if (this.latestNewsRequest.wasExecuted && this.stores.user.isLoggedIn) { | ||
26 | this.latestNewsRequest.invalidate({ immediately: true }); | ||
27 | } | ||
28 | }, CHECK_INTERVAL); | ||
29 | } | ||
30 | |||
31 | @computed get latest() { | ||
32 | return this.latestNewsRequest.execute().result || []; | ||
33 | } | ||
34 | |||
35 | // Actions | ||
36 | _hide({ newsId }) { | ||
37 | this.hideNewsRequest.execute(newsId); | ||
38 | |||
39 | this.latestNewsRequest.invalidate().patch((result) => { | ||
40 | // TODO: check if we can use mobx.array remove | ||
41 | remove(result, (n) => n.id === newsId); | ||
42 | }); | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Reset the news request when current user logs out so that when another user | ||
47 | * logs in again without an app restart, the request will be fetched again and | ||
48 | * the news will be shown to the user. | ||
49 | * | ||
50 | * @private | ||
51 | */ | ||
52 | _resetNewsRequest() { | ||
53 | this.latestNewsRequest.reset(); | ||
54 | } | ||
55 | } | ||