-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from Sayafarov/module7-task1
- Loading branch information
Showing
56 changed files
with
1,418 additions
and
483 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,24 @@ | ||
/* eslint-env node */ | ||
|
||
module.exports = { | ||
env: { browser: true, es2022: true }, | ||
extends: [ | ||
'eslint:recommended', | ||
'plugin:@typescript-eslint/recommended', | ||
'plugin:react-hooks/recommended', | ||
"htmlacademy/react-typescript", | ||
], | ||
parser: '@typescript-eslint/parser', | ||
parserOptions: { ecmaVersion: 'latest', sourceType: 'module', project: 'tsconfig.json' }, | ||
settings: { react: { version: 'detect' } }, | ||
plugins: ['react-refresh'], | ||
rules: { | ||
'react-refresh/only-export-components': 'warn', | ||
}, | ||
overrides: [ | ||
{ | ||
files: [ '*test*' ], | ||
rules: { '@typescript-eslint/unbound-method': 'off' } | ||
}, | ||
], | ||
} |
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,77 @@ | ||
.spinner { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-grow: 1; | ||
} | ||
|
||
.spinner__icon { | ||
width: 48px; | ||
height: 48px; | ||
border: 5px solid #b9b9b9; | ||
border-top: 5px solid transparent; | ||
border-radius: 50%; | ||
animation: spin 1s linear infinite; | ||
} | ||
|
||
@keyframes spin { | ||
0% { transform: rotate(0deg); } | ||
100% { transform: rotate(360deg); } | ||
} | ||
|
||
.alert { | ||
color: red; | ||
margin: 10px; | ||
padding: 10px; | ||
border: 1px solid red; | ||
border-radius: 5px; | ||
background-color: #f8d7da; | ||
text-align: center; | ||
font-size: 16px; | ||
font-weight: bold; | ||
display: flex; | ||
height: fit-content; | ||
flex-grow: 1; | ||
justify-content: center; | ||
} | ||
|
||
.login__error { | ||
color: #f64747; | ||
margin: 20px 10px 10px; | ||
text-align: center; | ||
font-size: 16px; | ||
} | ||
|
||
.review-send__error { | ||
color: #f64747; | ||
margin: 20px 10px 10px; | ||
text-align: center; | ||
font-size: 16px; | ||
} | ||
|
||
fieldset { | ||
border: 0; | ||
padding: 0.01em 0 0 0; | ||
margin: 0; | ||
min-width: 0; | ||
} | ||
|
||
.not-found { | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
flex-grow: 1; | ||
height: 100vh; | ||
width: 100vw; | ||
font-size: 24px; | ||
flex-direction: column; | ||
text-align: center; | ||
} | ||
|
||
.not-found__button { | ||
font-size: 16px; | ||
padding: 10px 20px; | ||
border-radius: 5px; | ||
background-color: #000; | ||
color: #fff; | ||
} |
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,36 @@ | ||
import axios, {AxiosError, AxiosInstance, InternalAxiosRequestConfig} from 'axios'; | ||
import { getToken } from './token'; | ||
|
||
const BASE_URL = 'https://14.design.htmlacademy.pro/six-cities'; | ||
const REQUEST_TIMEOUT = 5000; | ||
|
||
class DetailMessageType { | ||
} | ||
|
||
export const createAPI = (): AxiosInstance => { | ||
const api = axios.create({ | ||
baseURL: BASE_URL, | ||
timeout: REQUEST_TIMEOUT, | ||
}); | ||
|
||
api.interceptors.request.use( | ||
(config: InternalAxiosRequestConfig) => { | ||
const token = getToken(); | ||
|
||
if (token && config.headers) { | ||
config.headers['X-Token'] = token; | ||
} | ||
|
||
return config; | ||
}, | ||
); | ||
|
||
api.interceptors.response.use( | ||
(response) => response, | ||
(error: AxiosError<DetailMessageType>) => { | ||
throw error; | ||
} | ||
); | ||
|
||
return api; | ||
}; |
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,16 @@ | ||
const AUTH_TOKEN_KEY_NAME = 'six-cities-token'; | ||
|
||
export type Token = string; | ||
|
||
export const getToken = (): Token => { | ||
const token = localStorage.getItem(AUTH_TOKEN_KEY_NAME); | ||
return token ?? ''; | ||
}; | ||
|
||
export const saveToken = (token: Token): void => { | ||
localStorage.setItem(AUTH_TOKEN_KEY_NAME, token); | ||
}; | ||
|
||
export const dropToken = (): void => { | ||
localStorage.removeItem(AUTH_TOKEN_KEY_NAME); | ||
}; |
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,3 @@ | ||
export function Alert({message}: {message: string}) { | ||
return <div className="alert">{message}</div>; | ||
} |
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,28 +1,60 @@ | ||
import {memo} from 'react'; | ||
import {useAppDispatch, useAppSelector} from '@hooks/index.ts'; | ||
import {authStatusSelector, userSelector} from '@store/user-data/selectors.ts'; | ||
import {logout} from '@store/api-actions.ts'; | ||
import {Auth} from '@type/auth.ts'; | ||
import {Link} from 'react-router-dom'; | ||
import {AppRoute} from '@const/app-routes.ts'; | ||
import {favoritesSelector} from '@store/offers-data/selectors.ts'; | ||
|
||
function Header(): JSX.Element { | ||
const dispatch = useAppDispatch(); | ||
const favorites = useAppSelector(favoritesSelector); | ||
const favoritesCount = favorites.length; | ||
const user = useAppSelector(userSelector); | ||
const authStatus = useAppSelector(authStatusSelector); | ||
|
||
const handleSignOut = () => { | ||
dispatch(logout()); | ||
}; | ||
|
||
return ( | ||
<header className="header"> | ||
<div className="container"> | ||
<div className="header__wrapper"> | ||
<div className="header__left"> | ||
<a className="header__logo-link header__logo-link--active"> | ||
<Link className="header__logo-link" to={AppRoute.Main}> | ||
<img className="header__logo" src="img/logo.svg" alt="6 cities logo" width="81" height="41" /> | ||
</a> | ||
</Link> | ||
</div> | ||
<nav className="header__nav"> | ||
<ul className="header__nav-list"> | ||
<li className="header__nav-item user"> | ||
<a className="header__nav-link header__nav-link--profile" href="#"> | ||
<div className="header__avatar-wrapper user__avatar-wrapper"> | ||
</div> | ||
<span className="header__user-name user__name">[email protected]</span> | ||
<span className="header__favorite-count">3</span> | ||
</a> | ||
</li> | ||
<li className="header__nav-item"> | ||
<a className="header__nav-link" href="#"> | ||
<span className="header__signout">Sign out</span> | ||
</a> | ||
</li> | ||
{authStatus === Auth.Auth && user !== null ? ( | ||
<> | ||
<li className="header__nav-item user"> | ||
<Link className="header__nav-link header__nav-link--profile" to={AppRoute.Favorites}> | ||
<div className="header__avatar-wrapper user__avatar-wrapper"> | ||
<img className="user__avatar" src={user.avatarUrl} alt="avatar"/> | ||
</div> | ||
<span className="header__user-name user__name">{user.email}</span> | ||
<span className="header__favorite-count">{favoritesCount}</span> | ||
</Link> | ||
</li> | ||
<li className="header__nav-item"> | ||
<Link className="header__nav-link" to={AppRoute.Main} onClick={handleSignOut}> | ||
<span className="header__signout">Sign out</span> | ||
</Link> | ||
</li> | ||
</>) | ||
: ( | ||
<li className="header__nav-item user"> | ||
<Link className="header__nav-link header__nav-link--profile" to={AppRoute.Login}> | ||
<div className="header__avatar-wrapper user__avatar-wrapper"> | ||
</div> | ||
<span className="header__login">Sign in</span> | ||
</Link> | ||
</li> | ||
)} | ||
</ul> | ||
</nav> | ||
</div> | ||
|
@@ -31,4 +63,5 @@ function Header(): JSX.Element { | |
); | ||
} | ||
|
||
export default Header; | ||
const HeaderMemo = memo(Header); | ||
export default HeaderMemo; |
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.