Skip to content

Commit

Permalink
add json settings
Browse files Browse the repository at this point in the history
  • Loading branch information
vladislav0sidorov committed Aug 26, 2023
1 parent f2f1290 commit ae5eb64
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 34 deletions.
96 changes: 73 additions & 23 deletions json-server/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,27 +109,39 @@
"id": "1",
"username": "Vladislav",
"password": "123",
"roles": ["ADMIN"],
"roles": [
"ADMIN"
],
"features": {
"isArticleRaitingEnebled": true
},
"jsonSettings": {
"theme": "app_purple_theme"
},
"avatar": "https://sun9-60.userapi.com/impg/eqo5sYxr_jyw_yWO9_pKJRMkx8WVwVENfJrecQ/1zVSkEZs5NM.jpg?size=1620x2160&quality=95&sign=73cab6427b2934395f04d96835eb3720&type=album"
},
{
"id": "2",
"username": "Ksu",
"password": "1234",
"roles": ["USER"],
"roles": [
"USER"
],
"features": {
"isArticleRaitingEnebled": false
},
"jsonSettings": {
"theme": "app_dark_green"
},
"avatar": "https://sun9-36.userapi.com/impf/c850220/v850220268/12e2c7/TxYnZV8Q7N4.jpg?size=1536x2048&quality=96&sign=e3a114e332c19380899361b7d5084f5d&type=album"
},
{
"id": "3",
"username": "test-user",
"password": "123",
"roles": ["ADMIN"],
"roles": [
"ADMIN"
],
"avatar": "https://sun9-34.userapi.com/impg/UzNXUUdynHLaVwLdtI9UYpHGAIQ_PhRPsOlbgA/AGeZ7EkXPqo.jpg?size=749x811&quality=95&sign=3969dbae6e15b69972c3db5c3d5cb93c&type=album"
}
],
Expand Down Expand Up @@ -177,7 +189,9 @@
"views": 1022,
"createdAt": "26.04.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand Down Expand Up @@ -247,7 +261,9 @@
"views": 5204,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -269,7 +285,9 @@
"views": 94002,
"createdAt": "26.02.2019",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -291,7 +309,9 @@
"views": 10222,
"createdAt": "24.01.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -313,7 +333,9 @@
"views": 10222,
"createdAt": "26.02.2020",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -335,7 +357,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -357,7 +381,9 @@
"views": 100,
"createdAt": "21.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -379,7 +405,9 @@
"views": 102222,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -401,7 +429,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -423,7 +453,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -445,7 +477,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -467,7 +501,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -489,7 +525,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -511,7 +549,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -533,7 +573,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -555,7 +597,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -577,7 +621,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["IT"],
"type": [
"IT"
],
"blocks": [
{
"id": "1",
Expand All @@ -599,7 +645,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["ECONOMICS"],
"type": [
"ECONOMICS"
],
"blocks": [
{
"id": "1",
Expand All @@ -621,7 +669,9 @@
"views": 1022,
"createdAt": "26.02.2022",
"userId": "1",
"type": ["SCIENCE"],
"type": [
"SCIENCE"
],
"blocks": [
{
"id": "1",
Expand Down Expand Up @@ -752,4 +802,4 @@
"id": "sRPy2F-"
}
]
}
}
18 changes: 13 additions & 5 deletions src/app/providers/ThemeProvider/ui/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { useMemo, useState, ReactNode } from 'react'
import { useMemo, useState, ReactNode, useEffect } from 'react'

import { ThemeContext } from '../../../../shared/lib/context/ThemeContext'

import { Theme } from '@/shared/const/theme'
import { LOCAL_STORAGE_ITEM_KEY } from '@/shared/const/lodalStorage'

const defaulTheme = (localStorage.getItem(LOCAL_STORAGE_ITEM_KEY) as Theme) || Theme.LIGHT
import { useJsonUserSettings } from '@/entities/User'

interface ThemeProviderProps {
children: ReactNode
Expand All @@ -14,7 +12,17 @@ interface ThemeProviderProps {

const ThemeProvider = (props: ThemeProviderProps) => {
const { initialTheme, children } = props
const [theme, setTheme] = useState<Theme>(initialTheme || defaulTheme)
const { theme: defaultTheme = Theme.LIGHT } = useJsonUserSettings()
const [isThemeInited, setIsThemeInited] = useState(false)

const [theme, setTheme] = useState<Theme>(initialTheme || defaultTheme)

useEffect(() => {
if (!isThemeInited) {
setTheme(defaultTheme)
setIsThemeInited(true)
}
}, [defaultTheme, isThemeInited])

const defaultProps = useMemo(
() => ({
Expand Down
4 changes: 4 additions & 0 deletions src/entities/User/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export { saveJsonSettings } from './model/services/saveJsonSettings'

export { useJsonUserSettings } from './model/selectors/jsonUserSettings/jsonUserSettings'

export { isUserAdmin, isUserManager, getUserRoleSelector } from './model/selectors/userRoleSelector/userRoleSelector'

export { getUserInited } from './model/selectors/getUserInited.ts/getUserInited'
Expand Down
23 changes: 23 additions & 0 deletions src/entities/User/model/api/userApi.ts
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
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,
)
41 changes: 41 additions & 0 deletions src/entities/User/model/services/saveJsonSettings.ts
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')
}
},
)
9 changes: 9 additions & 0 deletions src/entities/User/model/slice/userSlice.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import { UserSchema, User } from '../types/user'
import { saveJsonSettings } from '../services/saveJsonSettings'
import { JsonSettings } from '../types/jsonSettings'

import { USER_LOCALSTORAGE_KEY } from '@/shared/const/lodalStorage'
import { setFeatureFlags } from '@/shared/lib/features'
Expand All @@ -27,6 +29,13 @@ export const userSlice = createSlice({
localStorage.removeItem(USER_LOCALSTORAGE_KEY)
},
},
extraReducers: (builder) => {
builder.addCase(saveJsonSettings.fulfilled, (state, action: PayloadAction<JsonSettings>) => {
if (state.authData) {
state.authData.jsonSettings = action.payload
}
})
},
})

export const { actions: userActions } = userSlice
Expand Down
Loading

0 comments on commit ae5eb64

Please sign in to comment.