Skip to content

Commit

Permalink
Add error handling for questionnaire submission and update loading st…
Browse files Browse the repository at this point in the history
…ate messages
  • Loading branch information
abhimanyurajeesh committed Jan 8, 2025
1 parent 0a14340 commit 51197b7
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 15 deletions.
1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,7 @@
"questionnaire": "Questionnaire",
"questionnaire_error_loading": "Error loading questionnaire",
"questionnaire_not_exist": "The questionnaire you tried to access does not exist.",
"questionnaire_submission_failed": "Failed to submit questionnaire",
"questionnaire_submitted_successfully": "Questionnaire submitted successfully",
"raise_consent_request": "Raise a consent request to fetch patient records over ABDM",
"ration_card__APL": "APL",
Expand Down
31 changes: 16 additions & 15 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import Loading from "@/components/Common/Loading";

import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import { HTTPError } from "@/Utils/request/types";
import useQuery from "@/Utils/request/useQuery";
import {
DetailedValidationError,
Expand Down Expand Up @@ -78,17 +79,18 @@ export function QuestionnaireForm({
prefetch: !!questionnaireSlug && !FIXED_QUESTIONNAIRES[questionnaireSlug],
});

const { mutate: submitBatch, isPending: isProcessing } = useMutation({
mutationFn: mutate(routes.batchRequest),
const { mutate: submitBatch, isPending } = useMutation({
mutationFn: mutate(routes.batchRequest, { silent: true }),
onSuccess: () => {
toast.success(t("questionnaire_submitted_successfully"));
onSubmit?.();
},
onError: (error: any) => {
if (error.results) {
handleSubmissionError(error.results as ValidationErrorResponse[]);
onError: (error: HTTPError) => {
const errorData = error.cause;
if (errorData?.results) {
handleSubmissionError(errorData.results as ValidationErrorResponse[]);
}
toast.error(t("questionnaire_error_loading"));
toast.error(t("questionnaire_submission_failed"));
},
});

Expand Down Expand Up @@ -243,7 +245,6 @@ export function QuestionnaireForm({
}
});

// Execute the mutation
submitBatch({ requests });
};

Expand All @@ -260,7 +261,7 @@ export function QuestionnaireForm({
"bg-gray-100 text-green-600",
)}
onClick={() => setActiveQuestionnaireId(form.questionnaire.id)}
disabled={isProcessing}
disabled={isPending}
>
{form.questionnaire.title}
</button>
Expand All @@ -279,7 +280,7 @@ export function QuestionnaireForm({
setActiveQuestionnaireId(form.questionnaire.id);
setActiveGroupId(group.id);
}}
disabled={isProcessing}
disabled={isPending}
>
{group.text}
</button>
Expand Down Expand Up @@ -321,7 +322,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
>
<CareIcon icon="l-times-circle" />
<span>Remove</span>
Expand All @@ -343,7 +344,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
activeGroupId={activeGroupId}
errors={form.errors}
patientId={patientId}
Expand Down Expand Up @@ -388,7 +389,7 @@ export function QuestionnaireForm({
},
]);
}}
disabled={isProcessing}
disabled={isPending}
/>
</div>

Expand All @@ -399,17 +400,17 @@ export function QuestionnaireForm({
type="button"
variant="outline"
onClick={onCancel}
disabled={isProcessing}
disabled={isPending}
>
{t("cancel")}
</Button>
<Button
type="button"
onClick={handleSubmit}
disabled={isProcessing || hasErrors}
disabled={isPending || hasErrors}
className="relative"
>
{isProcessing ? (
{isPending ? (
<>
<span className="opacity-0">{t("submit")}</span>
<div className="absolute inset-0 flex items-center justify-center">
Expand Down

0 comments on commit 51197b7

Please sign in to comment.