Skip to content
Snippets Groups Projects
addGameToList.tsx 2.19 KiB
Newer Older
Caner's avatar
Caner committed
"use client"

import { Post, Prisma } from "@prisma/client";
import { useRouter } from "next/navigation";
import { startTransition, useEffect, useState } from "react";

DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
export default function AddGameToList(props: { userGameList: Number[], gameId: string }) {
Caner's avatar
Caner committed

    const router = useRouter();
    const gameId = props.gameId
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
    let formData = { gameId: "", add: true }
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.gameId = gameId;
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
        formData.add = false;
        const response = await fetch('http://localhost:3000/api/gameList', {
            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.
            router.refresh();
        });
        return await response.json()
    }

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

        formData.gameId = gameId;
        formData.add = true;
Caner's avatar
Caner committed
        const response = await fetch('http://localhost:3000/api/gameList', {
            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's avatar
DESKTOP-9FO96TP\hehexd committed

    let button = <div></div>;
    try {
        if (!props.userGameList.includes(parseFloat(props.gameId))) {

            button = (
                <form onSubmit={addGame}>
                    <button type="submit" className="mt-2 bg-gray-300 text-gray-800 px-4 py-2 rounded float-right">
                        AddGame
                    </button>
                </form>
            )
        } else {
            button = (
                <form onSubmit={removeGame}>
                    <button type="submit" className="mt-2 bg-gray-300 text-gray-800 px-4 py-2 rounded float-right">
                        Remove Game
                    </button>
                </form>
            )
        }
    } catch (error) {

    }

Caner's avatar
Caner committed
    return (
DESKTOP-9FO96TP\hehexd's avatar
DESKTOP-9FO96TP\hehexd committed
        button
Caner's avatar
Caner committed
    )