/
lanre
/
notes
Обзор
Документация
Войти
/
lanre
/
notes
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
client/src/components/HomeWrapper.tsx
38 строк
1 KB
Mellettan
add JSDocs
24 май 2024, 12:07
24 май 2024, 12:07
10f8e35
Код
Авторство
О чём код?
import React, { PropsWithChildren, useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { AppDispatch, RootState } from '../stores/rootStore'; import { auth } from '../stores/authStore'; import { Navigate } from 'react-router-dom'; /** * A React functional component that wraps its children with authentication and loading logic. * * @param {PropsWithChildren} props - The props object that contains the children to be wrapped. * @return {JSX.Element} The wrapped children or a loading or redirect component based on authentication state. */ const HomeWrapper: React.FC<PropsWithChildren> = (props) => { const dispatch = useDispatch<AppDispatch>(); const loggedIn = useSelector((state: RootState) => state.auth.isAuthorized); const isInitializing = useSelector((state: RootState) => state.auth.isInitializing); const token = localStorage.getItem('token'); useEffect(() => { if (token) dispatch(auth()); }, [dispatch, token]); if (isInitializing){ return <div>Loading...</div> }else{ if (!loggedIn){ return <Navigate to="/sign-in" /> } else{ return <>{props.children}</>; } } }; export default HomeWrapper;