2020-01-16 02:13:42 +00:00
|
|
|
import React, {useState, useEffect, useReducer} from 'react';
|
2020-01-18 03:57:27 +00:00
|
|
|
import socketIOClient from 'socket.io-client';
|
2020-01-07 21:03:59 +00:00
|
|
|
import config from './config';
|
2020-01-20 21:22:18 +00:00
|
|
|
import { Switch, Route, BrowserRouter as Router, Redirect } from 'react-router-dom';
|
2020-01-18 21:02:03 +00:00
|
|
|
import MainWrapper from './pages/Layout/MainWrapper/MainWrapper';
|
2020-01-16 08:22:27 +00:00
|
|
|
import { stateReducer } from './reducers/stateReducer';
|
|
|
|
import { initState } from './reducers/init/stateReducer.init';
|
2020-01-18 03:57:27 +00:00
|
|
|
import indexServices from './services/api/indexServices';
|
|
|
|
import './App.scss';
|
2020-01-16 08:22:27 +00:00
|
|
|
|
2020-01-18 03:57:27 +00:00
|
|
|
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();
|
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-18 03:57:27 +00:00
|
|
|
const fetchIndexAPI = async () => {
|
|
|
|
const response = await indexServices.indexService();
|
2020-01-18 04:24:57 +00:00
|
|
|
if (response) {
|
2020-01-18 00:32:40 +00:00
|
|
|
const action = {
|
|
|
|
type: 'INDEX',
|
2020-01-18 03:57:27 +00:00
|
|
|
message: 'SET_USER',
|
2020-01-18 04:24:57 +00:00
|
|
|
body: response
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
dispatch(action)
|
2020-01-18 03:57:27 +00:00
|
|
|
}
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
|
2020-01-18 04:24:57 +00:00
|
|
|
useEffect(() => {
|
|
|
|
fetchIndexAPI();
|
|
|
|
}, [])
|
|
|
|
|
2020-01-18 00:32:40 +00:00
|
|
|
const socketConnect = () => {
|
2020-01-16 02:13:42 +00:00
|
|
|
socket.emit('connect');
|
2020-01-18 00:32:40 +00:00
|
|
|
socket.on('connected', data => setSocketData('socket connected'));
|
2020-01-16 02:13:42 +00:00
|
|
|
socket.on('connect_error', err => setError([...error, err]));
|
|
|
|
socket.on('error', err => setError([...error, err]))
|
2020-01-18 00:32:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
socketConnect();
|
2020-01-18 04:24:57 +00:00
|
|
|
}, [])
|
2020-01-17 20:21:43 +00:00
|
|
|
|
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 23:21:23 +00:00
|
|
|
<div data-testid="App" className="App">
|
2020-01-16 02:13:42 +00:00
|
|
|
<Switch>
|
|
|
|
|
|
|
|
<Route path="/account">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="account" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
2020-01-22 19:43:36 +00:00
|
|
|
<Route path="/rooms/:id">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="room" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/games">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="game" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
|
|
|
|
|
|
|
<Route path="/news">
|
2020-01-16 23:21:23 +00:00
|
|
|
<MainWrapper page="news" state={state} dispatch={dispatch}/>
|
2020-01-16 02:13:42 +00:00
|
|
|
</Route>
|
2020-01-20 21:22:18 +00:00
|
|
|
|
|
|
|
<Route path="/home">
|
|
|
|
<MainWrapper page="home" state={state} dispatch={dispatch}/>
|
|
|
|
</Route>
|
2020-01-16 02:13:42 +00:00
|
|
|
|
|
|
|
<Route path="/">
|
2020-01-20 21:22:18 +00:00
|
|
|
{state.user.username
|
|
|
|
? <MainWrapper page="home" state={state} dispatch={dispatch}/>
|
|
|
|
: <MainWrapper page="news" 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>
|
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>): <></>} */}
|
2020-01-16 02:13:42 +00:00
|
|
|
</div>
|
|
|
|
</Router>
|
2020-01-07 20:19:11 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default App;
|