forked from Roflin/gamenight
Adds a Apihelper to cleanup the react components.
This commit is contained in:
@@ -9,6 +9,7 @@ import { AdapterDateFns } from '@mui/x-date-pickers/AdapterDateFns';
|
||||
import { LocalizationProvider } from '@mui/x-date-pickers/LocalizationProvider';
|
||||
|
||||
import GameAdder from './GameAdder';
|
||||
import { post_gamenight, unpack_api_result} from '../api/Api';
|
||||
|
||||
function AddGameNight(props) {
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
@@ -44,32 +45,14 @@ function AddGameNight(props) {
|
||||
game_list: gameList,
|
||||
}
|
||||
|
||||
const requestOptions = {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${props.user.jwt}`
|
||||
},
|
||||
body: JSON.stringify(input)
|
||||
};
|
||||
|
||||
fetch('api/gamenights', requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if(data.result !== "Ok") {
|
||||
props.setFlash({
|
||||
type: "Error",
|
||||
message: data.message
|
||||
});
|
||||
} else {
|
||||
setExpanded(false);
|
||||
setGameName("");
|
||||
setDate(null);
|
||||
}
|
||||
unpack_api_result(post_gamenight(input, props.user.jwt), props.setFlash)
|
||||
.then(result => {
|
||||
setExpanded(false);
|
||||
setGameName("");
|
||||
setDate(null);
|
||||
})
|
||||
.then(() => props.refetchGamenights())
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
if(expanded) {
|
||||
|
||||
@@ -9,35 +9,15 @@ import GamesIcon from '@mui/icons-material/Games';
|
||||
import DeleteIcon from '@mui/icons-material/Delete';
|
||||
|
||||
import AddGameNight from './AddGameNight';
|
||||
import {delete_gamenight, unpack_api_result} from '../api/Api';
|
||||
|
||||
function Gamenights(props) {
|
||||
const [dense, setDense] = React.useState(false);
|
||||
|
||||
const DeleteGamenight = (gameId) => {
|
||||
const DeleteGamenight = (game_id) => {
|
||||
if (props.user !== null) {
|
||||
let input = {
|
||||
game_id: gameId,
|
||||
}
|
||||
|
||||
const requestOptions = {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${props.user.jwt}`
|
||||
},
|
||||
body: JSON.stringify(input)
|
||||
};
|
||||
|
||||
fetch('api/gamenights', requestOptions)
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if(data.result !== "Ok") {
|
||||
props.setFlash({
|
||||
type: "Error",
|
||||
message: data.message
|
||||
});
|
||||
}
|
||||
})
|
||||
const input = { game_id: game_id };
|
||||
unpack_api_result(delete_gamenight(input, props.user.jwt), props.setFlash)
|
||||
.then(() => props.refetchGamenights());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user