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 (
<>
-
+