diff --git a/src/components/BikeMap.vue b/src/components/BikeMap.vue index e0db556..9e3758e 100644 --- a/src/components/BikeMap.vue +++ b/src/components/BikeMap.vue @@ -12,25 +12,39 @@ import { API_BASE_URL, API_LIMIT, MAP_TILELAYER_URL, SpotAccess, SpotType } from import { useQuery } from '@pinia/colada' import { LControlZoom, LMap, LTileLayer } from '@vue-leaflet/vue-leaflet' import BikeFilters from './BikeFilters.vue' -import { computed } from 'vue' +import { computed, ref } from 'vue' import { storeToRefs } from 'pinia' import BikeClusterLayer from './BikeClusterLayer.vue' +import { PhCircleNotch } from '@phosphor-icons/vue' + +const fetchedPages = ref(0) +const totalPages = ref(1) +const progress = computed(() => Math.round((fetchedPages.value / totalPages.value) * 100)) async function fetchAllBikeParkings(): Promise { + fetchedPages.value = 0 + const firstPage = await fetch(`${API_BASE_URL}?limit=${API_LIMIT}`).then(r => r.json()) as ApiResponse const { total_count } = firstPage - const remainingPages = await Promise.all( - Array.from( - { length: Math.ceil((total_count - API_LIMIT) / API_LIMIT) }, - (_, i) => fetch(`${API_BASE_URL}?limit=${API_LIMIT}&offset=${(i + 1) * API_LIMIT}`).then(r => r.json()) as Promise + const remainingCount = Math.ceil((total_count - API_LIMIT) / API_LIMIT) + totalPages.value = remainingCount + 1 + fetchedPages.value = 1 + + const results: BikeParking[] = [...(firstPage.results ?? [])] + + await Promise.all( + Array.from({ length: remainingCount }, (_, i) => + fetch(`${API_BASE_URL}?limit=${API_LIMIT}&offset=${(i + 1) * API_LIMIT}`) + .then(r => r.json() as Promise) + .then(page => { + results.push(...(page.results ?? [])) + fetchedPages.value++ + }) ) ) - return [ - ...(firstPage.results ?? []), - ...remainingPages.flatMap(page => page.results ?? []) - ] + return results } const { state } = useQuery({ @@ -42,25 +56,25 @@ const { state } = useQuery({ const nonCoveredParkings = computed(() => state.value.data?.filter(p => p.type === SpotType.Uncovered) ?? [] ) -const nonCoveredParkingsSpots = computed(() => { - return nonCoveredParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) -}) +const nonCoveredParkingsSpots = computed(() => + nonCoveredParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) +) const coveredTypes = new Set([SpotType.Covered, SpotType.Boxed]) const coveredParkings = computed(() => state.value.data?.filter(p => coveredTypes.has(p.type as SpotType)) ?? [] ) -const coveredParkingsSpots = computed(() => { - return coveredParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) -}) +const coveredParkingsSpots = computed(() => + coveredParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) +) const premiumParkings = computed(() => state.value.data?.filter(p => p.condition_acces === SpotAccess.Korrigo) ?? [] ) -const premiumParkingsSpots = computed(() => { - return premiumParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) -}) +const premiumParkingsSpots = computed(() => + premiumParkings.value.reduce((acc, val) => acc + val.nb_total_place, 0) +) const { zoom, minZoom, center, maxBounds, maxBoundsViscosity, maxClusterRadius, disableClusteringAtZoom, extraOptions } = useMap() const { filterUncovered, filterCovered, filterKorrigo } = storeToRefs(useMap()) @@ -74,21 +88,25 @@ const { filterUncovered, filterCovered, filterKorrigo } = storeToRefs(useMap())
-
Loading...
+
+ Chargement des parkings… {{ progress }}% + + +
+
Error: {{ state.error.message }}
+