Newer
Older
import { startTransition } from "react";
import { Button } from "./ui/button";

DESKTOP-9FO96TP\hehexd
committed
import { User } from "@prisma/client";

DESKTOP-9FO96TP\hehexd
committed
export default function AddGameToFinishedList(props: { gameId: string, user: User }) {

DESKTOP-9FO96TP\hehexd
committed
const gameId = parseFloat(props.gameId);
const user = props.user;
let formData: {
id: String;
gameId: Number;
add: boolean;
planningGameList: number[] | undefined;
playingGameList: number[] | undefined;
finishedGameList: number[] | undefined;
} = {
id: "",
gameId: -1,
add: true,
planningGameList: undefined,
playingGameList: undefined,
finishedGameList: undefined
};

DESKTOP-9FO96TP\hehexd
committed
formData.id = user.id;
formData.finishedGameList = props.user.finishedGameList.filter((id) => id !== gameId)
console.log(formData.finishedGameList)
const response = await fetch('/api/gamelists', {
method: 'PUT',
body: JSON.stringify(formData)
})
startTransition(() => {
// Refresh the current route and fetch new data from the server without
// losing client-side browser or React state.
router.refresh();
});
return await response.json()
}
async function addGame(e: any) {
e.preventDefault()

DESKTOP-9FO96TP\hehexd
committed
formData.id = user.id;
props.user.finishedGameList.push(gameId)
formData.finishedGameList = props.user.finishedGameList;
const response = await fetch('/api/gamelists', {
method: 'PUT',
body: JSON.stringify(formData)
})
startTransition(() => {
// Refresh the current route and fetch new data from the server without
// losing client-side browser or React state.
router.refresh();
});
return await response.json()
}

DESKTOP-9FO96TP\hehexd
committed
if (!props.user.finishedGameList.includes(parseFloat(props.gameId))) {

DESKTOP-9FO96TP\hehexd
committed
Add Game To finished-playing-List
</form>
)
} else {
button = (
<form onSubmit={removeGame}>

DESKTOP-9FO96TP\hehexd
committed
Remove Game From finished-playing-List

DESKTOP-9FO96TP\hehexd
committed
throw new Error("Failed to check finished-playing-List");