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
Changes from 1 commit
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
44 changes: 20 additions & 24 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,7 @@ 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 useQuery from "@/Utils/request/useQuery";
import {
DetailedValidationError,
Expand Down Expand Up @@ -77,10 +78,19 @@ export function QuestionnaireForm({
prefetch: !!questionnaireSlug && !FIXED_QUESTIONNAIRES[questionnaireSlug],
});

const { mutate: submitBatch, isProcessing } = useMutation(
routes.batchRequest,
{ silent: true },
);
const { mutate: submitBatch, isPending: isProcessing } = useMutation({
abhimanyurajeesh marked this conversation as resolved.
Show resolved Hide resolved
mutationFn: mutate(routes.batchRequest),
abhimanyurajeesh marked this conversation as resolved.
Show resolved Hide resolved
onSuccess: () => {
toast.success("Questionnaire submitted successfully");
onSubmit?.();
},
onError: (error: any) => {
abhimanyurajeesh marked this conversation as resolved.
Show resolved Hide resolved
if (error.results) {
handleSubmissionError(error.results as ValidationErrorResponse[]);
}
toast.error("Failed to submit questionnaire");
},
});

useEffect(() => {
if (!isInitialized && questionnaireSlug) {
Expand Down Expand Up @@ -199,9 +209,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 +243,8 @@ 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?.();
// Execute the mutation
abhimanyurajeesh marked this conversation as resolved.
Show resolved Hide resolved
submitBatch({ requests });
};

return (
Expand Down
Loading