Adds a Apihelper to cleanup the react components.

This commit is contained in:
2022-05-31 19:56:44 +02:00
parent cfaa6ebdb1
commit f7f9f7456b
5 changed files with 169 additions and 97 deletions

View File

@@ -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) {

View File

@@ -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());
}
}