From a0d2247ff845fad5a0fc7d15283e6e2790eecddd Mon Sep 17 00:00:00 2001 From: Owen Date: Tue, 2 Jun 2026 15:06:56 +0200 Subject: [PATCH] rudimentary pdf saving --- src/hooks/useChessGame.ts | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/hooks/useChessGame.ts b/src/hooks/useChessGame.ts index d8a43a9..980680d 100644 --- a/src/hooks/useChessGame.ts +++ b/src/hooks/useChessGame.ts @@ -1,5 +1,6 @@ import { type ChangeEvent, useReducer, useRef, useState } from "react"; import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts"; +import { downloadPDF } from "#/utils/pdfUtils.ts"; import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts"; import { downloadString } from "#/utils/stringUtils.ts"; @@ -27,8 +28,42 @@ export const useChessGame = () => { downloadString(pgnString, "game.pgn"); }; - const handleSavePdf = () => { - console.log("Save PDF"); + const handleSavePdf = async () => { + console.log("Save PDF", gameState.diagrams, gameState.diagramClock); + + try { + const { diagrams, diagramClock } = gameState; + const pgnString = buildPgnString(gameState); + + const apiBaseUrl = import.meta.env.VITE_API_BASE_URL; + + const response = await fetch(`${apiBaseUrl}/pdf`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + pgn: pgnString, + diagrams, + diagramClock, + }), + }); + + if (response.ok) { + downloadPDF(await response.blob()); + // TODO toast + } else { + const body = await response.json(); + // TODO toast and remove console but pass to logger + console.error("Error saving PDF:", body.error); + } + } catch (error: unknown) { + console.error("Error saving PDF:", error); + + if (error instanceof Error) { + // do something here such as toast + } + } }; const handleClearGame = () => {