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.
*update backendApi *change reducer for async/await *redirects with router (spa) +add toast for error message
- Loading branch information
Showing
12 changed files
with
1,422 additions
and
783 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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,5 +1,24 @@ | ||
import { Outlet } from 'react-router-dom' | ||
import { Outlet, useNavigate } from 'react-router-dom' | ||
import {useSelector} from "react-redux"; | ||
import {RootState, useAppDispatch} from "@/store"; | ||
import {actions, getUser, 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]) | ||
|
||
return <Outlet /> | ||
} |
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,22 +1,39 @@ | ||
import { useSelector } from 'react-redux' | ||
import { Outlet } from 'react-router-dom' | ||
import { RootState } from '@/store' | ||
import { getUser, UserType } from '@/store/reducers/auth-reducer' | ||
import {Outlet, useNavigate} from 'react-router-dom' | ||
import {RootState, useAppDispatch} from '@/store' | ||
import {actions, getUser, UserType} from '@/store/reducers/auth-reducer' | ||
import { useEffect } from 'react' | ||
import { useAppDispatch } from '@/store' | ||
import {toast} from "react-toastify"; | ||
|
||
export default function PrivateLayout() { | ||
// get user from store | ||
const user = useSelector<RootState, UserType>(state => state.AuthReducer.user) | ||
const navigate = useNavigate(); | ||
const userIsLogged = window.sessionStorage.getItem('userIsLogged') === '1'; | ||
const dispatch = useAppDispatch() | ||
|
||
useEffect(() => { | ||
// if user is empty call backend | ||
if (user === null) { | ||
if (!userIsLogged) { | ||
dispatch(getUser()) | ||
.unwrap() | ||
.then((data) => { | ||
dispatch(actions.setUser(data.data)) | ||
window.sessionStorage.setItem('userIsLogged', '1') // 0 | ||
}) | ||
.catch(() => { | ||
window.sessionStorage.setItem('userIsLogged', '0') // 0 | ||
|
||
toast.error('Необходимо авторизоваться', { | ||
autoClose: 1500, | ||
onClose: () => { | ||
navigate('/sign-in') | ||
} | ||
}) | ||
}) | ||
} | ||
}, [user]) | ||
}, []) | ||
|
||
// if the user is full, show page | ||
if (userIsLogged) { | ||
return <Outlet /> | ||
} | ||
return user === null ? <h1>Загрузка...</h1> : <Outlet /> | ||
} |
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
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,13 +1,22 @@ | ||
import { logoutUser } from '@/store/reducers/auth-reducer' | ||
import { useAppDispatch } from '@/store' | ||
import {useNavigate} from "react-router-dom"; | ||
|
||
export const Profile = () => { | ||
const dispatch = useAppDispatch() | ||
const navigate = useNavigate(); | ||
|
||
const onLogoutUser = () => { | ||
dispatch(logoutUser()) | ||
.then(() => { | ||
window.sessionStorage.setItem('userIsLogged', '0') // 0 | ||
navigate('/') | ||
}) | ||
} | ||
return ( | ||
<> | ||
Страница профиля | ||
<button onClick={() => dispatch(logoutUser())}>Выйти</button> | ||
<button onClick={() => onLogoutUser()}>Выйти</button> | ||
</> | ||
) | ||
} |
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
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
Oops, something went wrong.