diff options
author | vantezzen <properly@protonmail.com> | 2019-09-07 15:50:23 +0200 |
---|---|---|
committer | vantezzen <properly@protonmail.com> | 2019-09-07 15:50:23 +0200 |
commit | e7a74514c1e7c3833dfdcf5900cb87f9e6e8354e (patch) | |
tree | b8314e4155503b135dcb07e8b4a0e847e25c19cf /src/stores/UserStore.js | |
parent | Update CHANGELOG.md (diff) | |
parent | Update CHANGELOG.md (diff) | |
download | ferdium-app-e7a74514c1e7c3833dfdcf5900cb87f9e6e8354e.tar.gz ferdium-app-e7a74514c1e7c3833dfdcf5900cb87f9e6e8354e.tar.zst ferdium-app-e7a74514c1e7c3833dfdcf5900cb87f9e6e8354e.zip |
Merge branch 'master' of https://github.com/meetfranz/franz into franz-5.3.0
Diffstat (limited to 'src/stores/UserStore.js')
-rw-r--r-- | src/stores/UserStore.js | 68 |
1 files changed, 67 insertions, 1 deletions
diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index d813e97b1..e1d9672b6 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js | |||
@@ -2,11 +2,15 @@ import { observable, computed, action } from 'mobx'; | |||
2 | import moment from 'moment'; | 2 | import moment from 'moment'; |
3 | import jwt from 'jsonwebtoken'; | 3 | import jwt from 'jsonwebtoken'; |
4 | import localStorage from 'mobx-localstorage'; | 4 | import localStorage from 'mobx-localstorage'; |
5 | import ms from 'ms'; | ||
5 | 6 | ||
6 | import { isDevMode } from '../environment'; | 7 | import { isDevMode } from '../environment'; |
7 | import Store from './lib/Store'; | 8 | import Store from './lib/Store'; |
8 | import Request from './lib/Request'; | 9 | import Request from './lib/Request'; |
9 | import CachedRequest from './lib/CachedRequest'; | 10 | import CachedRequest from './lib/CachedRequest'; |
11 | import { sleep } from '../helpers/async-helpers'; | ||
12 | import { getPlan } from '../helpers/plan-helpers'; | ||
13 | import { PLANS } from '../config'; | ||
10 | 14 | ||
11 | const debug = require('debug')('Franz:UserStore'); | 15 | const debug = require('debug')('Franz:UserStore'); |
12 | 16 | ||
@@ -36,6 +40,8 @@ export default class UserStore extends Store { | |||
36 | 40 | ||
37 | @observable passwordRequest = new Request(this.api.user, 'password'); | 41 | @observable passwordRequest = new Request(this.api.user, 'password'); |
38 | 42 | ||
43 | @observable activateTrialRequest = new Request(this.api.user, 'activateTrial'); | ||
44 | |||
39 | @observable inviteRequest = new Request(this.api.user, 'invite'); | 45 | @observable inviteRequest = new Request(this.api.user, 'invite'); |
40 | 46 | ||
41 | @observable getUserInfoRequest = new CachedRequest(this.api.user, 'getInfo'); | 47 | @observable getUserInfoRequest = new CachedRequest(this.api.user, 'getInfo'); |
@@ -56,7 +62,9 @@ export default class UserStore extends Store { | |||
56 | 62 | ||
57 | @observable accountType; | 63 | @observable accountType; |
58 | 64 | ||
59 | @observable hasCompletedSignup = null; | 65 | @observable hasCompletedSignup = false; |
66 | |||
67 | @observable hasActivatedTrial = false; | ||
60 | 68 | ||
61 | @observable userData = {}; | 69 | @observable userData = {}; |
62 | 70 | ||
@@ -76,6 +84,7 @@ export default class UserStore extends Store { | |||
76 | this.actions.user.retrievePassword.listen(this._retrievePassword.bind(this)); | 84 | this.actions.user.retrievePassword.listen(this._retrievePassword.bind(this)); |
77 | this.actions.user.logout.listen(this._logout.bind(this)); | 85 | this.actions.user.logout.listen(this._logout.bind(this)); |
78 | this.actions.user.signup.listen(this._signup.bind(this)); | 86 | this.actions.user.signup.listen(this._signup.bind(this)); |
87 | this.actions.user.activateTrial.listen(this._activateTrial.bind(this)); | ||
79 | this.actions.user.invite.listen(this._invite.bind(this)); | 88 | this.actions.user.invite.listen(this._invite.bind(this)); |
80 | this.actions.user.update.listen(this._update.bind(this)); | 89 | this.actions.user.update.listen(this._update.bind(this)); |
81 | this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); | 90 | this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); |
@@ -86,6 +95,7 @@ export default class UserStore extends Store { | |||
86 | this.registerReactions([ | 95 | this.registerReactions([ |
87 | // this._requireAuthenticatedUser, | 96 | // this._requireAuthenticatedUser, |
88 | this._getUserData.bind(this), | 97 | this._getUserData.bind(this), |
98 | this._resetTrialActivationState.bind(this), | ||
89 | ]); | 99 | ]); |
90 | } | 100 | } |
91 | 101 | ||
@@ -141,10 +151,34 @@ export default class UserStore extends Store { | |||
141 | return this.getUserInfoRequest.execute().result || {}; | 151 | return this.getUserInfoRequest.execute().result || {}; |
142 | } | 152 | } |
143 | 153 | ||
154 | @computed get team() { | ||
155 | return this.data.team || null; | ||
156 | } | ||
157 | |||
144 | @computed get isPremium() { | 158 | @computed get isPremium() { |
145 | return !!this.data.isPremium; | 159 | return !!this.data.isPremium; |
146 | } | 160 | } |
147 | 161 | ||
162 | @computed get isPremiumOverride() { | ||
163 | return ((!this.team || !this.team.plan) && this.isPremium) || (this.team.state === 'expired' && this.isPremium); | ||
164 | } | ||
165 | |||
166 | @computed get isPersonal() { | ||
167 | if (!this.team || !this.team.plan) return false; | ||
168 | const plan = getPlan(this.team.plan); | ||
169 | |||
170 | return plan === PLANS.PERSONAL; | ||
171 | } | ||
172 | |||
173 | @computed get isPro() { | ||
174 | if (this.isPremiumOverride) return true; | ||
175 | |||
176 | if (!this.team || (!this.team.plan || this.team.state === 'expired')) return false; | ||
177 | const plan = getPlan(this.team.plan); | ||
178 | |||
179 | return plan === PLANS.PRO || plan === PLANS.LEGACY; | ||
180 | } | ||
181 | |||
148 | @computed get legacyServices() { | 182 | @computed get legacyServices() { |
149 | return this.getLegacyServicesRequest.execute() || {}; | 183 | return this.getLegacyServicesRequest.execute() || {}; |
150 | } | 184 | } |
@@ -190,6 +224,21 @@ export default class UserStore extends Store { | |||
190 | this.actionStatus = request.result.status || []; | 224 | this.actionStatus = request.result.status || []; |
191 | } | 225 | } |
192 | 226 | ||
227 | @action async _activateTrial({ planId }) { | ||
228 | debug('activate trial', planId); | ||
229 | |||
230 | this.activateTrialRequest.execute({ | ||
231 | plan: planId, | ||
232 | }); | ||
233 | |||
234 | await this.activateTrialRequest._promise; | ||
235 | |||
236 | this.hasActivatedTrial = true; | ||
237 | |||
238 | this.stores.features.featuresRequest.invalidate({ immediately: true }); | ||
239 | this.stores.user.getUserInfoRequest.invalidate({ immediately: true }); | ||
240 | } | ||
241 | |||
193 | @action async _invite({ invites }) { | 242 | @action async _invite({ invites }) { |
194 | const data = invites.filter(invite => invite.email !== ''); | 243 | const data = invites.filter(invite => invite.email !== ''); |
195 | 244 | ||
@@ -305,6 +354,14 @@ export default class UserStore extends Store { | |||
305 | } | 354 | } |
306 | } | 355 | } |
307 | 356 | ||
357 | async _resetTrialActivationState() { | ||
358 | if (this.hasActivatedTrial) { | ||
359 | await sleep(ms('12s')); | ||
360 | |||
361 | this.hasActivatedTrial = false; | ||
362 | } | ||
363 | } | ||
364 | |||
308 | // Helpers | 365 | // Helpers |
309 | _parseToken(authToken) { | 366 | _parseToken(authToken) { |
310 | try { | 367 | try { |
@@ -334,6 +391,15 @@ export default class UserStore extends Store { | |||
334 | } | 391 | } |
335 | } | 392 | } |
336 | 393 | ||
394 | getAuthURL(url) { | ||
395 | const parsedUrl = new URL(url); | ||
396 | const params = new URLSearchParams(parsedUrl.search.slice(1)); | ||
397 | |||
398 | params.append('authToken', this.authToken); | ||
399 | |||
400 | return `${parsedUrl.origin}${parsedUrl.pathname}?${params.toString()}`; | ||
401 | } | ||
402 | |||
337 | async _migrateUserLocale() { | 403 | async _migrateUserLocale() { |
338 | await this.getUserInfoRequest._promise; | 404 | await this.getUserInfoRequest._promise; |
339 | 405 | ||