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 backendApi +create axios interceptors +add userReducer and create function for use yandex api +add env *update SignIn.tsx for use backendApi *update SignUp.tsx for use backendApi *format and lint
- Loading branch information
Showing
17 changed files
with
383 additions
and
59 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import axios, { AxiosError, AxiosResponse } from 'axios' | ||
|
||
const instance = axios.create({ | ||
baseURL: import.meta.env.VITE_AUTH_URL, | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
withCredentials: true, | ||
}) | ||
|
||
instance.interceptors.response.use( | ||
function (response: AxiosResponse) { | ||
return response | ||
}, | ||
function (error: AxiosError) { | ||
// if app get response code 401 (died token), redirect user to sign-in form | ||
if (error.response?.status === 401) { | ||
localStorage.removeItem('user') | ||
// save page where we get 401 and redirect after login | ||
window.location.href = '/sign-in?redirectUrl=' + window.location.pathname | ||
} | ||
|
||
if (axios.isCancel(error)) return Promise.reject(error) | ||
|
||
return Promise.reject(error) | ||
} | ||
) | ||
|
||
export default instance |
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
Empty file.
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,22 @@ | ||
import { useSelector } from 'react-redux' | ||
import { Outlet } from 'react-router-dom' | ||
import { RootState } from '@/store' | ||
import { getUser, UserType } from '@/store/reducers/user-reducer' | ||
import { useEffect } from 'react' | ||
import { useAppDispatch } from '@/store' | ||
|
||
export default function PrivateLayout() { | ||
return <Outlet /> | ||
// get user from store | ||
const user = useSelector<RootState, UserType>(state => state.UserReducer.user) | ||
const dispatch = useAppDispatch() | ||
|
||
useEffect(() => { | ||
// if user is empty call backend | ||
if (user === null) { | ||
dispatch(getUser()) | ||
} | ||
}, [user]) | ||
|
||
// if the user is full, show page | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import React from 'react' | ||
import ReactDOM from 'react-dom/client' | ||
import { Provider } from 'react-redux' | ||
import { store } from '@/store' | ||
|
||
import App from './app/App' | ||
import './index.css' | ||
import '@/scss/styles.scss' | ||
|
||
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( | ||
<React.StrictMode> | ||
<App /> | ||
<Provider store={store}> | ||
<App /> | ||
</Provider> | ||
</React.StrictMode> | ||
) |
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,3 +1,10 @@ | ||
import { Link } from 'react-router-dom' | ||
|
||
export const Game = () => { | ||
return <>Тут будет игра</> | ||
return ( | ||
<> | ||
Тут будет игра | ||
<Link to={'/forum'}>Forum</Link> | ||
</> | ||
) | ||
} |
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,3 +1,13 @@ | ||
import { logoutUser } from '@/store/reducers/user-reducer' | ||
import { useAppDispatch } from '@/store' | ||
|
||
export const Profile = () => { | ||
return <>Страница профиля</> | ||
const dispatch = useAppDispatch() | ||
|
||
return ( | ||
<> | ||
Страница профиля | ||
<button onClick={() => dispatch(logoutUser())}>Выйти</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,50 @@ | ||
import { useState } from 'react' | ||
import { useAppDispatch } from '@/store' | ||
import { signInUser } from '@/store/reducers/user-reducer' | ||
import { Button } from '@/components/ui/Button/Button' | ||
import { useSearchParams } from 'react-router-dom' | ||
|
||
export const SignIn = () => { | ||
return <>Тут форма входа</> | ||
const [form, setForm] = useState({ login: '', password: '' }) | ||
const [searchParams] = useSearchParams() | ||
const [query] = useState(searchParams.get('redirectUrl')) | ||
const dispatch = useAppDispatch() | ||
|
||
const handleForm = (name: string, value: string) => { | ||
setForm({ ...form, [name]: value }) | ||
} | ||
|
||
const handleSubmit = () => dispatch(signInUser(form, query)) | ||
|
||
return ( | ||
<> | ||
<form | ||
onSubmit={e => { | ||
e.preventDefault() | ||
}}> | ||
<label> | ||
<span>Логин</span> | ||
<input | ||
onChange={e => handleForm('login', e.target.value)} | ||
type="text" | ||
name={'login'} | ||
/> | ||
</label> | ||
<label> | ||
<span>Пароль</span> | ||
<input | ||
onChange={e => handleForm('password', e.target.value)} | ||
type="password" | ||
name={'password'} | ||
/> | ||
</label> | ||
<Button | ||
text={'войти'} | ||
useFixWidth={true} | ||
onClick={() => handleSubmit()} | ||
/> | ||
</form> | ||
<Button text={'Регистрация'} useFixWidth={true} href={'/sign-up'} /> | ||
</> | ||
) | ||
} |
Oops, something went wrong.