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

Replace useTanStack with useQuery #10345

Merged
Show file tree
Hide file tree
Changes from 5 commits
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
2 changes: 1 addition & 1 deletion src/CAREUI/misc/PaginatedList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import { classNames } from "@/Utils/utils";

const DEFAULT_PER_PAGE_LIMIT = 14;

// only this file is pending except this remaining is ready for review
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
interface PaginatedListContext<TItem>
extends ReturnType<typeof useTanStackQueryInstead<PaginatedResponse<TItem>>> {
items: TItem[];
Expand Down Expand Up @@ -77,7 +77,7 @@
if (queryCB) {
queryCB(query);
}
}, [query]);

Check warning on line 80 in src/CAREUI/misc/PaginatedList.tsx

View workflow job for this annotation

GitHub Actions / cypress-run (1)

React Hook useEffect has a missing dependency: 'queryCB'. Either include it or remove the dependency array. If 'queryCB' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<context.Provider
Expand Down
38 changes: 21 additions & 17 deletions src/components/Common/UserAutocompleteFormField.tsx
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useQuery } from "@tanstack/react-query";
import { useEffect, useState } from "react";

import { Autocomplete } from "@/components/Form/FormFields/Autocomplete";
Expand All @@ -10,7 +11,7 @@ import { UserType } from "@/components/Users/UserFormValidations";
import { UserBareMinimum } from "@/components/Users/models";

import routes from "@/Utils/request/api";
import useTanStackQueryInstead from "@/Utils/request/useQuery";
import query from "@/Utils/request/query";
import {
classNames,
formatName,
Expand All @@ -33,35 +34,38 @@ type UserSearchProps = BaseProps & {

export default function UserAutocomplete(props: UserSearchProps) {
const field = useFormFieldPropsResolver(props);
const [query, setQuery] = useState("");
const [queryParam, setQueryParam] = useState("");
const [disabled, setDisabled] = useState(false);

const { data, loading } = useTanStackQueryInstead(routes.userList, {
query: {
home_facility: props.homeFacility,
user_type: props.userType,
search_text: query,
limit: 50,
offset: 0,
},
const { data, isLoading } = useQuery({
queryKey: ["user", props.homeFacility, props.facilityId],
queryFn: query(routes.userList, {
queryParams: {
home_facility: props.homeFacility,
user_type: props.userType,
search_text: queryParam,
limit: 50,
offset: 0,
},
}),
});

useEffect(() => {
if (
loading ||
query ||
isLoading ||
queryParam ||
!field.required ||
!props.noResultsError ||
!data?.results
!data
) {
return;
}

if (data.results.length === 0) {
if (data.count === 0) {
setDisabled(true);
field.handleChange(undefined as unknown as UserBareMinimum);
}
}, [loading, field.required, data?.results, props.noResultsError]);
}, [isLoading, field.required, data?.results, props.noResultsError]);

const getAvatar = (option: UserBareMinimum) => {
return (
Expand Down Expand Up @@ -94,8 +98,8 @@ export default function UserAutocomplete(props: UserSearchProps) {
`${option.user_type} - ${option.username}`
}
optionValue={(option) => option}
onQuery={setQuery}
isLoading={loading}
onQuery={setQueryParam}
isLoading={isLoading}
/>
</FormField>
);
Expand Down
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 @@ -19,7 +20,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 @@ -89,6 +90,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 @@ -118,54 +120,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", facilityId, patientId],
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
queryFn: query(routes.getPatient, {
pathParams: { id: patientId },
}),
enabled: !!patientId,
});

return (
<Page
hideBack={false}
Expand Down
57 changes: 30 additions & 27 deletions src/components/Resource/ResourceDetailsUpdate.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useQuery } 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 +27,8 @@ import useAppHistory from "@/hooks/useAppHistory";
import { RESOURCE_STATUS_CHOICES } from "@/common/constants";

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

interface resourceProps {
Expand Down Expand Up @@ -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]);
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved

const validateForm = () => {
const errors = { ...initError };
Expand Down Expand Up @@ -129,21 +129,24 @@ 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(),
},
});
}
setIsLoading(false);
AdityaJ2305 marked this conversation as resolved.
Show resolved Hide resolved
}, [resourceDetails]);

const handleSubmit = async () => {
const validForm = validateForm();
Expand Down
Loading