generated from yandex-praktikum/client-server-template-with-vite
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+add hoc component for layouts
- Loading branch information
Showing
3 changed files
with
72 additions
and
54 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { ComponentType, useEffect } from 'react' | ||
import { actions, getUser, UserType } from '@/store/reducers/auth-reducer' | ||
import { toast } from 'react-toastify' | ||
import { useNavigate, useLocation } from 'react-router-dom' | ||
import { RootState, useAppDispatch } from '@/store' | ||
import { useSelector } from 'react-redux' | ||
|
||
export default function withAuthUser<P extends object>( | ||
Component: ComponentType<P>, | ||
redirectTo?: string | ||
) { | ||
return function WrappedWithAuthUser(props: P) { | ||
const user = useSelector<RootState, UserType>( | ||
state => state.authReducer.user | ||
) | ||
const userIsLogged = window.sessionStorage.getItem('userIsLogged') === '1' | ||
const navigate = useNavigate() | ||
const dispatch = useAppDispatch() | ||
const location = useLocation() | ||
const { pathname } = location | ||
|
||
const successAuth = (redirectTo: string) => { | ||
toast.success('Вы уже авторизованы', { | ||
autoClose: 1500, | ||
onClose: () => { | ||
navigate(redirectTo) | ||
}, | ||
}) | ||
} | ||
|
||
useEffect(() => { | ||
if (!userIsLogged || user === null) { | ||
dispatch(getUser()) | ||
.unwrap() | ||
.then(data => { | ||
dispatch(actions.setUser(data)) | ||
window.sessionStorage.setItem('userIsLogged', '1') // 0 | ||
|
||
if (redirectTo) { | ||
successAuth(redirectTo) | ||
} | ||
}) | ||
.catch(() => { | ||
window.sessionStorage.setItem('userIsLogged', '0') // 1 | ||
|
||
if (!['/sign-in', '/sign-up'].includes(pathname)) { | ||
toast.error('Необходимо авторизоваться', { | ||
autoClose: 1500, | ||
onClose: () => { | ||
navigate('/sign-in') | ||
}, | ||
}) | ||
} | ||
}) | ||
} else if (redirectTo) { | ||
successAuth(redirectTo) | ||
} | ||
}, [user, userIsLogged]) | ||
|
||
return <Component {...props} userIsLogged={userIsLogged} user={user} /> | ||
} | ||
} |
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 |
---|---|---|
@@ -1,24 +1,7 @@ | ||
import { Outlet, useNavigate } from 'react-router-dom' | ||
import { useSelector } from 'react-redux' | ||
import { RootState } from '@/store' | ||
import { UserType } from '@/store/reducers/auth-reducer' | ||
import { toast } from 'react-toastify' | ||
import { useEffect } from 'react' | ||
|
||
export default function AuthLayout() { | ||
const user = useSelector<RootState, UserType>(state => state.authReducer.user) | ||
const navigate = useNavigate() | ||
|
||
useEffect(() => { | ||
if (window.sessionStorage.getItem('userIsLogged') === '1') { | ||
toast.success('Вы уже авторизованы', { | ||
autoClose: 1500, | ||
onClose: () => { | ||
navigate('/game') | ||
}, | ||
}) | ||
} | ||
}, [user]) | ||
import { Outlet } from 'react-router-dom' | ||
import withAuthUser from '@/components/hoc/withAuthUser' | ||
|
||
const AuthLayout = () => { | ||
return <Outlet /> | ||
} | ||
export default withAuthUser(AuthLayout, '/game') |
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