(WrappedComponent: ComponentType
) => { const ReturnedComponent: FC
= props => { // Достаем accessToken из redux'a - const accessToken = '23123123' //useAppSelector(authSelector.accessTokenSelector) + const accessToken = useAppSelector(auth.selector.accessToken) // Объект location на понадобиться для задания состояния при redirect'e const location = useLocation() diff --git a/frontend/src/hocs/with-response.tsx b/frontend/src/hocs/with-response.tsx new file mode 100644 index 0000000..53dc280 --- /dev/null +++ b/frontend/src/hocs/with-response.tsx @@ -0,0 +1,67 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +import { useEffect, type ComponentType, type FC } from 'react' +import { Navigate, useLocation } from 'react-router-dom' +import { auth, useAppDispatch, useAppSelector } from '../storage' +import { toast } from 'react-toastify' +import { Box } from '@mui/material' +import { Spinner } from '../components' + +type StorageType = { + selector: { + loading: any + error: any + } +} + +export const withResponse =
( + WrappedComponent: ComponentType
, + storage: T, + fecthFunc: () => any +) => { + const ReturnedComponent: FC
= props => {
+ const accessToken = useAppSelector(auth.selector.accessToken)
+ const loading: boolean = useAppSelector(storage.selector.loading)
+ const error: string = useAppSelector(storage.selector.error)
+ const dispatch = useAppDispatch()
+
+ const location = useLocation()
+
+ useEffect(() => {
+ if (accessToken) {
+ dispatch(fecthFunc())
+ }
+ }, [accessToken])
+
+ // Если ошибка, то нужно отправить пользователя на странице входа в систему
+ if (error) {
+ toast.error(error || 'Не известная ошибка при аутентификации пользователя')
+ return (
+ {currentUser?.name} {currentUser?.about} {userData?.nickname} {userData?.email}