refactor: error handling confirm email

This commit is contained in:
Sergey Yarkov 2021-09-08 20:29:41 +03:00
parent 20e3b99ba8
commit fc31f1fcbd
3 changed files with 41 additions and 26 deletions

View File

@ -41,16 +41,19 @@ const useUpdateUserEmailQuery = () => {
} }
if (error.response) { if (error.response) {
switch (error.response.status) { if (error.response.status === 503) {
case 503: switch (error.response.data.code) {
toast({ title: 'Service unavailable. Please, try later again.', status: 'error' }); case 'E_CONFIRM_CODE_EXIST':
toast({ title: 'Please, try send code later.', status: 'info' });
break; break;
default: default:
handleError(error); toast({ title: `Unable to send confirmation code, try again later.`, status: 'error' });
break; break;
} }
} else { } else {
console.error(error); handleError(error);
}
} else {
handleError(error); handleError(error);
} }
} finally { } finally {

View File

@ -16,34 +16,16 @@ export default class UserService {
this.api = api; this.api = api;
} }
/**
* Fetch authorized user data
*
* @returns User data
*/
public async fetchMe(): Promise<IUserResult> { public async fetchMe(): Promise<IUserResult> {
const result = await this.api.get('v1/me'); const result = await this.api.get('v1/me');
return result.data; return result.data;
} }
/**
* Update authorized user contacts data
*
* @param data User contacts
* @returns Updated user contacts data
*/
public async updateContacts(data: IUserContacts): Promise<IUpdatedContactsResult> { public async updateContacts(data: IUserContacts): Promise<IUpdatedContactsResult> {
const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data)); const result = await this.api.put('v1/me/contacts', helpers.removeEmptyValues(data));
return result.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<IDataResult> { public async updatePassword(oldPassword: string, newPassword: string): Promise<IDataResult> {
const result = await this.api.patch('v1/me/password', { const result = await this.api.patch('v1/me/password', {
oldPassword, oldPassword,

View File

@ -35,6 +35,11 @@ export default class UserStore {
makeAutoObservable(this); makeAutoObservable(this);
} }
/**
* Initialize authorized user in store
*
* @returns User result
*/
public async loadCurrentUserData(): Promise<IUserResult> { public async loadCurrentUserData(): Promise<IUserResult> {
try { try {
const result = await this.userService.fetchMe(); 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<IUpdatedContactsResult> { public async updateCurrentUserContacts(data: IUserContacts): Promise<IUpdatedContactsResult> {
try { try {
const result = await this.userService.updateContacts(data); 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<IDataResult> { public async updateCurrentUserPassword(oldPassword: string, newPassword: string): Promise<IDataResult> {
try { try {
const result = await this.userService.updatePassword(oldPassword, newPassword); 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<IUpdatedUserEmail> { public async updateCurrentUserEmailConfirm(email: string, confirmationCode: string): Promise<IUpdatedUserEmail> {
try { try {
const result = await this.userService.updateEmailConfirm(email, confirmationCode); const result = await this.userService.updateEmailConfirm(email, confirmationCode);
runInAction(() => {
if (this.me) {
this.me.email = result.data.email;
}
});
return result; return result;
} catch (error) { } catch (error) {
throw error; throw error;