2020-01-16 02:13:42 +00:00
|
|
|
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
|
|
|
|
2020-01-16 02:13:42 +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-09 01:17:10 +00:00
|
|
|
|
2020-01-16 08:22:27 +00:00
|
|
|
import Sidebar from './components/Sidebar/Sidebar';
|
|
|
|
|
2020-01-09 01:17:10 +00:00
|
|
|
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';
|
|
|
|
|
2020-01-16 08:22:27 +00:00
|
|
|
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();
|
2020-01-16 02:13:42 +00:00
|
|
|
const [error, setError] = useState([]);
|
|
|
|
|
2020-01-16 08:22:27 +00:00
|
|
|
const [ state, dispatch ] = useReducer(
|
|
|
|
stateReducer,
|
|
|
|
{},
|
|
|
|
initState
|
|
|
|
);
|
|
|
|
|
2020-01-16 02:13:42 +00:00
|
|
|
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 (
|
2020-01-16 02:13:42 +00:00
|
|
|
<Router>
|
2020-01-09 01:17:10 +00:00
|
|
|
|
2020-01-16 02:13:42 +00:00
|
|
|
<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>): <></>}
|
2020-01-16 08:22:27 +00:00
|
|
|
|
2020-01-16 02:13:42 +00:00
|
|
|
<Switch>
|
|
|
|
|
|
|
|
<Route path="/account">
|
2020-01-16 08:22:27 +00:00
|
|
|
<Sidebar page="account" state={state} dispatch={dispatch}/>
|
|
|
|
<Account state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/rooms">
|
2020-01-16 08:22:27 +00:00
|
|
|
<Sidebar page="rooms"/>
|
|
|
|
<Room state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/games">
|
2020-01-16 08:22:27 +00:00
|
|
|
<Sidebar page="games"/>
|
|
|
|
<Game state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/news">
|
2020-01-16 08:22:27 +00:00
|
|
|
<Sidebar page="news"/>
|
|
|
|
<News state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/">
|
2020-01-16 08:22:27 +00:00
|
|
|
<Sidebar page="home" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
{/* Add ternary for login */}
|
2020-01-16 08:22:27 +00:00
|
|
|
<Home 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>
|
|
|
|
</div>
|
|
|
|
</Router>
|
2020-01-07 20:19:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|