1
+ import { useQuery , useQueryClient } from "@tanstack/react-query" ;
1
2
import { Loader2 } from "lucide-react" ;
2
3
import { ReactNode , useState } from "react" ;
3
4
import { useTranslation } from "react-i18next" ;
@@ -19,7 +20,7 @@ import useFileUpload from "@/hooks/useFileUpload";
19
20
import { RESULTS_PER_PAGE_LIMIT } from "@/common/constants" ;
20
21
21
22
import routes from "@/Utils/request/api" ;
22
- import useTanStackQueryInstead from "@/Utils/request/useQuery " ;
23
+ import query from "@/Utils/request/query " ;
23
24
24
25
export const LinearProgressWithLabel = ( props : { value : number } ) => {
25
26
return (
@@ -89,6 +90,7 @@ export const FileUpload = (props: FileUploadProps) => {
89
90
const [ offset , setOffset ] = useState ( 0 ) ;
90
91
const [ tab , setTab ] = useState ( "UNARCHIVED" ) ;
91
92
const authUser = useAuthUser ( ) ;
93
+ const queryClient = useQueryClient ( ) ;
92
94
93
95
const handlePagination = ( page : number , limit : number ) => {
94
96
const offset = ( page - 1 ) * limit ;
@@ -118,54 +120,79 @@ export const FileUpload = (props: FileUploadProps) => {
118
120
CLAIM : claimId ,
119
121
} [ type ] || "" ;
120
122
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
+ } ;
130
136
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
+ } ) ,
139
148
} ) ;
140
149
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
+ } ) ;
152
179
153
180
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
+ } ,
157
187
} ;
158
188
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 ) ;
163
190
const fileQuery = queries [ tab as keyof typeof queries ] ;
164
191
165
192
const tabs = [
166
193
{ text : "Active Files" , value : "UNARCHIVED" } ,
167
194
{ text : "Archived Files" , value : "ARCHIVED" } ,
168
- ...( dischargeSummaryQuery . data ?. results ?. length
195
+ ...( dischargeSummaryQuery ?. results ?. length
169
196
? [
170
197
{
171
198
text : "Discharge Summary" ,
0 commit comments