2020-01-16 02:13:42 +00:00
|
|
|
import React, {useState, useEffect, useReducer} from 'react';
|
2020-01-07 21:03:59 +00:00
|
|
|
import config from './config';
|
2020-01-20 20:16:46 +00:00
|
|
|
import { Switch, Route, BrowserRouter as Router, Redirect } from 'react-router-dom';
|
2020-01-18 21:02:03 +00:00
|
|
|
import MainWrapper from './pages/Layout/MainWrapper/MainWrapper';
|
2020-01-29 21:28:49 +00:00
|
|
|
import { stateReducer } from './reducers/reducer';
|
|
|
|
import { initState } from './reducers/init/reducer.init';
|
2020-01-18 03:57:27 +00:00
|
|
|
import indexServices from './services/api/indexServices';
|
|
|
|
import './App.scss';
|
2020-01-16 08:22:27 +00:00
|
|
|
|
2020-01-16 04:04:55 +00:00
|
|
|
|
2020-01-07 20:19:11 +00:00
|
|
|
function App() {
|
2020-01-16 08:22:27 +00:00
|
|
|
const [ state, dispatch ] = useReducer(
|
|
|
|
stateReducer,
|
|
|
|
{},
|
|
|
|
initState
|
|
|
|
);
|
|
|
|
|
2020-01-18 03:57:27 +00:00
|
|
|
const fetchIndexAPI = async () => {
|
|
|
|
const response = await indexServices.indexService();
|
2020-01-18 04:24:57 +00:00
|
|
|
if (response) {
|
2020-01-18 00:32:40 +00:00
|
|
|
const action = {
|
|
|
|
type: 'INDEX',
|
2020-01-18 03:57:27 +00:00
|
|
|
message: 'SET_USER',
|
2020-01-18 04:24:57 +00:00
|
|
|
body: response
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
dispatch(action)
|
2020-01-18 03:57:27 +00:00
|
|
|
}
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 04:24:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
fetchIndexAPI();
|
|
|
|
}, [])
|
|
|
|
|
2020-01-18 00:32:40 +00:00
|
|
|
const socketConnect = () => {
|
2020-01-28 06:40:03 +00:00
|
|
|
if (state.connect.type) return;
|
|
|
|
dispatch({type:'SOCKET', message: 'LAUNCH', body:{nsp:'', dispatch}});
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
socketConnect();
|
2020-01-18 04:24:57 +00:00
|
|
|
}, [])
|
2020-01-17 20:21:43 +00:00
|
|
|
|
2020-01-07 20:19:11 +00:00
|
|
|
return (
|
2020-01-16 02:13:42 +00:00
|
|
|
<Router>
|
2020-01-09 01:17:10 +00:00
|
|
|
|
2020-01-26 00:50:18 +00:00
|
|
|
<div data-testid="App" className="App">
|
2020-01-16 02:13:42 +00:00
|
|
|
<Switch>
|
|
|
|
|
|
|
|
<Route path="/account">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="account" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
2020-01-22 19:43:36 +00:00
|
|
|
<Route path="/rooms/:id">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="room" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
2020-01-24 23:50:47 +00:00
|
|
|
<Route path="/games/:id">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="game" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/news">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="news" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
2020-01-20 20:16:46 +00:00
|
|
|
|
|
|
|
<Route path="/home">
|
|
|
|
<MainWrapper page="home" state={state} dispatch={dispatch}/>
|
|
|
|
</Route>
|
2020-01-16 02:13:42 +00:00
|
|
|
|
|
|
|
<Route path="/">
|
2020-01-20 20:16:46 +00:00
|
|
|
{state.user.username
|
|
|
|
? <MainWrapper page="home" state={state} dispatch={dispatch}/>
|
|
|
|
: <MainWrapper page="news" state={state} dispatch={dispatch}/>}
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
2020-01-08 19:38:09 +00:00
|
|
|
|
2020-01-16 02:13:42 +00:00
|
|
|
</Switch>
|
2020-01-29 01:03:17 +00:00
|
|
|
<p className="App__socket-flag">{state.connect.type ? '✓' : ' ⃠'}</p>
|
2020-01-16 02:13:42 +00:00
|
|
|
</div>
|
|
|
|
</Router>
|
2020-01-07 20:19:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|