-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ca6841d
commit 92279a1
Showing
14 changed files
with
138 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"profile_validate_incorrect_age_text": "profile_validate_incorrect_age_text", | ||
"profile_validate_incorrect_user_age": "profile_validate_incorrect_user_age", | ||
"profile_validate_incorrect_user_data_text": "profile_validate_incorrect_user_data_text", | ||
"profile_validate_incorrect_username_text": "profile_validate_incorrect_username_text" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const validateProfileErrors = { | ||
INCORRECT_USER_DATA: 'profile_validate_incorrect_user_data_text', | ||
INCORRECT_AGE: 'profile_validate_incorrect_age_text', | ||
INCORRECT_USERNAME: 'profile_validate_incorrect_username_text', | ||
INCORRECT_CITY: 'profile_validate_incorrect_city_text', | ||
INCORRECT_CURRENCY: 'profile_validate_incorrect_currency_text', | ||
INCORRECT_AVATAR: 'profile_validate_incorrect_avatar_text', | ||
INCORRECT_COUNTRY: 'profile_validate_incorrect_country_text', | ||
SERVER_ERROR: 'profile_validate_server_error_text', | ||
NO_DATA: 'profile_validate_no_data_error_text', | ||
} as const; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
src/entities/Profile/model/selectors/getProfileValidateErrors/getProfileValidateErrors.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { IStateSchema } from 'shared/config/storeConfig/StateSchema'; | ||
|
||
export const getProfileValidateErrors = (state: IStateSchema) => state.profile?.validateErrors; |
17 changes: 14 additions & 3 deletions
17
src/entities/Profile/model/services/updateProfileData/updateProfileData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,33 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { IThunkConfig } from 'shared/config/storeConfig/StateSchema'; | ||
import { IProfile } from '../../types/profile'; | ||
import { IProfile, TValidateProfileError } from '../../types/profile'; | ||
import { getProfileForm } from '../../selectors/getProfileForm/getProfileForm'; | ||
import { validateProfileData } from 'entities/Profile/model/services/validateProfileData/validateProfileData'; | ||
|
||
export const updateProfileData= createAsyncThunk<IProfile, undefined, IThunkConfig<string>>( | ||
export const updateProfileData = createAsyncThunk<IProfile, undefined, IThunkConfig<TValidateProfileError[]>>( | ||
'profile/updateProfileData', | ||
async (_, thunkAPI) => { | ||
const { extra, getState } = thunkAPI; | ||
const formData = getProfileForm( getState() ); | ||
|
||
if (!formData) { | ||
return thunkAPI.rejectWithValue( [ 'NO_DATA' ] ); | ||
} | ||
|
||
const errors = validateProfileData( formData ); | ||
|
||
if (errors.length > 0) { | ||
return thunkAPI.rejectWithValue( errors ); | ||
} | ||
|
||
try { | ||
const response = await extra.api.put<IProfile>( '/profile', formData ); | ||
|
||
thunkAPI.fulfillWithValue( response.data ); | ||
|
||
return response.data; | ||
} catch (err) { | ||
return thunkAPI.rejectWithValue( 'error' ); | ||
return thunkAPI.rejectWithValue( [ 'SERVER_ERROR' ] ); | ||
} | ||
} | ||
); |
46 changes: 46 additions & 0 deletions
46
src/entities/Profile/model/services/validateProfileData/validateProfileData.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { IProfile, TValidateProfileError } from '../../types/profile'; | ||
|
||
export const validateProfileData = (profile: IProfile) => { | ||
const { | ||
country, | ||
avatar, | ||
currency, | ||
age, | ||
city, | ||
username, | ||
lastname, | ||
firstname | ||
} = profile; | ||
|
||
const errors: TValidateProfileError[] = []; | ||
|
||
if (!firstname || !lastname) { | ||
errors.push( 'INCORRECT_USER_DATA' ); | ||
} | ||
|
||
if (!username) { | ||
errors.push( 'INCORRECT_USERNAME' ); | ||
} | ||
|
||
if (!age || !Number.isInteger( age )) { | ||
errors.push( 'INCORRECT_AGE' ); | ||
} | ||
|
||
if (!city) { | ||
errors.push( 'INCORRECT_CITY' ); | ||
} | ||
|
||
if (!currency) { | ||
errors.push( 'INCORRECT_CURRENCY' ); | ||
} | ||
|
||
if (!avatar) { | ||
errors.push( 'INCORRECT_AVATAR' ); | ||
} | ||
|
||
if (!country) { | ||
errors.push( 'INCORRECT_COUNTRY' ); | ||
} | ||
|
||
return errors; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters