Skip to content

Commit 08d0f88

Browse files
committed
feat: auto-fetch latest curated collections
1 parent 003902b commit 08d0f88

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

admin/inertia/pages/easy-setup/index.tsx

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { 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'
44
import AppLayout from '~/layouts/AppLayout'
55
import StyledButton from '~/components/StyledButton'
66
import api from '~/lib/api'
@@ -15,6 +15,9 @@ import classNames from 'classnames'
1515

1616
type 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+
1821
export 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' },

admin/inertia/pages/settings/maps.tsx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { useModals } from '~/context/ModalContext'
66
import StyledModal from '~/components/StyledModal'
77
import { FileEntry } from '../../../types/files'
88
import { useNotifications } from '~/context/NotificationContext'
9-
import { useState } from 'react'
9+
import { useEffect, useState } from 'react'
1010
import api from '~/lib/api'
1111
import DownloadURLModal from '~/components/DownloadURLModal'
1212
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
@@ -172,6 +172,17 @@ export default function MapsManager(props: {
172172
},
173173
})
174174

175+
// Auto-fetch latest collections if the list is empty
176+
useEffect(() => {
177+
if (
178+
curatedCollections &&
179+
curatedCollections.length === 0 &&
180+
!fetchLatestCollections.isPending
181+
) {
182+
fetchLatestCollections.mutate()
183+
}
184+
}, [curatedCollections, fetchLatestCollections])
185+
175186
return (
176187
<SettingsLayout>
177188
<Head title="Maps Manager" />

admin/inertia/pages/settings/zim/remote-explorer.tsx

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ export default function ZimRemoteExplorer() {
191191
},
192192
})
193193

194+
// Auto-fetch latest collections if the list is empty
195+
useEffect(() => {
196+
if (curatedCollections && curatedCollections.length === 0 && !fetchLatestCollections.isPending) {
197+
fetchLatestCollections.mutate()
198+
}
199+
}, [curatedCollections, fetchLatestCollections])
200+
194201
return (
195202
<SettingsLayout>
196203
<Head title="ZIM Remote Explorer | Project N.O.M.A.D." />

0 commit comments

Comments
 (0)