From 9051016362bc2cc59922dbba6705bb661211c7f6 Mon Sep 17 00:00:00 2001 From: algafur Date: Thu, 26 Sep 2024 10:50:24 +0500 Subject: [PATCH] fix: settings --- src/app/routes/router.tsx | 29 +++++++++++++- src/features/auth/auth-form/auth-form.tsx | 4 ++ src/features/settings/new-post/new-post.tsx | 43 +++++++++++---------- src/features/settings/new-user/new-user.tsx | 6 ++- src/shared/api/api-query/api-query.ts | 1 + src/shared/redux/settings/settings-slice.ts | 6 ++- src/widget/header/header.tsx | 17 +++++--- 7 files changed, 76 insertions(+), 30 deletions(-) diff --git a/src/app/routes/router.tsx b/src/app/routes/router.tsx index aec8c0a..31afd55 100644 --- a/src/app/routes/router.tsx +++ b/src/app/routes/router.tsx @@ -1,13 +1,38 @@ +import { useNotify } from 'app/providers/app' import { ErrorPage } from 'pages/error' import { Settings } from 'pages/settings' -import React, { lazy } from 'react' +import React, { lazy, useEffect } from 'react' +import { useCookies } from 'react-cookie' import { Route, Routes } from 'react-router' +import { getAxiosInstance } from 'shared/api/api-query/api-query' +import { setRoleState } from 'shared/redux/settings/settings-slice' +import { useAppDispatch, useAppSelector } from 'shared/redux/store' import { PreloaderPage } from 'shared/ui/preloader-page' const Router = () => { const Main = lazy(async () => await import('pages/main')) const Dashboard = lazy(async () => await import('pages/dashboard')) + const dispatch = useAppDispatch() + const { openNotification } = useNotify() + const [{ token }] = useCookies(['token']) + const access = useAppSelector((state) => state.settings.role) + + const fetchData = async () => { + try { + const axiosInstance = await getAxiosInstance() + const res = await axiosInstance.get('/auth/me') + dispatch(setRoleState(res.data.role || '')) + } catch (error) { + dispatch(setRoleState('')) + openNotification('Произошла ошибка при загрузке данных о пользователе') + } + } + + useEffect(() => { + if (token) fetchData() + }, [token]) + return ( } /> @@ -20,7 +45,7 @@ const Router = () => { } > } /> - } /> + {access === 'ADMINISTRATOR' && } />} ) diff --git a/src/features/auth/auth-form/auth-form.tsx b/src/features/auth/auth-form/auth-form.tsx index fdb074d..2fc5692 100644 --- a/src/features/auth/auth-form/auth-form.tsx +++ b/src/features/auth/auth-form/auth-form.tsx @@ -8,10 +8,13 @@ import React, { FC } from 'react' import { useCookies } from 'react-cookie' import { getAxiosInstance } from 'shared/api/api-query/api-query' import { useToggle } from 'shared/lib/hooks/use-toggle' +import { setRoleState } from 'shared/redux/settings/settings-slice' +import { useAppDispatch } from 'shared/redux/store' export const AuthForm: FC = () => { const [auth, handleAuth] = useToggle() const { openNotification } = useNotify() + const dispatch = useAppDispatch() const [_, updateToken] = useCookies(['token']) const onFinish: FormProps['onFinish'] = async (data) => { @@ -34,6 +37,7 @@ export const AuthForm: FC = () => { await axiosInstance.post('/auth/login', data).then((res) => { updateToken('token', res.data.token) + dispatch(setRoleState('refetch')) }) } catch (error) { openNotification('Что-то пошло не так') diff --git a/src/features/settings/new-post/new-post.tsx b/src/features/settings/new-post/new-post.tsx index 9bf282e..fb64f9a 100644 --- a/src/features/settings/new-post/new-post.tsx +++ b/src/features/settings/new-post/new-post.tsx @@ -62,39 +62,42 @@ export const NewPost: FC = ({ open, handeOpen, item, refetch }) = setLoading(true) try { const axiosInstance = await getAxiosInstance() + if (item && item.id) { const users = data.users - ? data.users.map((user) => { - return { - post: item.id, - user: user, - } - }) + ? data.users.map((user) => ({ + post: item.id, + user: user, + })) : [] - await axiosInstance.patch(`/posts/${item.id}`, data).then(() => { - openNotification('Пост изменен', 'success') - handeOpen() - refetch() - setLoading(false) - }) - await axiosInstance.post('/posts-users-access', users) + await axiosInstance.patch(`/posts/${item.id}`, data) + openNotification('Пост изменен', 'success') + handeOpen() + refetch() + await axiosInstance.patch(`/posts-users-access/post/${item.id}`, users) } else { - await axiosInstance.post('/posts', data).then(() => { - openNotification('Пост создан', 'success') - handeOpen() - refetch() - setLoading(false) + const res = await axiosInstance.post('/posts', data) + await axiosInstance.patch(`/posts-users-access/post/${res.data.id}`, { + users: data.users, }) + openNotification('Пост создан', 'success') + handeOpen() + refetch() } } catch (error) { - openNotification('Что-то пошло не так') + openNotification('Что-то пошло не так', 'error') + } finally { setLoading(false) } } return ( <> - +
= ({ open, handleModal, item, refetch }) }, [item, open]) return ( - + { (error) => { if (error.response && error.response.status === 401) { Cookies.remove('token') + window.location.replace('/') } return Promise.reject(error) }, diff --git a/src/shared/redux/settings/settings-slice.ts b/src/shared/redux/settings/settings-slice.ts index 4446178..58ac656 100644 --- a/src/shared/redux/settings/settings-slice.ts +++ b/src/shared/redux/settings/settings-slice.ts @@ -3,6 +3,7 @@ import { createSlice } from '@reduxjs/toolkit' const initialState = { posts: [], users: [], + role: '', } export const SettingsSlice = createSlice({ @@ -15,9 +16,12 @@ export const SettingsSlice = createSlice({ setUsersState: (state, action) => { state.users = action.payload }, + setRoleState: (state, action) => { + state.role = action.payload + }, }, }) -export const { setPostsState, setUsersState } = SettingsSlice.actions +export const { setPostsState, setUsersState, setRoleState } = SettingsSlice.actions export default SettingsSlice.reducer diff --git a/src/widget/header/header.tsx b/src/widget/header/header.tsx index c07f02f..5e407a1 100644 --- a/src/widget/header/header.tsx +++ b/src/widget/header/header.tsx @@ -3,12 +3,14 @@ import { FC } from 'react' import { useCookies } from 'react-cookie' import { useMediaQuery } from 'react-responsive' import { useNavigate } from 'react-router' +import { useAppSelector } from 'shared/redux/store' export const Header: FC = () => { const navigator = useNavigate() // eslint-disable-next-line @typescript-eslint/no-unused-vars const [token, update, removeToken] = useCookies(['token']) const isMobile = useMediaQuery({ query: '(max-width: 768px )' }) + const access = useAppSelector((state) => state.settings.role) return ( { navigator('/')}> Аналитика - navigator('/settings')} - > - Настройки - + + {access === 'ADMINISTRATOR' && ( + navigator('/settings')} + > + Настройки + + )} removeToken('token')}> Выйти