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

Replaced deprecated useMutation in questionnaire with tanstack's useMutation #9840

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1502,6 +1502,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
61 changes: 29 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 @@ -12,7 +13,8 @@ import { Button } from "@/components/ui/button";
import Loading from "@/components/Common/Loading";

import routes from "@/Utils/request/api";
import useMutation from "@/Utils/request/useMutation";
import mutate from "@/Utils/request/mutate";
import { HTTPError } from "@/Utils/request/types";
import useQuery from "@/Utils/request/useQuery";
import {
DetailedValidationError,
Expand Down Expand Up @@ -77,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: HTTPError) => {
abhimanyurajeesh marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -199,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 @@ -233,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 @@ -264,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 @@ -283,7 +280,7 @@ export function QuestionnaireForm({
setActiveQuestionnaireId(form.questionnaire.id);
setActiveGroupId(group.id);
}}
disabled={isProcessing}
disabled={isPending}
>
{group.text}
</button>
Expand Down Expand Up @@ -325,7 +322,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
>
<CareIcon icon="l-times-circle" />
<span>Remove</span>
Expand All @@ -347,7 +344,7 @@ export function QuestionnaireForm({
),
);
}}
disabled={isProcessing}
disabled={isPending}
activeGroupId={activeGroupId}
errors={form.errors}
patientId={patientId}
Expand Down Expand Up @@ -392,7 +389,7 @@ export function QuestionnaireForm({
},
]);
}}
disabled={isProcessing}
disabled={isPending}
/>
</div>

Expand All @@ -403,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
Loading