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

91 lines
2.5 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';
import Sidebar from './components/Sidebar/Sidebar';
import Account from './pages/Account/Account';
import Game from './pages/Game/Game';
import Home from './pages/Home/Home';
import News from './pages/News/News';
import Room from './pages/Room/Room';
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>
<div data-testid="App" className="App">
<h1>React Boilerplate</h1>
{fetchData ? <p>{fetchData}</p> : <></>}
{socketData ? <p>{socketData}</p> : <></>}
{error ? error.map(err => <p>{err}</p>): <></>}
<Switch>
<Route path="/account">
<Sidebar page="account" state={state} dispatch={dispatch}/>
<Account state={state} dispatch={dispatch}/>
</Route>
<Route path="/rooms">
<Sidebar page="rooms"/>
<Room state={state} dispatch={dispatch}/>
</Route>
<Route path="/games">
<Sidebar page="games"/>
<Game state={state} dispatch={dispatch}/>
</Route>
<Route path="/news">
<Sidebar page="news"/>
<News state={state} dispatch={dispatch}/>
</Route>
<Route path="/">
<Sidebar page="home" state={state} dispatch={dispatch}/>
{/* Add ternary for login */}
<Home state={state} dispatch={dispatch}/>
</Route>
2020-01-08 19:38:09 +00:00
</Switch>
</div>
</Router>
2020-01-07 20:19:11 +00:00
);
}
export default App;