-
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
e090540
commit 1478406
Showing
11 changed files
with
104 additions
and
14 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
28 changes: 28 additions & 0 deletions
28
...ntities/Profile/model/selectors/getProfileValidateErorrs/getProfileValidateErorrs.test.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,28 @@ | ||
import { DeepPartial } from '@reduxjs/toolkit'; | ||
import { StateSchema } from 'app/providers/StoreProvider'; | ||
import { getProfileValidateErorrs } from './getProfileValidateErorrs'; | ||
import { ValidateProfileError } from '../../types/profile'; | ||
|
||
describe('getProfileValidateErorrs.test', () => { | ||
test('should return value', () => { | ||
const state: DeepPartial<StateSchema> = { | ||
profile: { | ||
validateError: [ | ||
ValidateProfileError.INCORRECT_COUNTRY, | ||
ValidateProfileError.INCORRECT_USER_DATA, | ||
], | ||
}, | ||
}; | ||
|
||
expect(getProfileValidateErorrs(state as StateSchema)).toEqual([ | ||
ValidateProfileError.INCORRECT_COUNTRY, | ||
ValidateProfileError.INCORRECT_USER_DATA, | ||
]); | ||
}); | ||
|
||
test('should work with empty state', () => { | ||
const state: DeepPartial<StateSchema> = {}; | ||
|
||
expect(getProfileValidateErorrs(state as StateSchema)).toEqual(undefined); | ||
}); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/entities/Profile/model/selectors/getProfileValidateErorrs/getProfileValidateErorrs.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 { StateSchema } from 'app/providers/StoreProvider'; | ||
|
||
export const getProfileValidateErorrs = (state: StateSchema) => state.profile?.validateError; |
13 changes: 10 additions & 3 deletions
13
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,25 +1,32 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { StateSchema, ThunkConfig } from 'app/providers/StoreProvider'; | ||
import { Profile } from '../../types/profile'; | ||
import { Profile, ValidateProfileError } from '../../types/profile'; | ||
import { getProfileForm } from '../../selectors/getProfileForm/getProfileForm'; | ||
import { validateProfileData } from '../validateProfileData/validateProfileData'; | ||
|
||
export const updateProfileData = createAsyncThunk< | ||
Profile, | ||
void, | ||
ThunkConfig<string> | ||
ThunkConfig<ValidateProfileError[]> | ||
>( | ||
'profile/updateProfileData', | ||
async (_, thunkApi) => { | ||
const { extra, rejectWithValue, getState } = thunkApi; | ||
|
||
const formData = getProfileForm(getState() as StateSchema); | ||
|
||
const errors = validateProfileData(formData); | ||
|
||
if (errors.length) { | ||
return rejectWithValue(errors); | ||
} | ||
|
||
try { | ||
const response = await extra.api.put<Profile>('/profile', formData); | ||
|
||
return response.data; | ||
} catch (e) { | ||
return rejectWithValue('error'); | ||
return rejectWithValue([ValidateProfileError.SERVER_ERROR]); | ||
} | ||
}, | ||
); |
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
import { Profile, ValidateProfileError } from '../../types/profile'; | ||
|
||
export const validateProfileData = (profile: Profile) => { | ||
if (!profile) { | ||
return [ValidateProfileError.NO_DATA]; | ||
} | ||
|
||
const { | ||
country, age, firstName, secondName, | ||
} = profile; | ||
const error: ValidateProfileError[] = []; | ||
|
||
if (!firstName || !secondName) { | ||
error.push(ValidateProfileError.INCORRECT_USER_DATA); | ||
} | ||
|
||
if (!country) { | ||
error.push(ValidateProfileError.INCORRECT_COUNTRY); | ||
} | ||
|
||
if (!age) { | ||
error.push(ValidateProfileError.INCORRECT_AGE); | ||
} | ||
|
||
return error; | ||
}; |
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