Adds the ability to join or leave a gamenight.

This commit is contained in:
2022-05-31 21:27:35 +02:00
parent f7f9f7456b
commit b7f981e3a6
7 changed files with 171 additions and 22 deletions

View File

@@ -1,4 +1,4 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText';
@@ -6,15 +6,26 @@ import ListSubheader from '@mui/material/ListSubheader';
import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import Button from '@mui/material/Button';
import moment from 'moment';
import {unpack_api_result, get_gamenight, patch_gamenight} from '../api/Api';
function Gamenight(props) {
const [dense, setDense] = useState(true);
const [gamenight, setGamenight] = useState(null);
let games = props.gamenight.game_list.map(g =>
const fetchGamenight = () => {
if (props.user !== null) {
unpack_api_result(get_gamenight(props.gamenightId, props.user.jwt), props.setFlash)
.then(result => setGamenight(result.gamenight));
}
}
useEffect(fetchGamenight, []);
let games = gamenight?.game_list.map(g =>
(
<ListItem>
<ListItemText
@@ -24,7 +35,7 @@ function Gamenight(props) {
)
);
let participants = props.gamenight.participants.map(p =>
const participants = gamenight?.participants.map(p =>
(
<ListItem>
<ListItemText
@@ -32,7 +43,46 @@ function Gamenight(props) {
/>
</ListItem>
)
)
);
const Join = () => {
const input = {
action: 'AddParticipant'
};
unpack_api_result(patch_gamenight(gamenight.id, input, props.user.jwt), props.setFlash)
.then(() => fetchGamenight());
};
const Leave = () => {
const input = {
action: 'RemoveParticipant',
};
unpack_api_result(patch_gamenight(gamenight.id, input, props.user.jwt), props.setFlash)
.then(() => fetchGamenight());
};
let join_or_leave_button;
if(gamenight?.participants.find(p => p.id === props.user.id) === undefined) {
join_or_leave_button = (
<Button
variant="outlined"
color="success"
onClick={Join}>
Join
</Button>
)
} else {
join_or_leave_button = (
<Button
variant="outlined"
color="error"
onClick={Leave}>
Leave
</Button>
)
}
return (
<div>
@@ -43,10 +93,10 @@ function Gamenight(props) {
</IconButton>
<Typography type="h3">
{props.gamenight.name}
{gamenight?.name}
</Typography>
<Typography type="body1">
When: {moment(props.gamenight.datetime).format('LL HH:mm')}
When: {moment(gamenight?.datetime).format('LL HH:mm')}
</Typography>
<List
@@ -69,6 +119,7 @@ function Gamenight(props) {
}>
{participants}
</List>
{join_or_leave_button}
</div>
)
}