aboutsummaryrefslogtreecommitdiffstats
path: root/src/features/announcements/store.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/features/announcements/store.js')
-rw-r--r--src/features/announcements/store.js148
1 files changed, 0 insertions, 148 deletions
diff --git a/src/features/announcements/store.js b/src/features/announcements/store.js
deleted file mode 100644
index 794d20142..000000000
--- a/src/features/announcements/store.js
+++ /dev/null
@@ -1,148 +0,0 @@
1import {
2 action,
3 computed,
4 observable,
5} from 'mobx';
6import semver from 'semver';
7import localStorage from 'mobx-localstorage';
8
9import { FeatureStore } from '../utils/FeatureStore';
10import { ANNOUNCEMENTS_ROUTES } from './constants';
11import { getAnnouncementRequest, getChangelogRequest, getCurrentVersionRequest } from './api';
12import { announcementActions } from './actions';
13import { createActionBindings } from '../utils/ActionBinding';
14import { createReactions } from '../../stores/lib/Reaction';
15import { matchRoute } from '../../helpers/routing-helpers';
16import { DEFAULT_APP_SETTINGS } from '../../environment';
17
18const LOCAL_STORAGE_KEY = 'announcements';
19
20const debug = require('debug')('Ferdi:feature:announcements:store');
21
22export class AnnouncementsStore extends FeatureStore {
23 @observable targetVersion = null;
24
25 @observable isFeatureActive = false;
26
27 @computed get changelog() {
28 return getChangelogRequest.result;
29 }
30
31 @computed get announcement() {
32 if (!this.stores || !getAnnouncementRequest.result) return null;
33 const { locale } = this.stores.app;
34 const announcement = getAnnouncementRequest.result;
35 // User locale
36 if (announcement[locale]) return announcement[locale];
37 // Default locale
38 if (announcement[DEFAULT_APP_SETTINGS.fallbackLocale]) return announcement[DEFAULT_APP_SETTINGS.fallbackLocale];
39 // No locales specified
40 return announcement;
41 }
42
43 @computed get areNewsAvailable() {
44 const isChangelogAvailable = getChangelogRequest.wasExecuted && !!this.changelog;
45 const isAnnouncementAvailable = getAnnouncementRequest.wasExecuted && !!this.announcement;
46 return isChangelogAvailable || isAnnouncementAvailable;
47 }
48
49 @computed get settings() {
50 return localStorage.getItem(LOCAL_STORAGE_KEY) || {};
51 }
52
53 @computed get lastSeenAnnouncementVersion() {
54 return this.settings.lastSeenAnnouncementVersion || null;
55 }
56
57 @computed get currentVersion() {
58 return getCurrentVersionRequest.result;
59 }
60
61 @computed get isNewUser() {
62 return this.stores.settings.stats.appStarts <= 1;
63 }
64
65 @computed get isAnnouncementShown() {
66 const { router } = this.stores;
67 return router.location.pathname.includes('/announcements');
68 }
69
70 async start(stores, actions) {
71 debug('AnnouncementsStore::start');
72 this.stores = stores;
73 this.actions = actions;
74 getCurrentVersionRequest.execute();
75
76 this._registerActions(createActionBindings([
77 [announcementActions.show, this._showAnnouncement],
78 ]));
79
80 this._reactions = createReactions([
81 this._showAnnouncementOnRouteMatch,
82 this._showAnnouncementToUsersWhoUpdatedApp,
83 this._fetchAnnouncements,
84 ]);
85 this._registerReactions(this._reactions);
86 this.isFeatureActive = true;
87 }
88
89 stop() {
90 super.stop();
91 debug('AnnouncementsStore::stop');
92 this.isFeatureActive = false;
93 }
94
95 // ======= HELPERS ======= //
96
97 _updateSettings = (changes) => {
98 localStorage.setItem(LOCAL_STORAGE_KEY, {
99 ...this.settings,
100 ...changes,
101 });
102 };
103
104 // ======= ACTIONS ======= //
105
106 @action _showAnnouncement = ({ targetVersion } = {}) => {
107 const { router } = this.stores;
108 this.targetVersion = targetVersion || this.currentVersion;
109 this._updateSettings({
110 lastSeenAnnouncementVersion: this.currentVersion,
111 });
112 const targetRoute = `/announcements/${this.targetVersion}`;
113 if (router.location.pathname !== targetRoute) {
114 this.stores.router.push(targetRoute);
115 }
116 };
117
118 // ======= REACTIONS ========
119
120 _showAnnouncementToUsersWhoUpdatedApp = () => {
121 const { announcement, isNewUser } = this;
122 // Check if there is an announcement and don't show announcements to new users
123 if (!announcement || isNewUser) return;
124
125 // Check if the user has already used current version (= has seen the announcement)
126 const { currentVersion, lastSeenAnnouncementVersion } = this;
127 if (semver.gt(currentVersion, lastSeenAnnouncementVersion || '0.0.0')) {
128 debug(`${currentVersion} > ${lastSeenAnnouncementVersion}: announcement is shown`);
129 this._showAnnouncement();
130 }
131 };
132
133 _fetchAnnouncements = () => {
134 const targetVersion = this.targetVersion || this.currentVersion;
135 if (!targetVersion) return;
136 getChangelogRequest.reset().execute(targetVersion);
137 getAnnouncementRequest.reset().execute(targetVersion);
138 };
139
140 _showAnnouncementOnRouteMatch = () => {
141 const { router } = this.stores;
142 const match = matchRoute(ANNOUNCEMENTS_ROUTES.TARGET, router.location.pathname);
143 if (match) {
144 const targetVersion = match.id;
145 this._showAnnouncement({ targetVersion });
146 }
147 }
148}