diff options
author | 2019-03-12 21:36:10 +0100 | |
---|---|---|
committer | 2019-03-12 21:37:33 +0100 | |
commit | 6fb07bcb716af76ec2e96345f37624d12d0d1af0 (patch) | |
tree | 276191a782dc1d44f78331e548e43ff71758baca /src/features/announcements/store.js | |
parent | refactor server api even more (diff) | |
download | ferdium-app-6fb07bcb716af76ec2e96345f37624d12d0d1af0.tar.gz ferdium-app-6fb07bcb716af76ec2e96345f37624d12d0d1af0.tar.zst ferdium-app-6fb07bcb716af76ec2e96345f37624d12d0d1af0.zip |
implements basic release announcement feature
Diffstat (limited to 'src/features/announcements/store.js')
-rw-r--r-- | src/features/announcements/store.js | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/src/features/announcements/store.js b/src/features/announcements/store.js new file mode 100644 index 000000000..004a44062 --- /dev/null +++ b/src/features/announcements/store.js | |||
@@ -0,0 +1,95 @@ | |||
1 | import { action, observable, reaction } from 'mobx'; | ||
2 | import semver from 'semver'; | ||
3 | |||
4 | import Request from '../../stores/lib/Request'; | ||
5 | import Store from '../../stores/lib/Store'; | ||
6 | |||
7 | const debug = require('debug')('Franz:feature:announcements:store'); | ||
8 | |||
9 | export class AnnouncementsStore extends Store { | ||
10 | @observable getCurrentVersion = new Request(this.api, 'getCurrentVersion'); | ||
11 | |||
12 | @observable getAnnouncement = new Request(this.api, 'getAnnouncementForVersion'); | ||
13 | |||
14 | constructor(stores, api, actions, state) { | ||
15 | super(stores, api, actions); | ||
16 | this.state = state; | ||
17 | } | ||
18 | |||
19 | async setup() { | ||
20 | await this.fetchLastUsedVersion(); | ||
21 | await this.fetchCurrentVersion(); | ||
22 | await this.fetchReleaseAnnouncement(); | ||
23 | this.showAnnouncementIfNotSeenYet(); | ||
24 | |||
25 | this.actions.announcements.show.listen(this._showAnnouncement.bind(this)); | ||
26 | } | ||
27 | |||
28 | // ====== PUBLIC ====== | ||
29 | |||
30 | async fetchLastUsedVersion() { | ||
31 | debug('getting last used version from local storage'); | ||
32 | const lastUsedVersion = window.localStorage.getItem('lastUsedVersion'); | ||
33 | this._setLastUsedVersion(lastUsedVersion == null ? '0.0.0' : lastUsedVersion); | ||
34 | } | ||
35 | |||
36 | async fetchCurrentVersion() { | ||
37 | debug('getting current version from api'); | ||
38 | const version = await this.getCurrentVersion.execute(); | ||
39 | this._setCurrentVersion(version); | ||
40 | } | ||
41 | |||
42 | async fetchReleaseAnnouncement() { | ||
43 | debug('getting release announcement from api'); | ||
44 | try { | ||
45 | const announcement = await this.getAnnouncement.execute(this.state.currentVersion); | ||
46 | this._setAnnouncement(announcement); | ||
47 | } catch (error) { | ||
48 | this._setAnnouncement(null); | ||
49 | } | ||
50 | } | ||
51 | |||
52 | showAnnouncementIfNotSeenYet() { | ||
53 | const { announcement, currentVersion, lastUsedVersion } = this.state; | ||
54 | if (announcement && semver.gt(currentVersion, lastUsedVersion)) { | ||
55 | debug(`${currentVersion} < ${lastUsedVersion}: announcement is shown`); | ||
56 | this._showAnnouncement(); | ||
57 | } else { | ||
58 | debug(`${currentVersion} >= ${lastUsedVersion}: announcement is hidden`); | ||
59 | this._hideAnnouncement(); | ||
60 | } | ||
61 | } | ||
62 | |||
63 | // ====== PRIVATE ====== | ||
64 | |||
65 | @action _setCurrentVersion(version) { | ||
66 | debug(`setting current version to ${version}`); | ||
67 | this.state.currentVersion = version; | ||
68 | } | ||
69 | |||
70 | @action _setLastUsedVersion(version) { | ||
71 | debug(`setting last used version to ${version}`); | ||
72 | this.state.lastUsedVersion = version; | ||
73 | } | ||
74 | |||
75 | @action _setAnnouncement(announcement) { | ||
76 | debug(`setting announcement to ${announcement}`); | ||
77 | this.state.announcement = announcement; | ||
78 | } | ||
79 | |||
80 | @action _showAnnouncement() { | ||
81 | this.state.isAnnouncementVisible = true; | ||
82 | this.actions.service.blurActive(); | ||
83 | const dispose = reaction( | ||
84 | () => this.stores.services.active, | ||
85 | () => { | ||
86 | this._hideAnnouncement(); | ||
87 | dispose(); | ||
88 | }, | ||
89 | ); | ||
90 | } | ||
91 | |||
92 | @action _hideAnnouncement() { | ||
93 | this.state.isAnnouncementVisible = false; | ||
94 | } | ||
95 | } | ||