Skip to content

Commit

Permalink
送信時の挙動を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
oka4shi committed Apr 25, 2024
1 parent 4e96d7c commit 417df3b
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/common_components/form_answer/Form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,21 @@ interface Props {
}

export const Form = ({ form, answerId, answerItems, editable }: Props) => {
const handleBeforeunload = useCallback(
(event: BeforeUnloadEvent) => {
const beforeUnloadController = new AbortController();

useEffect(() => {
const handleBeforeunload = (event: BeforeUnloadEvent) => {
if (editable || !answerItems) {
event.preventDefault();
event.returnValue = "";
}
},
[answerItems, editable],
);
useEffect(() => {
window.addEventListener("beforeunload", handleBeforeunload);
};
window.addEventListener("beforeunload", handleBeforeunload, { signal: beforeUnloadController.signal });

return () => {
window.removeEventListener("beforeunload", handleBeforeunload);
};
}, [handleBeforeunload]);
}, [editable, answerItems, beforeUnloadController.signal]);
const onSubmit: SubmitHandler<FormFieldsType> = async (data) => {
if (Array.from(fileErrors).some((v) => v[1])) {
toast.error(`正しいファイルをアップロードしてください`);
Expand Down Expand Up @@ -104,7 +103,7 @@ export const Form = ({ form, answerId, answerItems, editable }: Props) => {
await deleteAllUploadedFiles(fileIds);
throw new Error(error.message);
}
window.removeEventListener("beforeunload", handleBeforeunload);
beforeUnloadController.abort();
window.location.reload();
})
.catch(async () => {
Expand Down Expand Up @@ -132,7 +131,7 @@ export const Form = ({ form, answerId, answerItems, editable }: Props) => {
return;
}
toast.success("申請の送信に成功しました");
window.removeEventListener("beforeunload", handleBeforeunload);
beforeUnloadController.abort();
window.location.reload();
})
.catch(async () => {
Expand Down

0 comments on commit 417df3b

Please sign in to comment.