Skip to content

Commit

Permalink
Replace useTanStack with useQuery (#10345)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdityaJ2305 authored Feb 3, 2025
1 parent 6d880c7 commit 00fbb56
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 91 deletions.
16 changes: 10 additions & 6 deletions src/components/Files/FileBlock.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import dayjs from "dayjs";
import { t } from "i18next";

Expand All @@ -12,7 +13,7 @@ import { FileManagerResult } from "@/hooks/useFileManager";
import { FILE_EXTENSIONS } from "@/common/constants";

import routes from "@/Utils/request/api";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";

export interface FileBlockProps {
file: FileUploadModel;
Expand All @@ -33,10 +34,13 @@ export default function FileBlock(props: FileBlockProps) {

const filetype = fileManager.getFileType(file);

const fileData = useTanStackQueryInstead(routes.retrieveUpload, {
query: { file_type: fileManager.type, associating_id },
pathParams: { id: file.id || "" },
prefetch: filetype === "AUDIO" && !file.is_archived,
const { data: fileData } = useQuery({
queryKey: ["file", { id: file.id, type: fileManager.type, associating_id }],
queryFn: query(routes.retrieveUpload, {
queryParams: { file_type: fileManager.type, associating_id },
pathParams: { id: file.id || "" },
}),
enabled: filetype === "AUDIO" && !file.is_archived,
});

const icons: Record<keyof typeof FILE_EXTENSIONS | "UNKNOWN", IconName> = {
Expand Down Expand Up @@ -82,7 +86,7 @@ export default function FileBlock(props: FileBlockProps) {
<div className="w-full md:w-[300px]">
<audio
className="max-h-full w-full object-contain"
src={fileData.data?.read_signed_url}
src={fileData?.read_signed_url}
controls
preload="auto"
controlsList="nodownload"
Expand Down
98 changes: 62 additions & 36 deletions src/components/Files/FileUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useQuery, useQueryClient } from "@tanstack/react-query";
import { Loader2 } from "lucide-react";
import { ReactNode, useState } from "react";
import { useTranslation } from "react-i18next";
Expand All @@ -20,7 +21,7 @@ import useFileUpload from "@/hooks/useFileUpload";
import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";

import routes from "@/Utils/request/api";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";

export const LinearProgressWithLabel = (props: { value: number }) => {
return (
Expand Down Expand Up @@ -90,6 +91,7 @@ export const FileUpload = (props: FileUploadProps) => {
const [offset, setOffset] = useState(0);
const [tab, setTab] = useState("UNARCHIVED");
const authUser = useAuthUser();
const queryClient = useQueryClient();

const handlePagination = (page: number, limit: number) => {
const offset = (page - 1) * limit;
Expand Down Expand Up @@ -119,54 +121,78 @@ export const FileUpload = (props: FileUploadProps) => {
CLAIM: claimId,
}[type] || "";

const activeFilesQuery = useTanStackQueryInstead(routes.viewUpload, {
query: {
file_type: type,
associating_id: associatedId,
is_archived: false,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
},
});
const refetchAll = () => {
queryClient.invalidateQueries({
queryKey: ["viewUpload", "active", type, associatedId],
});
queryClient.invalidateQueries({
queryKey: ["viewUpload", "archived", type, associatedId],
});
if (type === "consultation") {
queryClient.invalidateQueries({
queryKey: ["viewUpload", "discharge_summary", associatedId],
});
}
};

const archivedFilesQuery = useTanStackQueryInstead(routes.viewUpload, {
query: {
file_type: type,
associating_id: associatedId,
is_archived: true,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
},
const { data: activeFiles, isLoading: activeFilesLoading } = useQuery({
queryKey: ["viewUpload", "active", type, associatedId, offset],
queryFn: query(routes.viewUpload, {
queryParams: {
file_type: type,
associating_id: associatedId,
is_archived: false,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
},
}),
});

const dischargeSummaryQuery = useTanStackQueryInstead(routes.viewUpload, {
query: {
file_type: "discharge_summary",
associating_id: associatedId,
is_archived: false,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
},
prefetch: type === "consultation",
silent: true,
const { data: archivedFiles, isLoading: archivedFilesLoading } = useQuery({
queryKey: ["viewUpload", "archived", type, associatedId, offset],
queryFn: query(routes.viewUpload, {
queryParams: {
file_type: type,
associating_id: associatedId,
is_archived: true,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
},
}),
});

const { data: dischargeSummary, isLoading: dischargeSummaryLoading } =
useQuery({
queryKey: ["viewUpload", "discharge_summary", associatedId, offset],
queryFn: query(routes.viewUpload, {
queryParams: {
file_type: "discharge_summary",
associating_id: associatedId,
is_archived: false,
limit: RESULTS_PER_PAGE_LIMIT,
offset: offset,
silent: true,
},
}),
enabled: type === "consultation",
});

const queries = {
UNARCHIVED: activeFilesQuery,
ARCHIVED: archivedFilesQuery,
DISCHARGE_SUMMARY: dischargeSummaryQuery,
UNARCHIVED: { data: activeFiles, isLoading: activeFilesLoading },
ARCHIVED: { data: archivedFiles, isLoading: archivedFilesLoading },
DISCHARGE_SUMMARY: {
data: dischargeSummary,
isLoading: dischargeSummaryLoading,
},
};

const refetchAll = async () =>
Promise.all(Object.values(queries).map((q) => q.refetch()));
const loading = Object.values(queries).some((q) => q.loading);

const loading = Object.values(queries).some((q) => q.isLoading);
const fileQuery = queries[tab as keyof typeof queries];

const tabs = [
{ text: "Active Files", value: "UNARCHIVED" },
{ text: "Archived Files", value: "ARCHIVED" },
...(dischargeSummaryQuery.data?.results?.length
...(dischargeSummary?.results?.length
? [
{
text: "Discharge Summary",
Expand Down
15 changes: 11 additions & 4 deletions src/components/Patient/FileUploadPage.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useQuery } from "@tanstack/react-query";

import Page from "@/components/Common/Page";
import { FileUpload } from "@/components/Files/FileUpload";

import routes from "@/Utils/request/api";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";

export default function FileUploadPage(props: {
facilityId: string;
Expand All @@ -11,10 +13,15 @@ export default function FileUploadPage(props: {
type: "encounter" | "patient";
}) {
const { facilityId, patientId, encounterId, type } = props;
const { data: patient } = useTanStackQueryInstead(routes.getPatient, {
pathParams: { id: patientId },
prefetch: !!patientId,

const { data: patient } = useQuery({
queryKey: ["patient", patientId],
queryFn: query(routes.getPatient, {
pathParams: { id: patientId },
}),
enabled: !!patientId,
});

return (
<Page
hideBack={false}
Expand Down
89 changes: 44 additions & 45 deletions src/components/Resource/ResourceDetailsUpdate.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useQuery } from "@tanstack/react-query";
import { useMutation } from "@tanstack/react-query";
import { t } from "i18next";
import { navigate, useQueryParams } from "raviger";
import { useReducer, useState } from "react";
import { useEffect, useReducer, useState } from "react";
import { toast } from "sonner";

import Card from "@/CAREUI/display/Card";
Expand All @@ -26,8 +28,8 @@ import useAppHistory from "@/hooks/useAppHistory";
import { RESOURCE_STATUS_CHOICES } from "@/common/constants";

import routes from "@/Utils/request/api";
import request from "@/Utils/request/request";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import mutate from "@/Utils/request/mutate";
import query from "@/Utils/request/query";
import { UpdateResourceRequest } from "@/types/resourceRequest/resourceRequest";

interface resourceProps {
Expand Down Expand Up @@ -62,7 +64,6 @@ const initialState = {
export const ResourceDetailsUpdate = (props: resourceProps) => {
const { goBack } = useAppHistory();
const [qParams, _] = useQueryParams();
const [isLoading, setIsLoading] = useState(true);
const [assignedUser, SetAssignedUser] = useState<UserModel>();
const resourceFormReducer = (state = initialState, action: any) => {
switch (action.type) {
Expand All @@ -84,17 +85,16 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
};

const [state, dispatch] = useReducer(resourceFormReducer, initialState);

const { loading: assignedUserLoading } = useTanStackQueryInstead(
routes.userList,
{
onResponse: ({ res, data }) => {
if (res?.ok && data && data.count) {
SetAssignedUser(data.results[0]);
}
},
},
);
const { data, isLoading: assignedUserLoading } = useQuery({
queryKey: ["user", props.facilityId],
queryFn: query(routes.userList),
});

useEffect(() => {
if (data) {
SetAssignedUser(data.results[0]);
}
}, [data]);

const validateForm = () => {
const errors = { ...initError };
Expand Down Expand Up @@ -129,28 +129,40 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
form[name] = selected;
dispatch({ type: "set_form", form });
};

const { data: resourceDetails } = useTanStackQueryInstead(
routes.getResourceDetails,
{
const { data: resourceDetails } = useQuery({
queryKey: ["resource", props.facilityId, props.id],
queryFn: query(routes.getResourceDetails, {
pathParams: { id: props.id },
onResponse: ({ res, data }) => {
if (res && data) {
const d = data;
d["status"] = qParams.status || data.status.toLowerCase();
dispatch({ type: "set_form", form: d });
}
setIsLoading(false);
}),
});
useEffect(() => {
if (resourceDetails) {
dispatch({
type: "set_form",
form: {
...resourceDetails,
status: qParams.status || resourceDetails.status.toLowerCase(),
},
});
}
}, [resourceDetails]);

const { mutate: updateResource, isPending: updateResourceLoading } =
useMutation({
mutationFn: mutate(routes.updateResource, {
pathParams: { id: props.id },
}),
onSuccess: (data) => {
dispatch({ type: "set_form", form: data });
toast.success(t("request_updated_successfully"));
navigate(`/facility/${props.facilityId}/resource/${props.id}`);
},
},
);
});

const handleSubmit = async () => {
const validForm = validateForm();

if (validForm) {
setIsLoading(true);

const resourceData: UpdateResourceRequest = {
id: props.id,
status: state.form.status,
Expand All @@ -169,24 +181,11 @@ export const ResourceDetailsUpdate = (props: resourceProps) => {
approving_facility: state.form.approving_facility?.id,
related_patient: state.form.related_patient?.id,
};

const { res, data } = await request(routes.updateResource, {
pathParams: { id: props.id },
body: resourceData,
});
setIsLoading(false);

if (res && res.status == 200 && data) {
dispatch({ type: "set_form", form: data });
toast.success(t("request_updated_successfully"));
navigate(`/facility/${props.facilityId}/resource/${props.id}`);
} else {
setIsLoading(false);
}
updateResource(resourceData);
}
};

if (isLoading) {
if (updateResourceLoading || !resourceDetails) {
return <Loading />;
}

Expand Down

0 comments on commit 00fbb56

Please sign in to comment.