11import { Head , router } from '@inertiajs/react'
2- import { useQuery } from '@tanstack/react-query'
3- import { useState } from 'react'
2+ import { useMutation , useQuery , useQueryClient } from '@tanstack/react-query'
3+ import { useEffect , useState } from 'react'
44import AppLayout from '~/layouts/AppLayout'
55import StyledButton from '~/components/StyledButton'
66import api from '~/lib/api'
@@ -15,6 +15,9 @@ import classNames from 'classnames'
1515
1616type WizardStep = 1 | 2 | 3 | 4
1717
18+ const CURATED_MAP_COLLECTIONS_KEY = 'curated-map-collections'
19+ const CURATED_ZIM_COLLECTIONS_KEY = 'curated-zim-collections'
20+
1821export default function EasySetupWizard ( props : { system : { services : ServiceSlim [ ] } } ) {
1922 const [ currentStep , setCurrentStep ] = useState < WizardStep > ( 1 )
2023 const [ selectedServices , setSelectedServices ] = useState < string [ ] > ( [ ] )
@@ -24,20 +27,21 @@ export default function EasySetupWizard(props: { system: { services: ServiceSlim
2427
2528 const { addNotification } = useNotifications ( )
2629 const { isOnline } = useInternetStatus ( )
30+ const queryClient = useQueryClient ( )
2731
2832 const anySelectionMade =
2933 selectedServices . length > 0 ||
3034 selectedMapCollections . length > 0 ||
3135 selectedZimCollections . length > 0
3236
3337 const { data : mapCollections , isLoading : isLoadingMaps } = useQuery ( {
34- queryKey : [ 'curated-map-collections' ] ,
38+ queryKey : [ CURATED_MAP_COLLECTIONS_KEY ] ,
3539 queryFn : ( ) => api . listCuratedMapCollections ( ) ,
3640 refetchOnWindowFocus : false ,
3741 } )
3842
3943 const { data : zimCollections , isLoading : isLoadingZims } = useQuery ( {
40- queryKey : [ 'curated-zim-collections' ] ,
44+ queryKey : [ CURATED_ZIM_COLLECTIONS_KEY ] ,
4145 queryFn : ( ) => api . listCuratedZimCollections ( ) ,
4246 refetchOnWindowFocus : false ,
4347 } )
@@ -125,6 +129,41 @@ export default function EasySetupWizard(props: { system: { services: ServiceSlim
125129 }
126130 }
127131
132+ const fetchLatestMapCollections = useMutation ( {
133+ mutationFn : ( ) => api . fetchLatestMapCollections ( ) ,
134+ onSuccess : ( ) => {
135+ addNotification ( {
136+ message : 'Successfully fetched the latest map collections.' ,
137+ type : 'success' ,
138+ } )
139+ queryClient . invalidateQueries ( { queryKey : [ CURATED_MAP_COLLECTIONS_KEY ] } )
140+ } ,
141+ } )
142+
143+ const fetchLatestZIMCollections = useMutation ( {
144+ mutationFn : ( ) => api . fetchLatestZimCollections ( ) ,
145+ onSuccess : ( ) => {
146+ addNotification ( {
147+ message : 'Successfully fetched the latest ZIM collections.' ,
148+ type : 'success' ,
149+ } )
150+ queryClient . invalidateQueries ( { queryKey : [ CURATED_ZIM_COLLECTIONS_KEY ] } )
151+ } ,
152+ } )
153+
154+ // Auto-fetch latest collections if the list is empty
155+ useEffect ( ( ) => {
156+ if ( mapCollections && mapCollections . length === 0 && ! fetchLatestMapCollections . isPending ) {
157+ fetchLatestMapCollections . mutate ( )
158+ }
159+ } , [ mapCollections , fetchLatestMapCollections ] )
160+
161+ useEffect ( ( ) => {
162+ if ( zimCollections && zimCollections . length === 0 && ! fetchLatestZIMCollections . isPending ) {
163+ fetchLatestZIMCollections . mutate ( )
164+ }
165+ } , [ zimCollections , fetchLatestZIMCollections ] )
166+
128167 const renderStepIndicator = ( ) => {
129168 const steps = [
130169 { number : 1 , label : 'Apps' } ,
0 commit comments