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

79 lines
2.1 KiB
JavaScript
Raw Normal View History

import React, {useState, useEffect, useReducer} from 'react';
2020-01-07 20:19:11 +00:00
import './App.scss';
2020-01-07 21:03:59 +00:00
import config from './config';
2020-01-07 20:19:11 +00:00
import { Switch, Route, BrowserRouter as Router } from 'react-router-dom';
2020-01-08 19:38:09 +00:00
2020-01-08 07:18:08 +00:00
import socketIOClient from 'socket.io-client';
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';
2020-01-08 07:18:08 +00:00
export const socket = socketIOClient(config.apiAddress);
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
);
useEffect(() => {
fetch(config.apiAddress)
.then(res => res.text())
.catch(err => setError([...error, err]))
.then(data => setFetchData(data))
})
useEffect(() => {
socket.emit('connect');
socket.on('connect', data => setSocketData('socket connected'));
socket.on('connect_error', err => setError([...error, err]));
socket.on('error', err => setError([...error, err]))
})
2020-01-07 20:19:11 +00:00
return (
<Router>
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> : <></>}
{error ? error.map(err => <p>{err}</p>): <></>}
</div>
</Router>
2020-01-07 20:19:11 +00:00
);
}
export default App;