Skip to content

Commit

Permalink
Confirm Dialog For Navigation on Edited Questionnaire Form (ohcnetwor…
Browse files Browse the repository at this point in the history
…k#9838)

* added logic for preventing navigation on dirty form

* switch to using raviger's hook

* reset isdirty after submission

* i18n

* Update src/components/Questionnaire/QuestionnaireForm.tsx

* reset isDirty on form submit

* rm setIsDirty in mutate

---------

Co-authored-by: Rithvik Nishad <[email protected]>
  • Loading branch information
Jacobjeevan and rithviknishad authored Jan 14, 2025
1 parent bc152cc commit b749918
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -2046,6 +2046,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 Down Expand Up @@ -94,6 +96,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("unsaved_changes"));

useEffect(() => {
if (!isInitialized && questionnaireSlug) {
const questionnaire =
Expand Down Expand Up @@ -186,6 +192,7 @@ export function QuestionnaireForm({
const hasErrors = questionnaireForms.some((form) => form.errors.length > 0);

const handleSubmit = async () => {
setIsDirty(false);
if (hasErrors) return;

const requests: BatchRequest[] = [];
Expand Down Expand Up @@ -348,6 +355,9 @@ export function QuestionnaireForm({
: formItem,
),
);
if (!isDirty) {
setIsDirty(true);
}
}}
disabled={isPending}
activeGroupId={activeGroupId}
Expand Down

0 comments on commit b749918

Please sign in to comment.