diff --git a/src/components/Map.vue b/src/components/Map.vue index af40dbb..b023833 100644 --- a/src/components/Map.vue +++ b/src/components/Map.vue @@ -2,25 +2,29 @@ import type { ApiResponse } from '@/types/Api' import type { BikeParking } from '@/types/Bikes' import { API_BASE_URL, API_LIMIT } from '@/utils/const' -import { ref } from 'vue' +import { useQuery } from '@pinia/colada' +async function fetchAllBikeParkings(): Promise { + const firstPage = await fetch(`${API_BASE_URL}?limit=${API_LIMIT}`).then(r => r.json()) as ApiResponse + const { total_count } = firstPage -const res = ref([]) - -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 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 + ) ) -) -res.value = [ - ...(firstPage.results ?? []), - ...remainingPages.flatMap(page => page.results ?? []) -] + return [ + ...(firstPage.results ?? []), + ...remainingPages.flatMap(page => page.results ?? []) + ] +} + +const { state } = useQuery({ + key: ['bike-parkings'], + query: fetchAllBikeParkings, +})