Skip to content

Commit

Permalink
Replace deprecated data fetching utils. with tanstack's useMutation
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyurajeesh committed Jan 8, 2025
1 parent 450a6b9 commit eb07a6e
Showing 1 changed file with 20 additions and 24 deletions.
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({
mutationFn: mutate(routes.batchRequest),
onSuccess: () => {
toast.success("Questionnaire submitted successfully");
onSubmit?.();
},
onError: (error: any) => {
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
submitBatch({ requests });
};

return (
Expand Down

0 comments on commit eb07a6e

Please sign in to comment.