fix : save eval on game save

This commit is contained in:
GuillaumeSD
2024-02-23 04:06:07 +01:00
parent 814d3ecf09
commit f77a425067

View File

@@ -3,33 +3,38 @@ import { Icon } from "@iconify/react";
import { IconButton } from "@mui/material"; import { IconButton } from "@mui/material";
import { useAtomValue } from "jotai"; import { useAtomValue } from "jotai";
import { useRouter } from "next/router"; import { useRouter } from "next/router";
import { gameAtom } from "../states"; import { gameAtom, gameEvalAtom } from "../states";
export default function SaveButton() { export default function SaveButton() {
const game = useAtomValue(gameAtom); const game = useAtomValue(gameAtom);
const { addGame } = useGameDatabase(); const gameEval = useAtomValue(gameEvalAtom);
const { addGame, setGameEval } = useGameDatabase();
const router = useRouter(); const router = useRouter();
const { gameId } = router.query; const { gameId } = router.query;
const isButtonEnabled = router.isReady && typeof gameId === undefined; const isButtonEnabled = router.isReady && typeof gameId === undefined;
const handleSave = async () => {
if (!isButtonEnabled) return;
const gameId = await addGame(game);
if (gameEval) {
await setGameEval(gameId, gameEval);
}
router.replace(
{
query: { gameId: gameId },
pathname: router.pathname,
},
undefined,
{ shallow: true, scroll: false }
);
};
return ( return (
<IconButton <IconButton onClick={handleSave} disabled={!isButtonEnabled}>
onClick={async () => {
if (!isButtonEnabled) return;
const gameId = await addGame(game);
router.replace(
{
query: { gameId: gameId },
pathname: router.pathname,
},
undefined,
{ shallow: true, scroll: false }
);
}}
disabled={!isButtonEnabled}
>
<Icon icon="ri:save-3-line" /> <Icon icon="ri:save-3-line" />
</IconButton> </IconButton>
); );