@@ -87,16 +87,17 @@ export default function FacilityOrganizationSelector(
8787 const isMobile = useBreakpoints ( { default : true , sm : false } ) ;
8888
8989 // Fetch preferred organizations
90- const { data : preferredOrganizations } = useQuery ( {
91- queryKey : [ "facilityOrganization-favorites" , facilityId , favoriteList ] ,
92- queryFn : query ( facilityOrganizationApi . list , {
93- pathParams : { facilityId } ,
94- queryParams : {
95- favorite_list : favoriteList ,
96- } ,
97- } ) ,
98- enabled : ! ! favoriteList ,
99- } ) ;
90+ const { data : preferredOrganizations , isLoading : isLoadingPreferred } =
91+ useQuery ( {
92+ queryKey : [ "facilityOrganization-favorites" , facilityId , favoriteList ] ,
93+ queryFn : query ( facilityOrganizationApi . list , {
94+ pathParams : { facilityId } ,
95+ queryParams : {
96+ favorite_list : favoriteList ,
97+ } ,
98+ } ) ,
99+ enabled : ! ! favoriteList ,
100+ } ) ;
100101
101102 const preferredOrgIds = useMemo ( ( ) => {
102103 return preferredOrganizations ?. results ?. map ( ( org ) => org . id ) || [ ] ;
@@ -132,28 +133,86 @@ export default function FacilityOrganizationSelector(
132133 } ) ) ,
133134 } ) ;
134135
136+ const handleConfirmSelection = useCallback (
137+ ( org : FacilityOrganizationRead ) => {
138+ if ( ! selectedOrganizations . includes ( org ) ) {
139+ const newSelection = singleSelection
140+ ? [ org ]
141+ : [ ...selectedOrganizations , org ] ;
142+ setSelectedOrganizations ( newSelection ) ;
143+ onChange ( newSelection . map ( ( org ) => org . id ) ) ;
144+ setAlreadySelected ( true ) ;
145+ }
146+ setCurrentSelection ( null ) ;
147+ setNavigationLevels ( [ ] ) ;
148+ setOpen ( false ) ;
149+ } ,
150+ [ selectedOrganizations , onChange ] ,
151+ ) ;
152+
153+ const getCurrentLevelOrganizations = useCallback ( ( ) => {
154+ if ( navigationLevels . length === 0 ) {
155+ return rootOrganizations ?. results || [ ] ;
156+ }
157+ const lastQuery = organizationQueries [ navigationLevels . length - 1 ] ;
158+ return lastQuery ?. data ?. results || [ ] ;
159+ } , [ navigationLevels , rootOrganizations , organizationQueries ] ) ;
160+
161+ // Auto-select when there's only one organization available
162+ useEffect ( ( ) => {
163+ const availableOrganizations = getCurrentLevelOrganizations ( ) ;
164+
165+ // Only auto-select if:
166+ // 1. We're at the root level (no navigation levels)
167+ // 2. There's exactly one organization
168+ // 3. No search is active
169+ // 4. No organizations are currently selected
170+ // 5. Not loading
171+ if (
172+ navigationLevels . length === 0 &&
173+ availableOrganizations . length === 1 &&
174+ ! facilityOrgSearch &&
175+ selectedOrganizations . length === 0 &&
176+ ! isLoadingRoot &&
177+ ! isLoadingPreferred &&
178+ preferredOrgIds . length === 0
179+ ) {
180+ const singleOrg = availableOrganizations [ 0 ] ;
181+
182+ // Check if this organization is already selected in currentOrganizations prop
183+ const isAlreadyInCurrent = currentOrganizations ?. find (
184+ ( org ) => org . id === singleOrg . id ,
185+ ) ;
186+
187+ if ( ! isAlreadyInCurrent && ! props . optional ) {
188+ handleConfirmSelection ( singleOrg ) ;
189+ }
190+ }
191+ } , [
192+ getCurrentLevelOrganizations ,
193+ handleConfirmSelection ,
194+ navigationLevels ,
195+ facilityOrgSearch ,
196+ selectedOrganizations ,
197+ isLoadingRoot ,
198+ currentOrganizations ,
199+ props . optional ,
200+ ] ) ;
201+
135202 useEffect ( ( ) => {
136203 if ( value && value . length > 0 ) {
137204 const resolvedOrganizations = value
138- . map (
139- ( id ) =>
140- currentOrganizations ?. find ( ( org ) => org . id === id ) ||
141- preferredOrganizations ?. results ?. find ( ( org ) => org . id === id ) ,
142- )
205+ . map ( ( id ) => currentOrganizations ?. find ( ( org ) => org . id === id ) )
143206 . filter ( ( org ) => org !== undefined ) ;
144207 if ( resolvedOrganizations . length > 0 ) {
145208 setSelectedOrganizations ( resolvedOrganizations ) ;
146- } else {
147- // Value has IDs but we can't resolve them - reset to allow auto-select
148- setSelectedOrganizations ( [ ] ) ;
149- setHasAutoSelectedPreferred ( false ) ;
150209 }
151210 } else {
152211 setSelectedOrganizations ( [ ] ) ;
153212 // Reset the auto-select flag when value is cleared (e.g., form reset)
154- setHasAutoSelectedPreferred ( false ) ;
213+ // setHasAutoSelectedPreferred(false);
155214 }
156- } , [ value , currentOrganizations , preferredOrganizations , showAllOrgs ] ) ;
215+ } , [ value , currentOrganizations , showAllOrgs ] ) ;
157216
158217 // Auto-select preferred departments
159218 useEffect ( ( ) => {
@@ -237,23 +296,6 @@ export default function FacilityOrganizationSelector(
237296 setFacilityOrgSearch ( "" ) ;
238297 } ;
239298
240- const handleConfirmSelection = useCallback (
241- ( org : FacilityOrganizationRead ) => {
242- if ( ! selectedOrganizations . includes ( org ) ) {
243- const newSelection = singleSelection
244- ? [ org ]
245- : [ ...selectedOrganizations , org ] ;
246- setSelectedOrganizations ( newSelection ) ;
247- onChange ( newSelection . map ( ( org ) => org . id ) ) ;
248- setAlreadySelected ( true ) ;
249- }
250- setCurrentSelection ( null ) ;
251- setNavigationLevels ( [ ] ) ;
252- setOpen ( false ) ;
253- } ,
254- [ selectedOrganizations , onChange ] ,
255- ) ;
256-
257299 const handleRemoveOrganization = ( index : number ) => {
258300 const newSelection = selectedOrganizations . filter ( ( _ , i ) => i !== index ) ;
259301 setSelectedOrganizations ( newSelection ) ;
@@ -264,8 +306,6 @@ export default function FacilityOrganizationSelector(
264306
265307 const handleOrganizationViewChange = ( value : string ) => {
266308 setShowAllOrgs ( value === "all" ) ;
267- setSelectedOrganizations ( [ ] ) ;
268- setCurrentSelection ( null ) ;
269309 setNavigationLevels ( [ ] ) ;
270310 } ;
271311
@@ -277,53 +317,6 @@ export default function FacilityOrganizationSelector(
277317 }
278318 } ;
279319
280- const getCurrentLevelOrganizations = useCallback ( ( ) => {
281- if ( navigationLevels . length === 0 ) {
282- return rootOrganizations ?. results || [ ] ;
283- }
284- const lastQuery = organizationQueries [ navigationLevels . length - 1 ] ;
285- return lastQuery ?. data ?. results || [ ] ;
286- } , [ navigationLevels , rootOrganizations , organizationQueries ] ) ;
287-
288- // Auto-select when there's only one organization available
289- useEffect ( ( ) => {
290- const availableOrganizations = getCurrentLevelOrganizations ( ) ;
291-
292- // Only auto-select if:
293- // 1. We're at the root level (no navigation levels)
294- // 2. There's exactly one organization
295- // 3. No search is active
296- // 4. No organizations are currently selected
297- // 5. Not loading
298- if (
299- navigationLevels . length === 0 &&
300- availableOrganizations . length === 1 &&
301- ! facilityOrgSearch &&
302- selectedOrganizations . length === 0 &&
303- ! isLoadingRoot
304- ) {
305- const singleOrg = availableOrganizations [ 0 ] ;
306-
307- // Check if this organization is already selected in currentOrganizations prop
308- const isAlreadyInCurrent = currentOrganizations ?. find (
309- ( org ) => org . id === singleOrg . id ,
310- ) ;
311-
312- if ( ! isAlreadyInCurrent && ! props . optional ) {
313- handleConfirmSelection ( singleOrg ) ;
314- }
315- }
316- } , [
317- getCurrentLevelOrganizations ,
318- handleConfirmSelection ,
319- navigationLevels ,
320- facilityOrgSearch ,
321- selectedOrganizations ,
322- isLoadingRoot ,
323- currentOrganizations ,
324- props . optional ,
325- ] ) ;
326-
327320 const renderNavigationPath = ( ) => {
328321 return (
329322 < div className = "flex items-center gap-2 flex-wrap" >
0 commit comments