node-go/packages/server/play-node-go/src/App.js

83 lines
2.1 KiB
JavaScript
Raw Normal View History

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