Skip to content
Snippets Groups Projects
add-game-to-playing-list.tsx 2.67 KiB
Newer Older
Caner's avatar
Caner committed
"use client"

import { User } from "@prisma/client"
import { useRouter } from "next/navigation"
import { startTransition } from "react"
import { Button } from "./ui/button"
Caner's avatar
Caner committed

export default function AddGameToPlayingList(props: { gameId: string, user: User }) {
Caner's avatar
Caner committed

    const router = useRouter()
    const gameId = parseFloat(props.gameId)
    const user = props.user
        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
Caner's avatar
Caner committed

DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
    async function removeGame(e: any) {
Caner's avatar
Caner committed
        e.preventDefault()

        formData.playingGameList = props.user.playingGameList.filter((id) => id !== gameId)
        const response = await fetch('/api/users/gamelists', {
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
            method: 'PUT',
            body: JSON.stringify(formData)
        })
Caner's avatar
Caner committed

DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
        startTransition(() => {
            // Refresh the current route and fetch new data from the server without
            // losing client-side browser or React state.
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
        return await response.json()
    }

    async function addGame(e: any) {
        e.preventDefault()

        formData.playingGameList = props.user.playingGameList
        const response = await fetch('/api/users/gamelists', {
Caner's avatar
Caner committed
            method: 'PUT',
            body: JSON.stringify(formData)
        })
Caner's avatar
Caner committed
        startTransition(() => {
            // Refresh the current route and fetch new data from the server without
            // losing client-side browser or React state.
Caner's avatar
Caner committed
        return await response.json()
    }

DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
    try {
        if (!props.user.playingGameList.includes(parseFloat(props.gameId))) {
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
            button = (
                <form onSubmit={addGame}>
                    <Button type="submit" size="lg" variant={"ghost"}>
Yusuf Akgül's avatar
Yusuf Akgül committed
                        Set as Playing
Yusuf Akgül's avatar
Yusuf Akgül committed
                    </Button>
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
                </form>
            )
        } else {
            button = (
                <form onSubmit={removeGame}>
                    <Button type="submit" size="lg" variant={"ghost"}>
Yusuf Akgül's avatar
Yusuf Akgül committed
                        Unset Playing
Yusuf Akgül's avatar
Yusuf Akgül committed
                    </Button>
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
                </form>
            )
        }
    } catch (error) {
Yusuf Akgül's avatar
Yusuf Akgül committed
        // throw new Error("Failed to check playing-to-play-List");
Caner's avatar
Caner committed
    return (
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
        button
Caner's avatar
Caner committed
    )