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

89 lines
2.5 KiB
JavaScript
Raw Normal View History

import React, {useState, useEffect, useReducer} from 'react';
import socketIOClient from 'socket.io-client';
2020-01-07 21:03:59 +00:00
import config from './config';
import { Switch, Route, BrowserRouter as Router } from 'react-router-dom';
2020-01-16 23:21:23 +00:00
import MainWrapper from './components/MainWrapper/MainWrapper';
import { stateReducer } from './reducers/stateReducer';
import { initState } from './reducers/init/stateReducer.init';
import indexServices from './services/api/indexServices';
import './App.scss';
export const socket = socketIOClient(config.socketAddress);
2020-01-08 07:18:08 +00:00
2020-01-16 04:04:55 +00:00
2020-01-07 20:19:11 +00:00
function App() {
2020-01-08 07:18:08 +00:00
const [fetchData, setFetchData] = useState();
const [socketData, setSocketData] = useState();
const [error, setError] = useState([]);
const [ state, dispatch ] = useReducer(
stateReducer,
{},
initState
);
const fetchIndexAPI = async () => {
const response = await indexServices.indexService();
if (response.body) {
console.log(response.body)
2020-01-18 00:32:40 +00:00
const action = {
type: 'INDEX',
message: 'SET_USER',
body: response.body
2020-01-18 00:32:40 +00:00
}
dispatch(action)
}
2020-01-18 00:32:40 +00:00
}
const socketConnect = () => {
socket.emit('connect');
2020-01-18 00:32:40 +00:00
socket.on('connected', data => setSocketData('socket connected'));
socket.on('connect_error', err => setError([...error, err]));
socket.on('error', err => setError([...error, err]))
2020-01-18 00:32:40 +00:00
}
useEffect(() => {
fetchIndexAPI();
socketConnect();
}, [socketData, state.user])
2020-01-17 20:21:43 +00:00
2020-01-07 20:19:11 +00:00
return (
<Router>
2020-01-18 00:32:40 +00:00
{/* {fetchOnLoad()}; */}
2020-01-16 23:21:23 +00:00
<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">
2020-01-16 23:21:23 +00:00
<MainWrapper page="room" state={state} dispatch={dispatch}/>
</Route>
<Route path="/games">
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>
<Route path="/">
{/* Add ternary for login */}
2020-01-16 23:21:23 +00:00
<MainWrapper page="home" state={state} dispatch={dispatch}/>
</Route>
2020-01-08 19:38:09 +00:00
</Switch>
2020-01-16 23:21:23 +00:00
<h1>React Boilerplate</h1>
{fetchData ? <p>{fetchData}</p> : <></>}
{socketData ? <p>{socketData}</p> : <></>}
2020-01-18 00:32:40 +00:00
{/* {error ? error.map(err => <p>{err}</p>): <></>} */}
</div>
</Router>
2020-01-07 20:19:11 +00:00
);
}
export default App;