rudimentary pdf saving

This commit is contained in:
2026-06-02 15:06:56 +02:00
parent 95bef61a29
commit a0d2247ff8
+37 -2
View File
@@ -1,5 +1,6 @@
import { type ChangeEvent, useReducer, useRef, useState } from "react"; import { type ChangeEvent, useReducer, useRef, useState } from "react";
import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts"; import { gameReducer, initialGameState } from "#/reducers/gameReducer.ts";
import { downloadPDF } from "#/utils/pdfUtils.ts";
import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts"; import { buildPgnString, getHeaders } from "#/utils/pgnUtils.ts";
import { downloadString } from "#/utils/stringUtils.ts"; import { downloadString } from "#/utils/stringUtils.ts";
@@ -27,8 +28,42 @@ export const useChessGame = () => {
downloadString(pgnString, "game.pgn"); downloadString(pgnString, "game.pgn");
}; };
const handleSavePdf = () => { const handleSavePdf = async () => {
console.log("Save PDF"); 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 = () => { const handleClearGame = () => {