mirror of
https://github.com/TheRealOwenRees/chess-scribe-website.git
synced 2026-07-23 09:56:57 +00:00
rudimentary pdf saving
This commit is contained in:
@@ -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 = () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user