Skip to content

Commit d210a44

Browse files
Refactor to use React Query for data fetching in FileBlock, FileUpload, UserAvatar, and UserHome components
1 parent b24e645 commit d210a44

File tree

4 files changed

+101
-75
lines changed

4 files changed

+101
-75
lines changed

src/components/Files/FileBlock.tsx

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQuery } from "@tanstack/react-query";
12
import dayjs from "dayjs";
23
import { t } from "i18next";
34

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

1415
import routes from "@/Utils/request/api";
15-
import useTanStackQueryInstead from "@/Utils/request/useQuery";
16+
import query from "@/Utils/request/query";
1617

1718
export interface FileBlockProps {
1819
file: FileUploadModel;
@@ -33,10 +34,18 @@ export default function FileBlock(props: FileBlockProps) {
3334

3435
const filetype = fileManager.getFileType(file);
3536

36-
const fileData = useTanStackQueryInstead(routes.retrieveUpload, {
37-
query: { file_type: fileManager.type, associating_id },
38-
pathParams: { id: file.id || "" },
39-
prefetch: filetype === "AUDIO" && !file.is_archived,
37+
const { data: fileData } = useQuery({
38+
queryKey: [
39+
routes.retrieveUpload.path,
40+
file.id,
41+
fileManager.type,
42+
associating_id,
43+
],
44+
queryFn: query(routes.retrieveUpload, {
45+
queryParams: { file_type: fileManager.type, associating_id },
46+
pathParams: { id: file.id || "" },
47+
}),
48+
enabled: filetype === "AUDIO" && !file.is_archived,
4049
});
4150

4251
const icons: Record<keyof typeof FILE_EXTENSIONS | "UNKNOWN", IconName> = {
@@ -82,7 +91,7 @@ export default function FileBlock(props: FileBlockProps) {
8291
<div className="w-full md:w-[300px]">
8392
<audio
8493
className="max-h-full w-full object-contain"
85-
src={fileData.data?.read_signed_url}
94+
src={fileData?.read_signed_url}
8695
controls
8796
preload="auto"
8897
controlsList="nodownload"

src/components/Files/FileUpload.tsx

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useQuery, useQueryClient } from "@tanstack/react-query";
12
import { Loader2 } from "lucide-react";
23
import { ReactNode, useState } from "react";
34
import { useTranslation } from "react-i18next";
@@ -19,7 +20,7 @@ import useFileUpload from "@/hooks/useFileUpload";
1920
import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants";
2021

2122
import routes from "@/Utils/request/api";
22-
import useTanStackQueryInstead from "@/Utils/request/useQuery";
23+
import query from "@/Utils/request/query";
2324

2425
export const LinearProgressWithLabel = (props: { value: number }) => {
2526
return (
@@ -89,6 +90,7 @@ export const FileUpload = (props: FileUploadProps) => {
8990
const [offset, setOffset] = useState(0);
9091
const [tab, setTab] = useState("UNARCHIVED");
9192
const authUser = useAuthUser();
93+
const queryClient = useQueryClient();
9294

9395
const handlePagination = (page: number, limit: number) => {
9496
const offset = (page - 1) * limit;
@@ -118,54 +120,79 @@ export const FileUpload = (props: FileUploadProps) => {
118120
CLAIM: claimId,
119121
}[type] || "";
120122

121-
const activeFilesQuery = useTanStackQueryInstead(routes.viewUpload, {
122-
query: {
123-
file_type: type,
124-
associating_id: associatedId,
125-
is_archived: false,
126-
limit: RESULTS_PER_PAGE_LIMIT,
127-
offset: offset,
128-
},
129-
});
123+
const refetchAll = () => {
124+
queryClient.invalidateQueries({
125+
queryKey: ["viewUpload", "active", type, associatedId],
126+
});
127+
queryClient.invalidateQueries({
128+
queryKey: ["viewUpload", "archived", type, associatedId],
129+
});
130+
if (type === "consultation") {
131+
queryClient.invalidateQueries({
132+
queryKey: ["viewUpload", "discharge_summary", associatedId],
133+
});
134+
}
135+
};
130136

131-
const archivedFilesQuery = useTanStackQueryInstead(routes.viewUpload, {
132-
query: {
133-
file_type: type,
134-
associating_id: associatedId,
135-
is_archived: true,
136-
limit: RESULTS_PER_PAGE_LIMIT,
137-
offset: offset,
138-
},
137+
const { data: activeFilesQuery, isLoading: activeFilesLoading } = useQuery({
138+
queryKey: ["viewUpload", "active", type, associatedId, offset],
139+
queryFn: query(routes.viewUpload, {
140+
queryParams: {
141+
file_type: type,
142+
associating_id: associatedId,
143+
is_archived: false,
144+
limit: RESULTS_PER_PAGE_LIMIT,
145+
offset: offset,
146+
},
147+
}),
139148
});
140149

141-
const dischargeSummaryQuery = useTanStackQueryInstead(routes.viewUpload, {
142-
query: {
143-
file_type: "discharge_summary",
144-
associating_id: associatedId,
145-
is_archived: false,
146-
limit: RESULTS_PER_PAGE_LIMIT,
147-
offset: offset,
148-
},
149-
prefetch: type === "consultation",
150-
silent: true,
151-
});
150+
const { data: archivedFilesQuery, isLoading: archivedFilesLoading } =
151+
useQuery({
152+
queryKey: ["viewUpload", "archived", type, associatedId, offset],
153+
queryFn: query(routes.viewUpload, {
154+
queryParams: {
155+
file_type: type,
156+
associating_id: associatedId,
157+
is_archived: true,
158+
limit: RESULTS_PER_PAGE_LIMIT,
159+
offset: offset,
160+
},
161+
}),
162+
});
163+
164+
const { data: dischargeSummaryQuery, isLoading: dischargeSummaryLoading } =
165+
useQuery({
166+
queryKey: ["viewUpload", "discharge_summary", associatedId, offset],
167+
queryFn: query(routes.viewUpload, {
168+
queryParams: {
169+
file_type: "discharge_summary",
170+
associating_id: associatedId,
171+
is_archived: false,
172+
limit: RESULTS_PER_PAGE_LIMIT,
173+
offset: offset,
174+
silent: true,
175+
},
176+
}),
177+
enabled: type === "consultation",
178+
});
152179

153180
const queries = {
154-
UNARCHIVED: activeFilesQuery,
155-
ARCHIVED: archivedFilesQuery,
156-
DISCHARGE_SUMMARY: dischargeSummaryQuery,
181+
UNARCHIVED: { data: activeFilesQuery, isLoading: activeFilesLoading },
182+
ARCHIVED: { data: archivedFilesQuery, isLoading: archivedFilesLoading },
183+
DISCHARGE_SUMMARY: {
184+
data: dischargeSummaryQuery,
185+
isLoading: dischargeSummaryLoading,
186+
},
157187
};
158188

159-
const refetchAll = async () =>
160-
Promise.all(Object.values(queries).map((q) => q.refetch()));
161-
const loading = Object.values(queries).some((q) => q.loading);
162-
189+
const loading = Object.values(queries).some((q) => q.isLoading);
163190
const fileQuery = queries[tab as keyof typeof queries];
164191

165192
const tabs = [
166193
{ text: "Active Files", value: "UNARCHIVED" },
167194
{ text: "Archived Files", value: "ARCHIVED" },
168-
...(dischargeSummaryQuery.data?.results?.length
195+
...(dischargeSummaryQuery?.results?.length
169196
? [
170197
{
171198
text: "Discharge Summary",

src/components/Users/UserAvatar.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import careConfig from "@careConfig";
22
import { useQueryClient } from "@tanstack/react-query";
3+
import { useQuery } from "@tanstack/react-query";
34
import { useState } from "react";
45
import { useTranslation } from "react-i18next";
56
import { toast } from "sonner";
@@ -15,9 +16,9 @@ import useAuthUser from "@/hooks/useAuthUser";
1516

1617
import { showAvatarEdit } from "@/Utils/permissions";
1718
import routes from "@/Utils/request/api";
19+
import query from "@/Utils/request/query";
1820
import request from "@/Utils/request/request";
1921
import uploadFile from "@/Utils/request/uploadFile";
20-
import useTanStackQueryInstead from "@/Utils/request/useQuery";
2122
import { getAuthorizationHeader } from "@/Utils/request/utils";
2223
import { formatDisplayName, sleep } from "@/Utils/utils";
2324

@@ -27,14 +28,14 @@ export default function UserAvatar({ username }: { username: string }) {
2728
const authUser = useAuthUser();
2829
const queryClient = useQueryClient();
2930

30-
const { data: userData, loading: isLoading } = useTanStackQueryInstead(
31-
routes.getUserDetails,
32-
{
31+
const { data: userData, isLoading } = useQuery({
32+
queryKey: ["getUserDetails", username],
33+
queryFn: query(routes.getUserDetails, {
3334
pathParams: {
3435
username: username,
3536
},
36-
},
37-
);
37+
}),
38+
});
3839

3940
if (isLoading || !userData) {
4041
return <Loading />;

src/components/Users/UserHome.tsx

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Link, navigate } from "raviger";
2-
import { useState } from "react";
1+
import { useQuery } from "@tanstack/react-query";
2+
import { Link } from "raviger";
33
import { useTranslation } from "react-i18next";
4-
import { toast } from "sonner";
54

65
import Loading from "@/components/Common/Loading";
76
import Page from "@/components/Common/Page";
@@ -14,9 +13,8 @@ import UserSummaryTab from "@/components/Users/UserSummary";
1413
import useAuthUser from "@/hooks/useAuthUser";
1514

1615
import routes from "@/Utils/request/api";
17-
import useTanStackQueryInstead from "@/Utils/request/useQuery";
16+
import query from "@/Utils/request/query";
1817
import { classNames, formatName, keysOf } from "@/Utils/utils";
19-
import { UserBase } from "@/types/user/user";
2018

2119
export interface UserHomeProps {
2220
username?: string;
@@ -31,35 +29,26 @@ export interface TabChildProp {
3129
export default function UserHome(props: UserHomeProps) {
3230
const { tab } = props;
3331
let { username } = props;
34-
const [userData, setUserData] = useState<UserBase>();
3532
const { t } = useTranslation();
3633
const authUser = useAuthUser();
3734
if (!username) {
3835
username = authUser.username;
3936
}
4037
const loggedInUser = username === authUser.username;
4138

42-
const { loading, refetch: refetchUserDetails } = useTanStackQueryInstead(
43-
routes.getUserDetails,
44-
{
45-
pathParams: {
46-
username: username,
47-
},
48-
onResponse: ({ res, data, error }) => {
49-
if (res?.status === 200 && data) {
50-
setUserData(data);
51-
} else if (res?.status === 400) {
52-
navigate("/users");
53-
} else if (error) {
54-
toast.error(
55-
t("error_fetching_user_details") + (error?.message || ""),
56-
);
57-
}
58-
},
59-
},
60-
);
39+
const {
40+
data: userData,
41+
isLoading,
42+
refetch: refetchUserDetails,
43+
} = useQuery({
44+
queryKey: ["getUserDetails", username],
45+
queryFn: query(routes.getUserDetails, {
46+
pathParams: { username },
47+
}),
48+
enabled: Boolean(username),
49+
});
6150

62-
if (loading || !userData) {
51+
if (isLoading || !userData) {
6352
return <Loading />;
6453
}
6554

0 commit comments

Comments
 (0)