107 lines
2.5 KiB
JavaScript
107 lines
2.5 KiB
JavaScript
import logo from './logo.svg';
|
|
import './App.css';
|
|
import React, { useState, useEffect } from 'react';
|
|
import MenuBar from './components/MenuBar';
|
|
import Login from './components/Login';
|
|
import Gamenights from './components/Gamenights'
|
|
import AddGameNight from './components/AddGameNight'
|
|
|
|
function App() {
|
|
|
|
const [user, setUser] = useState(null);
|
|
const [token, setToken] = useState(null);
|
|
const [gamenights, setGamenights] = useState([]);
|
|
const [flashData, setFlashData] = useState({});
|
|
|
|
const handleLogin = (input) => {
|
|
const requestOptions = {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(input)
|
|
};
|
|
fetch('api/login', requestOptions)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if(data.result === "Ok") {
|
|
setToken(data.jwt);
|
|
} else {
|
|
setFlashData({
|
|
type: "Error",
|
|
message: data.message
|
|
});
|
|
}
|
|
});
|
|
};
|
|
|
|
const handleAddGameNight = (input) => {
|
|
if (token !== null) {
|
|
const requestOptions = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'Authorization': `Bearer ${token}`
|
|
},
|
|
body: JSON.stringify(input)
|
|
};
|
|
|
|
fetch('api/gamenight', requestOptions)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if(data.result !== "Ok") {
|
|
setFlashData({
|
|
type: "Error",
|
|
message: data.message
|
|
});
|
|
} else {
|
|
setToken(token);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
const onLogout = () => {
|
|
setUser(null);
|
|
setToken(null);
|
|
}
|
|
|
|
useEffect(() => {
|
|
if (token !== null) {
|
|
const requestOptions = {
|
|
method: 'GET',
|
|
headers: { 'Authorization': `Bearer ${token}` },
|
|
};
|
|
fetch('api/gamenights', requestOptions)
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if(data.result === "Ok") {
|
|
setGamenights(data.gamenights)
|
|
} else {
|
|
setFlashData({
|
|
type: "Error",
|
|
message: data.message
|
|
});
|
|
}
|
|
});
|
|
}
|
|
});
|
|
|
|
if(token === null) {
|
|
return (
|
|
<div className="App">
|
|
<Login onChange={handleLogin}/>
|
|
</div>
|
|
);
|
|
} else {
|
|
return (
|
|
<>
|
|
<MenuBar user={user} onLogout={onLogout} />
|
|
<AddGameNight onChange={handleAddGameNight} />
|
|
<Gamenights gamenights={gamenights} />
|
|
</>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default App;
|