-
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 #10 from Nawwar14/module7-task1
- Loading branch information
Showing
22 changed files
with
311 additions
and
41 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
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,60 @@ | ||
import { AxiosInstance } from 'axios'; | ||
import { createAsyncThunk } from '@reduxjs/toolkit'; | ||
import { AppDispatch, State } from './types/types'; | ||
import { OfferObject } from './types/types'; | ||
//import {redirectToRoute} from './action'; | ||
//import { saveToken, dropToken } from './token'; | ||
import { APIRoute } from './const'; | ||
//import {AuthData} from '../types/auth-data'; | ||
//import {UserData} from '../types/user-data'; | ||
import { createAPI } from './api'; | ||
|
||
export const api = createAPI(); | ||
export const fetchOfferObjectAction = createAsyncThunk< | ||
OfferObject[], | ||
undefined, | ||
{ | ||
dispatch: AppDispatch; | ||
state: State; | ||
extra: AxiosInstance; | ||
} | ||
>('data/fetchOffers', async () => { | ||
const { data } = await api.get<OfferObject[]>(APIRoute.Offers); | ||
return data; | ||
}); | ||
/* | ||
export const checkAuthAction = createAsyncThunk<void, undefined, { | ||
dispatch: AppDispatch; | ||
state: State; | ||
extra: AxiosInstance; | ||
}>( | ||
'user/checkAuth', | ||
async (_arg, {extra: api}) => { | ||
await api.get(APIRoute.Login); | ||
}, | ||
); | ||
export const loginAction = createAsyncThunk<void, AuthData, { | ||
dispatch: AppDispatch; | ||
state: State; | ||
extra: AxiosInstance; | ||
}>( | ||
'user/login', | ||
async ({login: email, password}, {dispatch, extra: api}) => { | ||
const {data: {token}} = await api.post<UserData>(APIRoute.Login, {email, password}); | ||
saveToken(token); | ||
dispatch(redirectToRoute(AppRoute.Result)); | ||
}, | ||
); | ||
export const logoutAction = createAsyncThunk<void, undefined, { | ||
dispatch: AppDispatch; | ||
state: State; | ||
extra: AxiosInstance; | ||
}>( | ||
'user/logout', | ||
async (_arg, {extra: api}) => { | ||
await api.delete(APIRoute.Logout); | ||
dropToken(); | ||
}, | ||
);*/ |
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,52 @@ | ||
import axios, { | ||
AxiosInstance, | ||
//AxiosRequestConfig, | ||
InternalAxiosRequestConfig, | ||
AxiosResponse, | ||
AxiosError, | ||
} from 'axios'; | ||
//import AxiosRequestConfig from 'axios'; | ||
import { StatusCodes } from 'http-status-codes'; | ||
import { getToken } from './token'; | ||
|
||
const BACKEND_URL = 'https://14.design.htmlacademy.pro/six-cities'; | ||
const REQUEST_TIMEOUT = 5000; | ||
type DetailMessageType = { | ||
type: string; | ||
message: string; | ||
}; | ||
|
||
const StatusCodeMapping: Record<number, boolean> = { | ||
[StatusCodes.BAD_REQUEST]: true, | ||
[StatusCodes.UNAUTHORIZED]: true, | ||
[StatusCodes.NOT_FOUND]: true, | ||
}; | ||
|
||
const shouldDisplayError = (response: AxiosResponse) => | ||
!!StatusCodeMapping[response.status]; | ||
export const createAPI = (): AxiosInstance => { | ||
const api = axios.create({ | ||
baseURL: BACKEND_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>) => { | ||
if (error.response && shouldDisplayError(error.response)) { | ||
//const detailMessage = (error.response.data); | ||
//toast.warn(detailMessage.message); | ||
} | ||
|
||
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
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
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,8 @@ | ||
|
||
function LoadingScreen(): JSX.Element { | ||
return ( | ||
<p>Loading ...</p> | ||
); | ||
} | ||
|
||
export default LoadingScreen; |
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,35 @@ | ||
.spinner-container { | ||
display: flex; | ||
} | ||
.loader { | ||
width: 48px; | ||
height: 48px; | ||
margin-left: auto; | ||
margin-right: auto; | ||
border-radius: 50%; | ||
display: inline-block; | ||
border-top: 4px solid #000; | ||
border-right: 4px solid transparent; | ||
box-sizing: border-box; | ||
animation: rotation 1s linear infinite; | ||
} | ||
.loader::after { | ||
content: ''; | ||
box-sizing: border-box; | ||
position: absolute; | ||
left: 0; | ||
top: 0; | ||
width: 48px; | ||
height: 48px; | ||
border-radius: 50%; | ||
border-bottom: 4px solid #4481c3; | ||
border-left: 4px solid transparent; | ||
} | ||
@keyframes rotation { | ||
0% { | ||
transform: rotate(0deg); | ||
} | ||
100% { | ||
transform: rotate(360deg); | ||
} | ||
} |
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,5 @@ | ||
import './spinner.modules.css'; | ||
|
||
export default function Spinner(): JSX.Element { | ||
return <div className="spinner-container"><span className="loader"></span></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
Oops, something went wrong.