mirror of
https://github.com/sergeyyarkov/educt-client.git
synced 2026-10-11 10:19:22 +03:00
refactor: error handling confirm email
This commit is contained in:
parent
20e3b99ba8
commit
fc31f1fcbd
@ -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 {
|
||||
|
||||
@ -16,34 +16,16 @@ export default class UserService {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch authorized user data
|
||||
*
|
||||
* @returns User data
|
||||
*/
|
||||
public async fetchMe(): Promise<IUserResult> {
|
||||
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<IUpdatedContactsResult> {
|
||||
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<IDataResult> {
|
||||
const result = await this.api.patch('v1/me/password', {
|
||||
oldPassword,
|
||||
|
||||
@ -35,6 +35,11 @@ export default class UserStore {
|
||||
makeAutoObservable(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize authorized user in store
|
||||
*
|
||||
* @returns User result
|
||||
*/
|
||||
public async loadCurrentUserData(): Promise<IUserResult> {
|
||||
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<IUpdatedContactsResult> {
|
||||
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<IDataResult> {
|
||||
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<IUpdatedUserEmail> {
|
||||
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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user