Skip to content

Commit

Permalink
make confirmation before flashcard result saved
Browse files Browse the repository at this point in the history
  • Loading branch information
chakkun1121 authored Feb 1, 2024
1 parent 5d7cd23 commit 09cb850
Show file tree
Hide file tree
Showing 4 changed files with 239 additions and 18 deletions.
24 changes: 24 additions & 0 deletions app/(app)/_library/useLeavePageConfirmation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { useEffect } from "react";
import Router from "next/router";
import { useBeforeUnload } from "react-use";

export const useLeavePageConfirmation = (
showAlert = true,
message?: string
) => {
useBeforeUnload(showAlert, message);

useEffect(() => {
const handler = () => {
if (showAlert && !window.confirm(message)) {
throw "cancel";
}
};

Router.events.on("routeChangeStart", handler);

return () => {
Router.events.off("routeChangeStart", handler);
};
}, [showAlert, message]);
};
45 changes: 27 additions & 18 deletions app/(app)/flashCard/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import CardResult from "./cardResult";
import HeaderRight from "./HeaderRight";
import { cardResult } from "@/@types/cardResult";
import { useDocumentTitle } from "@uidotdev/usehooks";
import { useLeavePageConfirmation } from "../_library/useLeavePageConfirmation";

export default function Card({ fileId }: { fileId: string }) {
const [fileContent, setFileContent] = useState<fileType | undefined>();
Expand All @@ -32,6 +33,8 @@ export default function Card({ fileId }: { fileId: string }) {
},
results: [],
});
const [savingResults, setSavingResults] = useState(false);
useLeavePageConfirmation(savingResults);
const { data: session }: { data: customSession | null } =
useSession() as unknown as { data: customSession };
const [flashCardSettings, setFlashCardSettings] = useState<flashCardSettings>(
Expand Down Expand Up @@ -75,24 +78,30 @@ export default function Card({ fileId }: { fileId: string }) {
useEffect(() => {
(async () => {
if (mode !== "result") return;
if (resultFileID) {
uploadFile(token, resultFileID, JSON.stringify(results));
} else {
setResultFileID(
await fetch("https://www.googleapis.com/drive/v3/files", {
method: "POST",
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: fileId + ".json",
parents: ["appDataFolder"],
}),
})
.then((r) => r.json())
.then((r) => r.id)
);
setSavingResults(true);
try {
if (resultFileID) {
await uploadFile(token, resultFileID, JSON.stringify(results));
} else {
setResultFileID(
await fetch("https://www.googleapis.com/drive/v3/files", {
method: "POST",
headers: {
Authorization: "Bearer " + token,
"Content-Type": "application/json",
},
body: JSON.stringify({
name: fileId + ".json",
parents: ["appDataFolder"],
}),
})
.then((r) => r.json())
.then((r) => r.id)
);
}
setSavingResults(false);
} catch (e) {
console.error(e);
}
})();
}, [fileId, mode, resultFileID, results, token]);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"react-hotkeys-hook": "^4.4.1",
"react-icons": "^4.12.0",
"react-swipeable": "^7.0.1",
"react-use": "^17.5.0",
"recoil": "^0.7.7",
"recoil-persist": "^5.1.0",
"remark-gfm": "^4.0.0",
Expand Down
Loading

0 comments on commit 09cb850

Please sign in to comment.