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
|
|
|
|
|
|
|
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-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([]);
|
|
|
|
|
|
|
|
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>): <></>}
|
|
|
|
<Switch>
|
|
|
|
|
|
|
|
<Route path="/account">
|
|
|
|
<Account />
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/rooms">
|
|
|
|
<Room />
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/games">
|
|
|
|
<Game />
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/news">
|
|
|
|
<News />
|
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/">
|
|
|
|
{/* Add ternary for login */}
|
|
|
|
<Home />
|
|
|
|
</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;
|