aboutsummaryrefslogtreecommitdiffstats
path: root/src/stores/NewsStore.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/stores/NewsStore.js')
-rw-r--r--src/stores/NewsStore.js42
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 @@
1import { computed, observable } from 'mobx';
2import { remove } from 'lodash';
3
4import Store from './lib/Store';
5import CachedRequest from './lib/CachedRequest';
6import Request from './lib/Request';
7import { CHECK_INTERVAL } from '../config';
8
9export 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}