-
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
f2f1290
commit ae5eb64
Showing
11 changed files
with
194 additions
and
34 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
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,23 @@ | ||
import { JsonSettings } from '../types/jsonSettings' | ||
import { User } from '../types/user' | ||
|
||
import { rtkApi } from '@/shared/api/rtkApi' | ||
|
||
interface SetJsonSettingsArg { | ||
userId: String | ||
jsonSettings: JsonSettings | ||
} | ||
|
||
const userApi = rtkApi.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
setJsonSettings: build.mutation<User, SetJsonSettingsArg>({ | ||
query: ({ userId, jsonSettings }) => ({ | ||
url: `/users/${userId}`, | ||
method: 'PATCH', | ||
body: { jsonSettings }, | ||
}), | ||
}), | ||
}), | ||
}) | ||
|
||
export const setJsonSettingsMutation = userApi.endpoints.setJsonSettings.initiate |
9 changes: 9 additions & 0 deletions
9
src/entities/User/model/selectors/jsonUserSettings/jsonUserSettings.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,9 @@ | ||
import { JsonSettings } from '../../types/jsonSettings' | ||
|
||
import { buildSelector } from '@/shared/lib/store' | ||
|
||
const defaultJson: JsonSettings = {} | ||
|
||
export const [useJsonUserSettings, getUseJsonUserSettings] = buildSelector( | ||
(state) => state.user?.authData?.jsonSettings ?? defaultJson, | ||
) |
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,41 @@ | ||
import { createAsyncThunk } from '@reduxjs/toolkit' | ||
|
||
import { JsonSettings } from '../types/jsonSettings' | ||
import { getUserAuthData } from '../selectors/getUserAuthData/getUserAuthData' | ||
import { setJsonSettingsMutation } from '../api/userApi' | ||
import { getUseJsonUserSettings } from '../selectors/jsonUserSettings/jsonUserSettings' | ||
|
||
import { ThunkConfig } from '@/app/providers/StoreProvider' | ||
|
||
export const saveJsonSettings = createAsyncThunk<JsonSettings, JsonSettings, ThunkConfig<string>>( | ||
'user/saveJsonSettings', | ||
async (newJsonSettings, thunkApi) => { | ||
const { rejectWithValue, getState, dispatch } = thunkApi | ||
const userData = getUserAuthData(getState()) | ||
const currentSettings = getUseJsonUserSettings(getState()) | ||
|
||
if (!userData) { | ||
return rejectWithValue('Нет данных пользователя') | ||
} | ||
|
||
try { | ||
const response = await dispatch( | ||
setJsonSettingsMutation({ | ||
userId: userData.id, | ||
jsonSettings: { | ||
...currentSettings, | ||
...newJsonSettings, | ||
}, | ||
}), | ||
).unwrap() | ||
|
||
if (!response.jsonSettings) { | ||
return rejectWithValue('') | ||
} | ||
|
||
return response.jsonSettings | ||
} catch (e) { | ||
return rejectWithValue('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
Oops, something went wrong.