Skip to content

Commit

Permalink
Refactor questionnaire response and migrated to TanStack's query (#9995)
Browse files Browse the repository at this point in the history
  • Loading branch information
abhimanyurajeesh authored Jan 17, 2025
1 parent bef340f commit a0183bb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 24 deletions.
1 change: 1 addition & 0 deletions public/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,7 @@
"error_fetching_slots_data": "Error while fetching slots data",
"error_fetching_user_data": "Error while fetching user data",
"error_fetching_user_details": "Error while fetching user details: ",
"error_loading_questionnaire_response": "Error loading questionnaire response",
"error_sending_otp": "Error while sending OTP, Please try again later",
"error_updating_encounter": "Error to Updating Encounter",
"error_verifying_otp": "Error while verifying OTP, Please request a new OTP",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useTranslation } from "react-i18next";

import CareIcon from "@/CAREUI/icons/CareIcon";
Expand All @@ -9,28 +10,27 @@ import { Skeleton } from "@/components/ui/skeleton";
import Page from "@/components/Common/Page";

import routes from "@/Utils/request/api";
import useQuery from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import { formatDateTime } from "@/Utils/utils";
import { QuestionnaireResponse as Response } from "@/types/questionnaire/form";
import { Question } from "@/types/questionnaire/question";

export default function QuestionnaireResponseView({
responseId,

patientId,
}: {
responseId: string;
patientId: string;
}) {
const { t } = useTranslation();
const { data: formResponse, loading } = useQuery(
routes.getQuestionnaireResponse,
{
const { data: formResponse, isLoading } = useQuery({
queryKey: ["getQuestionnaireResponse", patientId, responseId],
queryFn: query(routes.getQuestionnaireResponse, {
pathParams: { patientId, responseId },
},
);
}),
});

if (loading) {
if (isLoading) {
return (
<div className="flex h-full items-center justify-center">
<Skeleton className="h-8 w-64" />
Expand Down
15 changes: 9 additions & 6 deletions src/components/Questionnaire/QuestionnaireEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { DragDropContext, Draggable, Droppable } from "@hello-pangea/dnd";
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { useNavigate } from "raviger";
import { useEffect, useState } from "react";
import { toast } from "sonner";
Expand Down Expand Up @@ -29,7 +29,7 @@ import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs";
import { Textarea } from "@/components/ui/textarea";

import mutate from "@/Utils/request/mutate";
import useQuery from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import {
AnswerOption,
EnableWhen,
Expand Down Expand Up @@ -60,10 +60,13 @@ export default function QuestionnaireEditor({ id }: QuestionnaireEditorProps) {

const {
data: initialQuestionnaire,
loading,
isLoading,
error,
} = useQuery(questionnaireApi.detail, {
pathParams: { id },
} = useQuery({
queryKey: ["questionnaireDetail", id],
queryFn: query(questionnaireApi.detail, {
pathParams: { id },
}),
});

const { mutate: updateQuestionnaire, isPending } = useMutation({
Expand All @@ -87,7 +90,7 @@ export default function QuestionnaireEditor({ id }: QuestionnaireEditorProps) {
}
}, [initialQuestionnaire]);

if (loading) return <Loading />;
if (isLoading) return <Loading />;
if (error) {
return (
<Alert variant="destructive">
Expand Down
15 changes: 9 additions & 6 deletions src/components/Questionnaire/QuestionnaireForm.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMutation } from "@tanstack/react-query";
import { useMutation, useQuery } from "@tanstack/react-query";
import { t } from "i18next";
import { useNavigationPrompt } from "raviger";
import { useEffect, useState } from "react";
Expand All @@ -16,7 +16,7 @@ import Loading from "@/components/Common/Loading";
import { PLUGIN_Component } from "@/PluginEngine";
import routes from "@/Utils/request/api";
import mutate from "@/Utils/request/mutate";
import useQuery from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import {
DetailedValidationError,
QuestionValidationError,
Expand Down Expand Up @@ -74,11 +74,14 @@ export function QuestionnaireForm({

const {
data: questionnaireData,
loading: isQuestionnaireLoading,
isLoading: isQuestionnaireLoading,
error: questionnaireError,
} = useQuery(questionnaireApi.detail, {
pathParams: { id: questionnaireSlug ?? "" },
prefetch: !!questionnaireSlug && !FIXED_QUESTIONNAIRES[questionnaireSlug],
} = useQuery({
queryKey: ["questionnaireDetail", questionnaireSlug],
queryFn: query(questionnaireApi.detail, {
pathParams: { id: questionnaireSlug ?? "" },
}),
enabled: !!questionnaireSlug && !FIXED_QUESTIONNAIRES[questionnaireSlug],
});

const { mutate: submitBatch, isPending } = useMutation({
Expand Down
10 changes: 7 additions & 3 deletions src/components/Questionnaire/index.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
import { useQuery } from "@tanstack/react-query";
import { useNavigate } from "raviger";

import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";

import useQuery from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import { QuestionnaireDetail } from "@/types/questionnaire/questionnaire";
import questionnaireApi from "@/types/questionnaire/questionnaireApi";

import Loading from "../Common/Loading";

export function QuestionnaireList() {
const navigate = useNavigate();
const { data: response, loading } = useQuery(questionnaireApi.list);
const { data: response, isLoading } = useQuery({
queryKey: ["questionnaires"],
queryFn: query(questionnaireApi.list),
});

if (loading) {
if (isLoading) {
return <Loading />;
}

Expand Down
2 changes: 1 addition & 1 deletion src/hooks/useQuestionnaireOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const DEFAULT_OPTIONS: EditQuestionnaireOption[] = [

export default function useQuestionnaireOptions(slug: string) {
const { data } = useQuery({
queryKey: ["questionnaire", slug] as const,
queryKey: ["questionnaires", slug] as const,
queryFn: query(questionnaireApi.list, {
queryParams: {
tag_slug: slug,
Expand Down

0 comments on commit a0183bb

Please sign in to comment.