@@ -3,6 +3,7 @@ import { type QueueEntry, type QueueEntrySearchCriteria } from '../types';
3
3
import useSWR from 'swr' ;
4
4
import { useCallback , useEffect , useMemo , useState } from 'react' ;
5
5
import { useSWRConfig } from 'swr/_internal' ;
6
+ import isEqual from 'lodash-es/isEqual' ;
6
7
7
8
type QueueEntryResponse = FetchResponse < {
8
9
results : Array < QueueEntry > ;
@@ -84,11 +85,37 @@ export function useQueueEntries(searchCriteria?: QueueEntrySearchCriteria, rep:
84
85
const [ data , setData ] = useState < Array < Array < QueueEntry > > > ( [ ] ) ;
85
86
const [ totalCount , setTotalCount ] = useState < number > ( ) ;
86
87
const [ currentPage , setCurrentPage ] = useState < number > ( 0 ) ;
87
- const [ pageUrl , setPageUrl ] = useState < string > ( getInitialUrl ( rep , searchCriteria ) ) ;
88
+ const [ currentSearchCriteria , setCurrentSearchCriteria ] = useState ( searchCriteria ) ;
89
+ const [ currentRep , setCurrentRep ] = useState ( rep ) ;
90
+ const [ pageUrl , setPageUrl ] = useState < string > ( getInitialUrl ( currentRep , currentSearchCriteria ) ) ;
88
91
const [ error , setError ] = useState < Error > ( ) ;
89
92
const { mutateQueueEntries } = useMutateQueueEntries ( ) ;
90
93
const [ waitingForMutate , setWaitingForMutate ] = useState ( false ) ;
91
94
95
+ const refetchAllData = useCallback (
96
+ ( newRep : string = currentRep , newSearchCriteria : QueueEntrySearchCriteria = currentSearchCriteria ) => {
97
+ setWaitingForMutate ( true ) ;
98
+ setCurrentPage ( 0 ) ;
99
+ setPageUrl ( getInitialUrl ( newRep , newSearchCriteria ) ) ;
100
+ } ,
101
+ [ currentRep , currentSearchCriteria ] ,
102
+ ) ;
103
+
104
+ // This hook listens to the searchCriteria and rep values and refetches the data when they change.
105
+ useEffect ( ( ) => {
106
+ const isSearchCriteriaUpdated = ! isEqual ( currentSearchCriteria , searchCriteria ) ;
107
+ const isRepUpdated = currentRep !== rep ;
108
+ if ( isSearchCriteriaUpdated || isRepUpdated ) {
109
+ if ( isSearchCriteriaUpdated ) {
110
+ setCurrentSearchCriteria ( searchCriteria ) ;
111
+ }
112
+ if ( isRepUpdated ) {
113
+ setCurrentRep ( rep ) ;
114
+ }
115
+ refetchAllData ( rep , searchCriteria ) ;
116
+ }
117
+ } , [ searchCriteria , currentSearchCriteria , setCurrentSearchCriteria , currentRep , rep ] ) ;
118
+
92
119
const { data : pageData , isValidating, error : pageError } = useSWR < QueueEntryResponse , Error > ( pageUrl , openmrsFetch ) ;
93
120
94
121
useEffect ( ( ) => {
@@ -99,10 +126,10 @@ export function useQueueEntries(searchCriteria?: QueueEntrySearchCriteria, rep:
99
126
}
100
127
if ( pageData && ! isValidating && ! stillWaitingForMutate ) {
101
128
// We've got results! Time to update the data array and move on to the next page.
102
- if ( pageData ?. data ?. totalCount && pageData ?. data ?. totalCount !== totalCount ) {
129
+ if ( pageData ?. data ?. totalCount > - 1 && pageData ?. data ?. totalCount !== totalCount ) {
103
130
setTotalCount ( pageData ?. data ?. totalCount ) ;
104
131
}
105
- if ( pageData ?. data ?. results ?. length ) {
132
+ if ( pageData ?. data ?. results ) {
106
133
const newData = [ ...data ] ;
107
134
newData [ currentPage ] = pageData ?. data ?. results ;
108
135
setData ( newData ) ;
@@ -140,10 +167,8 @@ export function useQueueEntries(searchCriteria?: QueueEntrySearchCriteria, rep:
140
167
} , [ pageError ] ) ;
141
168
142
169
const queueUpdateListener = useCallback ( ( ) => {
143
- setWaitingForMutate ( true ) ;
144
- setCurrentPage ( 0 ) ;
145
- setPageUrl ( getInitialUrl ( rep , searchCriteria ) ) ;
146
- } , [ rep , searchCriteria ] ) ;
170
+ refetchAllData ( ) ;
171
+ } , [ refetchAllData ] ) ;
147
172
148
173
useEffect ( ( ) => {
149
174
window . addEventListener ( 'queue-entry-updated' , queueUpdateListener ) ;
@@ -157,7 +182,7 @@ export function useQueueEntries(searchCriteria?: QueueEntrySearchCriteria, rep:
157
182
return {
158
183
queueEntries,
159
184
totalCount,
160
- isLoading : ! totalCount || ( totalCount && queueEntries . length < totalCount ) ,
185
+ isLoading : totalCount === undefined || ( totalCount && queueEntries . length < totalCount ) ,
161
186
isValidating : isValidating || currentPage < data . length ,
162
187
error,
163
188
mutate : mutateQueueEntries ,
0 commit comments