Skip to content

Commit

Permalink
Replaced deprecated useMutation in questionnaire with tanstack's useM…
Browse files Browse the repository at this point in the history
…utation (ohcnetwork#9840)
  • Loading branch information
abhimanyurajeesh authored Jan 10, 2025
1 parent 96b37f0 commit e0062a0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 82 deletions.
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,8 @@
"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",
"ration_card__BPL": "BPL",
Expand Down
50 changes: 0 additions & 50 deletions src/Utils/request/useMutation.ts

This file was deleted.

60 changes: 28 additions & 32 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useMutation } from "@tanstack/react-query";
import { t } from "i18next";
import { useEffect, useState } from "react";
import { toast } from "sonner";
Expand All @@ -13,7 +14,7 @@ import Loading from "@/components/Common/Loading";

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

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

useEffect(() => {
if (!isInitialized && questionnaireSlug) {
Expand Down Expand Up @@ -200,9 +211,9 @@ export function QuestionnaireForm({

// Then, add questionnaire submission requests
questionnaireForms.forEach((form) => {
const nonStructuredResponses = form.responses.filter((response) => {
return !response.structured_type;
});
const nonStructuredResponses = form.responses.filter(
(response) => !response.structured_type,
);

if (nonStructuredResponses.length > 0) {
requests.push({
Expand Down Expand Up @@ -234,22 +245,7 @@ export function QuestionnaireForm({
}
});

const response = await submitBatch({
body: { requests },
});

if (!response.data) {
if (response.error) {
handleSubmissionError(
response.error.results as ValidationErrorResponse[],
);
toast.error("Failed to submit questionnaire");
}
return;
}

toast.success("Questionnaire submitted successfully");
onSubmit?.();
submitBatch({ requests });
};

return (
Expand All @@ -265,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 @@ -284,7 +280,7 @@ export function QuestionnaireForm({
setActiveQuestionnaireId(form.questionnaire.id);
setActiveGroupId(group.id);
}}
disabled={isProcessing}
disabled={isPending}
>
{group.text}
</button>
Expand Down Expand Up @@ -331,7 +327,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
>
<CareIcon icon="l-times-circle" />
<span>Remove</span>
Expand All @@ -353,7 +349,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
activeGroupId={activeGroupId}
errors={form.errors}
patientId={patientId}
Expand Down Expand Up @@ -398,7 +394,7 @@ export function QuestionnaireForm({
},
]);
}}
disabled={isProcessing}
disabled={isPending}
/>
</div>

Expand All @@ -409,17 +405,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 e0062a0

Please sign in to comment.