Newer
Older

Yusuf Akgül
committed
import { User } from "@prisma/client"
import { useRouter } from "next/navigation"
import { startTransition } from "react"
import { Button } from "./ui/button"

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

Yusuf Akgül
committed
const router = useRouter()
const gameId = parseFloat(props.gameId)
const user = props.user

DESKTOP-9FO96TP\hehexd
committed
let formData: {

Yusuf Akgül
committed
id: String
gameId: Number
add: boolean
planningGameList: number[] | undefined
playingGameList: number[] | undefined
finishedGameList: number[] | undefined

DESKTOP-9FO96TP\hehexd
committed
} = {
id: "",
gameId: -1,
add: true,
planningGameList: undefined,
playingGameList: undefined,
finishedGameList: undefined

Yusuf Akgül
committed
}

Yusuf Akgül
committed
formData.id = user.id

DESKTOP-9FO96TP\hehexd
committed
formData.playingGameList = props.user.playingGameList.filter((id) => id !== gameId)
const response = await fetch('/api/users/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.

Yusuf Akgül
committed
router.refresh()
})
return await response.json()
}
async function addGame(e: any) {
e.preventDefault()

Yusuf Akgül
committed
formData.id = user.id

DESKTOP-9FO96TP\hehexd
committed
props.user.playingGameList.push(gameId)

Yusuf Akgül
committed
formData.playingGameList = props.user.playingGameList
const response = await fetch('/api/users/gamelists', {
startTransition(() => {
// Refresh the current route and fetch new data from the server without
// losing client-side browser or React state.

Yusuf Akgül
committed
router.refresh()
})

Yusuf Akgül
committed
let button = <div></div>

DESKTOP-9FO96TP\hehexd
committed
if (!props.user.playingGameList.includes(parseFloat(props.gameId))) {
<Button type="submit" size="lg" variant={"ghost"}>
</form>
)
} else {
button = (
<form onSubmit={removeGame}>
<Button type="submit" size="lg" variant={"ghost"}>