From fc31f1fcbd85e06b84b2148bacc8c2044e425e89 Mon Sep 17 00:00:00 2001 From: Sergey Yarkov Date: Wed, 8 Sep 2021 20:29:41 +0300 Subject: [PATCH] refactor: error handling confirm email --- src/hooks/useUpdateUserEmailQuery.ts.ts | 19 +++++++++------- src/services/UserService.ts | 18 --------------- src/stores/UserStore.ts | 30 +++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/hooks/useUpdateUserEmailQuery.ts.ts b/src/hooks/useUpdateUserEmailQuery.ts.ts index 2038dd2..1c5de0a 100644 --- a/src/hooks/useUpdateUserEmailQuery.ts.ts +++ b/src/hooks/useUpdateUserEmailQuery.ts.ts @@ -41,16 +41,19 @@ const useUpdateUserEmailQuery = () => { } if (error.response) { - switch (error.response.status) { - case 503: - toast({ title: 'Service unavailable. Please, try later again.', status: 'error' }); - break; - default: - handleError(error); - break; + if (error.response.status === 503) { + switch (error.response.data.code) { + case 'E_CONFIRM_CODE_EXIST': + toast({ title: 'Please, try send code later.', status: 'info' }); + break; + default: + toast({ title: `Unable to send confirmation code, try again later.`, status: 'error' }); + break; + } + } else { + handleError(error); } } else { - console.error(error); handleError(error); } } finally { diff --git a/src/services/UserService.ts b/src/services/UserService.ts index d709d4f..04a411f 100644 --- a/src/services/UserService.ts +++ b/src/services/UserService.ts @@ -16,34 +16,16 @@ export default class UserService { this.api = api; } - /** - * Fetch authorized user data - * - * @returns User data - */ public async fetchMe(): Promise { const result = await this.api.get('v1/me'); return result.data; } - /** - * Update authorized user contacts data - * - * @param data User contacts - * @returns Updated user contacts data - */ public async updateContacts(data: IUserContacts): Promise { const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data)); return result.data; } - /** - * Update password of authorized user - * - * @param oldPassword string - * @param newPassword string - * @returns Result data - */ public async updatePassword(oldPassword: string, newPassword: string): Promise { const result = await this.api.patch('v1/me/password', { oldPassword, diff --git a/src/stores/UserStore.ts b/src/stores/UserStore.ts index cb18581..0b2a69e 100644 --- a/src/stores/UserStore.ts +++ b/src/stores/UserStore.ts @@ -35,6 +35,11 @@ export default class UserStore { makeAutoObservable(this); } + /** + * Initialize authorized user in store + * + * @returns User result + */ public async loadCurrentUserData(): Promise { try { const result = await this.userService.fetchMe(); @@ -60,6 +65,12 @@ export default class UserStore { } } + /** + * Update contacts of authorized user + * + * @param data New contacts + * @returns Updated user contacts + */ public async updateCurrentUserContacts(data: IUserContacts): Promise { try { const result = await this.userService.updateContacts(data); @@ -76,6 +87,13 @@ export default class UserStore { } } + /** + * Update password of authorized user + * + * @param oldPassword Old password + * @param newPassword New password + * @returns Data result status + */ public async updateCurrentUserPassword(oldPassword: string, newPassword: string): Promise { try { const result = await this.userService.updatePassword(oldPassword, newPassword); @@ -100,9 +118,21 @@ export default class UserStore { } } + /** + * Confirm code and update current user email + * + * @param email New email + * @param confirmationCode Confirmation code + * @returns Updated user email + */ public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string): Promise { try { const result = await this.userService.updateEmailConfirm(email, confirmationCode); + runInAction(() => { + if (this.me) { + this.me.email = result.data.email; + } + }); return result; } catch (error) { throw error;