diff options
Diffstat (limited to 'src/stores/UserStore.js')
-rw-r--r-- | src/stores/UserStore.js | 78 |
1 files changed, 49 insertions, 29 deletions
diff --git a/src/stores/UserStore.js b/src/stores/UserStore.js index 2e009893a..e3d57c662 100644 --- a/src/stores/UserStore.js +++ b/src/stores/UserStore.js | |||
@@ -46,7 +46,10 @@ export default class UserStore extends Store { | |||
46 | 46 | ||
47 | @observable updateUserInfoRequest = new Request(this.api.user, 'updateInfo'); | 47 | @observable updateUserInfoRequest = new Request(this.api.user, 'updateInfo'); |
48 | 48 | ||
49 | @observable getLegacyServicesRequest = new CachedRequest(this.api.user, 'getLegacyServices'); | 49 | @observable getLegacyServicesRequest = new CachedRequest( |
50 | this.api.user, | ||
51 | 'getLegacyServices', | ||
52 | ); | ||
50 | 53 | ||
51 | @observable deleteAccountRequest = new CachedRequest(this.api.user, 'delete'); | 54 | @observable deleteAccountRequest = new CachedRequest(this.api.user, 'delete'); |
52 | 55 | ||
@@ -81,13 +84,17 @@ export default class UserStore extends Store { | |||
81 | 84 | ||
82 | // Register action handlers | 85 | // Register action handlers |
83 | this.actions.user.login.listen(this._login.bind(this)); | 86 | this.actions.user.login.listen(this._login.bind(this)); |
84 | this.actions.user.retrievePassword.listen(this._retrievePassword.bind(this)); | 87 | this.actions.user.retrievePassword.listen( |
88 | this._retrievePassword.bind(this), | ||
89 | ); | ||
85 | this.actions.user.logout.listen(this._logout.bind(this)); | 90 | this.actions.user.logout.listen(this._logout.bind(this)); |
86 | this.actions.user.signup.listen(this._signup.bind(this)); | 91 | this.actions.user.signup.listen(this._signup.bind(this)); |
87 | this.actions.user.invite.listen(this._invite.bind(this)); | 92 | this.actions.user.invite.listen(this._invite.bind(this)); |
88 | this.actions.user.update.listen(this._update.bind(this)); | 93 | this.actions.user.update.listen(this._update.bind(this)); |
89 | this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); | 94 | this.actions.user.resetStatus.listen(this._resetStatus.bind(this)); |
90 | this.actions.user.importLegacyServices.listen(this._importLegacyServices.bind(this)); | 95 | this.actions.user.importLegacyServices.listen( |
96 | this._importLegacyServices.bind(this), | ||
97 | ); | ||
91 | this.actions.user.delete.listen(this._delete.bind(this)); | 98 | this.actions.user.delete.listen(this._delete.bind(this)); |
92 | 99 | ||
93 | // Reactions | 100 | // Reactions |
@@ -176,7 +183,14 @@ export default class UserStore extends Store { | |||
176 | } | 183 | } |
177 | 184 | ||
178 | @action async _signup({ | 185 | @action async _signup({ |
179 | firstname, lastname, email, password, accountType, company, plan, currency, | 186 | firstname, |
187 | lastname, | ||
188 | email, | ||
189 | password, | ||
190 | accountType, | ||
191 | company, | ||
192 | plan, | ||
193 | currency, | ||
180 | }) { | 194 | }) { |
181 | const authToken = await this.signupRequest.execute({ | 195 | const authToken = await this.signupRequest.execute({ |
182 | firstname, | 196 | firstname, |
@@ -205,7 +219,7 @@ export default class UserStore extends Store { | |||
205 | } | 219 | } |
206 | 220 | ||
207 | @action async _invite({ invites }) { | 221 | @action async _invite({ invites }) { |
208 | const data = invites.filter((invite) => invite.email !== ''); | 222 | const data = invites.filter(invite => invite.email !== ''); |
209 | 223 | ||
210 | const response = await this.inviteRequest.execute(data)._promise; | 224 | const response = await this.inviteRequest.execute(data)._promise; |
211 | 225 | ||
@@ -220,7 +234,8 @@ export default class UserStore extends Store { | |||
220 | @action async _update({ userData }) { | 234 | @action async _update({ userData }) { |
221 | if (!this.isLoggedIn) return; | 235 | if (!this.isLoggedIn) return; |
222 | 236 | ||
223 | const response = await this.updateUserInfoRequest.execute(userData)._promise; | 237 | const response = await this.updateUserInfoRequest.execute(userData) |
238 | ._promise; | ||
224 | 239 | ||
225 | this.getUserInfoRequest.patch(() => response.data); | 240 | this.getUserInfoRequest.patch(() => response.data); |
226 | this.actionStatus = response.status || []; | 241 | this.actionStatus = response.status || []; |
@@ -250,19 +265,27 @@ export default class UserStore extends Store { | |||
250 | this.isImportLegacyServicesExecuting = true; | 265 | this.isImportLegacyServicesExecuting = true; |
251 | 266 | ||
252 | // Reduces recipe duplicates | 267 | // Reduces recipe duplicates |
253 | const recipes = services.filter((obj, pos, arr) => arr.map((mapObj) => mapObj.recipe.id).indexOf(obj.recipe.id) === pos).map((s) => s.recipe.id); | 268 | const recipes = services |
269 | .filter( | ||
270 | (obj, pos, arr) => | ||
271 | arr.map(mapObj => mapObj.recipe.id).indexOf(obj.recipe.id) === pos, | ||
272 | ) | ||
273 | .map(s => s.recipe.id); | ||
254 | 274 | ||
255 | // Install recipes | 275 | // Install recipes |
256 | for (const recipe of recipes) { // eslint-disable-line no-unused-vars | 276 | for (const recipe of recipes) { |
257 | // eslint-disable-next-line | 277 | // eslint-disable-line no-unused-vars |
278 | // eslint-disable-next-line no-await-in-loop | ||
258 | await this.stores.recipes._install({ recipeId: recipe }); | 279 | await this.stores.recipes._install({ recipeId: recipe }); |
259 | } | 280 | } |
260 | 281 | ||
261 | for (const service of services) { // eslint-disable-line no-unused-vars | 282 | for (const service of services) { |
283 | // eslint-disable-line no-unused-vars | ||
262 | this.actions.service.createFromLegacyService({ | 284 | this.actions.service.createFromLegacyService({ |
263 | data: service, | 285 | data: service, |
264 | }); | 286 | }); |
265 | await this.stores.services.createServiceRequest._promise; // eslint-disable-line | 287 | // eslint-disable-next-line no-await-in-loop |
288 | await this.stores.services.createServiceRequest._promise; | ||
266 | } | 289 | } |
267 | 290 | ||
268 | this.isImportLegacyServicesExecuting = false; | 291 | this.isImportLegacyServicesExecuting = false; |
@@ -281,8 +304,7 @@ export default class UserStore extends Store { | |||
281 | 304 | ||
282 | const { router } = this.stores; | 305 | const { router } = this.stores; |
283 | const currentRoute = window.location.hash; | 306 | const currentRoute = window.location.hash; |
284 | if (!this.isLoggedIn | 307 | if (!this.isLoggedIn && currentRoute.includes('token=')) { |
285 | && currentRoute.includes('token=')) { | ||
286 | router.push(this.WELCOME_ROUTE); | 308 | router.push(this.WELCOME_ROUTE); |
287 | const token = currentRoute.split('=')[1]; | 309 | const token = currentRoute.split('=')[1]; |
288 | 310 | ||
@@ -293,20 +315,18 @@ export default class UserStore extends Store { | |||
293 | this._tokenLogin(token); | 315 | this._tokenLogin(token); |
294 | }, 1000); | 316 | }, 1000); |
295 | } | 317 | } |
296 | } else if (!this.isLoggedIn | 318 | } else if (!this.isLoggedIn && !currentRoute.includes(this.BASE_ROUTE)) { |
297 | && !currentRoute.includes(this.BASE_ROUTE)) { | ||
298 | router.push(this.WELCOME_ROUTE); | 319 | router.push(this.WELCOME_ROUTE); |
299 | } else if (this.isLoggedIn | 320 | } else if (this.isLoggedIn && currentRoute === this.LOGOUT_ROUTE) { |
300 | && currentRoute === this.LOGOUT_ROUTE) { | ||
301 | this.actions.user.logout(); | 321 | this.actions.user.logout(); |
302 | router.push(this.LOGIN_ROUTE); | 322 | router.push(this.LOGIN_ROUTE); |
303 | } else if (this.isLoggedIn | 323 | } else if ( |
304 | && currentRoute.includes(this.BASE_ROUTE) | 324 | this.isLoggedIn && |
305 | && (this.hasCompletedSignup | 325 | currentRoute.includes(this.BASE_ROUTE) && |
306 | || this.hasCompletedSignup === null)) { | 326 | (this.hasCompletedSignup || this.hasCompletedSignup === null) && |
307 | if (!isDevMode) { | 327 | !isDevMode |
308 | this.stores.router.push('/'); | 328 | ) { |
309 | } | 329 | this.stores.router.push('/'); |
310 | } | 330 | } |
311 | }; | 331 | }; |
312 | 332 | ||
@@ -316,7 +336,7 @@ export default class UserStore extends Store { | |||
316 | let data; | 336 | let data; |
317 | try { | 337 | try { |
318 | data = await this.getUserInfoRequest.execute()._promise; | 338 | data = await this.getUserInfoRequest.execute()._promise; |
319 | } catch (e) { | 339 | } catch { |
320 | return false; | 340 | return false; |
321 | } | 341 | } |
322 | 342 | ||
@@ -336,12 +356,12 @@ export default class UserStore extends Store { | |||
336 | try { | 356 | try { |
337 | const decoded = jwt.decode(authToken); | 357 | const decoded = jwt.decode(authToken); |
338 | 358 | ||
339 | return ({ | 359 | return { |
340 | id: decoded.userId, | 360 | id: decoded.userId, |
341 | tokenExpiry: moment.unix(decoded.exp).toISOString(), | 361 | tokenExpiry: moment.unix(decoded.exp).toISOString(), |
342 | authToken, | 362 | authToken, |
343 | }); | 363 | }; |
344 | } catch (err) { | 364 | } catch { |
345 | this._logout(); | 365 | this._logout(); |
346 | return false; | 366 | return false; |
347 | } | 367 | } |
@@ -372,7 +392,7 @@ export default class UserStore extends Store { | |||
372 | async _migrateUserLocale() { | 392 | async _migrateUserLocale() { |
373 | try { | 393 | try { |
374 | await this.getUserInfoRequest._promise; | 394 | await this.getUserInfoRequest._promise; |
375 | } catch (e) { | 395 | } catch { |
376 | return false; | 396 | return false; |
377 | } | 397 | } |
378 | 398 | ||