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