Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confirm Dialog For Navigation on Edited Questionnaire Form #9838

Merged
13 changes: 13 additions & 0 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMutation } from "@tanstack/react-query";
import { t } from "i18next";
import { useNavigationPrompt } from "raviger";
import { useEffect, useState } from "react";
import { toast } from "sonner";

Expand Down Expand Up @@ -63,6 +64,7 @@ export function QuestionnaireForm({
onCancel,
facilityId,
}: QuestionnaireFormProps) {
const [isDirty, setIsDirty] = useState(false);
const [questionnaireForms, setQuestionnaireForms] = useState<
QuestionnaireFormState[]
>([]);
Expand All @@ -83,6 +85,7 @@ export function QuestionnaireForm({
mutationFn: mutate(routes.batchRequest, { silent: true }),
onSuccess: () => {
toast.success(t("questionnaire_submitted_successfully"));
setIsDirty(false);
onSubmit?.();
},
onError: (error) => {
Expand All @@ -94,6 +97,13 @@ export function QuestionnaireForm({
},
});

// TODO: Use useBlocker hook after switching to tanstack router
// https://tanstack.com/router/latest/docs/framework/react/guide/navigation-blocking#how-do-i-use-navigation-blocking
useNavigationPrompt(
isDirty,
"You have unsaved changes. Are you sure you want to leave?",
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
);
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
if (!isInitialized && questionnaireSlug) {
const questionnaire =
Expand Down Expand Up @@ -348,6 +358,9 @@ export function QuestionnaireForm({
: formItem,
),
);
if (!isDirty) {
setIsDirty(true);
}
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved
}}
disabled={isPending}
activeGroupId={activeGroupId}
Expand Down
Loading