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
1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,7 @@
"unlink_skill_error": "Error while unlinking skill. Try again later.",
"unlink_skill_success": "Skill unlinked successfully",
"unlink_this_facility": "Unlink Facility",
"unsaved_changes": "You have unsaved changes. Are you sure you want to leave?",
"unsubscribe": "Unsubscribe",
"unsubscribe_failed": "Unsubscribe failed.",
"unsubscribed_successfully": "Unsubscribed Successfully.",
Expand Down
10 changes: 10 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,10 @@ 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, t("questionnaire_unsaved_changes"));
rithviknishad marked this conversation as resolved.
Show resolved Hide resolved

useEffect(() => {
if (!isInitialized && questionnaireSlug) {
const questionnaire =
Expand Down Expand Up @@ -348,6 +355,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