2020-02-08 00:11:01 +00:00
|
|
|
import React, { useEffect, useReducer } from 'react';
|
2020-02-08 00:01:54 +00:00
|
|
|
import { Switch, Route, BrowserRouter as Router } 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-02-08 00:01:54 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const fetchIndexAPI = async () => {
|
|
|
|
const response = await indexServices.indexService();
|
|
|
|
if (response) {
|
|
|
|
const action = {
|
|
|
|
type: 'INDEX',
|
|
|
|
message: 'SET_USER',
|
|
|
|
body: response
|
|
|
|
}
|
|
|
|
dispatch(action)
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
2020-01-18 03:57:27 +00:00
|
|
|
}
|
2020-01-18 04:24:57 +00:00
|
|
|
fetchIndexAPI();
|
2020-02-08 00:11:01 +00:00
|
|
|
}, [ ])
|
2020-01-18 04:24:57 +00:00
|
|
|
|
2020-02-08 00:01:54 +00:00
|
|
|
|
2020-01-18 00:32:40 +00:00
|
|
|
useEffect(() => {
|
2020-02-08 00:01:54 +00:00
|
|
|
const socketConnect = () => {
|
|
|
|
if (state.connect.type) return;
|
|
|
|
dispatch({type:'SOCKET', message: 'LAUNCH', body:{nsp:'', dispatch}});
|
|
|
|
}
|
2020-01-18 00:32:40 +00:00
|
|
|
socketConnect();
|
2020-05-15 04:01:04 +00:00
|
|
|
|
|
|
|
return () => dispatch({type: 'SOCKET', message: 'DISCONNECT', body: {}});
|
2020-02-08 00:01:54 +00:00
|
|
|
}, [ state.connect ])
|
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 21:22:18 +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 21:22:18 +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;
|