diff --git a/src/app/_components/game/configurator/game-config-form.tsx b/src/app/_components/game/configurator/game-config-form.tsx index 37a1089..786a239 100644 --- a/src/app/_components/game/configurator/game-config-form.tsx +++ b/src/app/_components/game/configurator/game-config-form.tsx @@ -3,56 +3,120 @@ import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; - -import { Button } from "@/components/ui/button"; import { Form, FormControl, FormField, FormItem, FormLabel, + FormMessage, } from "@/components/ui/form"; import { gameConfigPatchSchema } from "@/lib/validations/game"; import { Switch } from "@/components/ui/switch"; +import { useGameStore } from "@/lib/store/game-store"; +import { api } from "@/trpc/react"; +import { useLobbyStore } from "@/lib/store/lobby-store"; +import { Slider } from "@/components/ui/slider"; +import { getMinMax } from "@/lib/validations/utils"; +import { debounce } from "@/lib/utils"; +import React from "react"; +import { useSessionPlayerStore } from "@/lib/store/current-player-store"; + +const formSchema = gameConfigPatchSchema.pick({ + allowHints: true, + // scoreMultiplier: true, + timeLimit: true, +}); export default function GameConfigForm() { - // ... - const form = useForm>({ - resolver: zodResolver(gameConfigPatchSchema), + const updateGameConfig = api.gameConfig.update.useMutation(); + const gameConfig = useGameStore((state) => state.gameConfig); + const lobbyId = useLobbyStore((state) => state.lobby.id); + + const form = useForm>({ + resolver: zodResolver(formSchema), defaultValues: { - allowHints: false, - scoreMultiplier: 1, - timeLimit: 2, + allowHints: gameConfig.allowHints, + timeLimit: gameConfig.timeLimit, }, }); - // 2. Define a submit handler. - function onSubmit(values: z.infer) { - // Do something with the form values. - // ✅ This will be type-safe and validated. - console.log(values); + function onSubmit(values: z.infer) { + updateGameConfig.mutate({ + lobbyId, + config: { + ...gameConfig, + ...values, + }, + }); } + const save = form.handleSubmit(onSubmit); + React.useEffect(() => { + form.reset({ + allowHints: gameConfig.allowHints, + timeLimit: gameConfig.timeLimit, + }); + }, [gameConfig]); + const isLobbyAdmin = useSessionPlayerStore((state) => state.isLobbyAdmin); + return (
- + ( - -
- Allow hints -
+ + Allow hints { + field.onChange(checked); + save(); + }} /> )} /> - {/* */} + { + const { min, max } = getMinMax(formSchema.shape.timeLimit); + + return ( + +
+ + Time Limit + + {`${field.value}`} min + + + {isLobbyAdmin && ( + + { + field.onChange(value); + debounce(save, 1000)(); + }} + /> + + )} +
+ +
+ ); + }} + /> ); diff --git a/src/app/_components/game/configurator/game-configurator.tsx b/src/app/_components/game/configurator/game-configurator.tsx index 83b804e..816a409 100644 --- a/src/app/_components/game/configurator/game-configurator.tsx +++ b/src/app/_components/game/configurator/game-configurator.tsx @@ -7,14 +7,16 @@ import GameConfigForm from "./game-config-form"; import GameVariantSelector from "./game-variant-selector"; import { api } from "@/trpc/react"; import { useLobbyStore } from "@/lib/store/lobby-store"; +import { isCallOrNewExpression } from "typescript"; +import { useSessionPlayerStore } from "@/lib/store/current-player-store"; function GameConfigurator() { const updateGameConfig = api.gameConfig.update.useMutation(); const selectGame = useGameStore((state) => state.setSelectedGame); const selectedGame = useGameStore((state) => state.selectedGame); - const setGameConfig = useGameStore((state) => state.setGameConfig); const gameConfig = useGameStore((state) => state.gameConfig); const lobbyId = useLobbyStore((state) => state.lobby.id); + const isLobbyAdmin = useSessionPlayerStore((state) => state.isLobbyAdmin); const handleUnselectGame = () => { selectGame(undefined); updateGameConfig.mutate({ @@ -26,9 +28,19 @@ function GameConfigurator() { }); }; + const handleVariantSelect = (gameVariantId?: string) => { + updateGameConfig.mutate({ + lobbyId, + config: { + ...gameConfig, + gameVariantId, + }, + }); + }; + return ( -
-
+
+

{selectedGame?.name ?? "Select a game to get started"} @@ -42,14 +54,14 @@ function GameConfigurator() { {selectedGame && ( <> - + )}

{selectedGame && ( -
+