From f8060ff8d26eb2345d226143d838108367f5b0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren?= Date: Tue, 18 Jun 2024 11:03:40 +0300 Subject: [PATCH] Fetch city locations only once per website visit - Centralize location fetching in the store, deduplicating API requests, both in code and in network requests - Fix filtered location requests not overwriting previous non-filtered requests: When visiting the main page, the locations for Mannheim were loaded without being filtered to "enabled=true". That then set the first array entry with all locations. When then navigating to Mannheim's page, locations were fetched again, but this time filtered to "enabled=true" and added to the store's array. But when querying the locations for a city from the store, only the first array item matching the city name was returned, which was still the unfiltered list of locations. - Also enable devtools in development (gives you hydration and props warnings in console) - Use new `only_enabled=1` filter added to the Supabase function for getting city locations --- components/CityCard.vue | 26 ++++++---------------- components/block/MapBusiness.vue | 36 +++++++++---------------------- nuxt.config.ts | 2 +- store/store.ts | 37 ++++++++++++++++---------------- 4 files changed, 35 insertions(+), 66 deletions(-) diff --git a/components/CityCard.vue b/components/CityCard.vue index 2169624..db24671 100644 --- a/components/CityCard.vue +++ b/components/CityCard.vue @@ -92,32 +92,18 @@ const cityLink = computed(() => { return useLocale(`/cities/${props.city.name.toLowerCase()}`) }) -// useAsyncData('getLocationsByCity', () => store.getLocationsByCity(props.city.name).then(() => true)) +// useAsyncData('loadLocationsByCity', () => store.loadLocationsByCity(props.city.name).then(() => true)) -interface Locations { - name: string -} -// const data = await $fetch(`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${props.city.name}&apikey=${useRuntimeConfig().public.SUPA_KEY}`) -// store.locations.push({ -// name: props.city.name, -// cityLocations: data as Array -// }) -const getPosts = async () => { - const data = await $fetch(`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${props.city.name}&apikey=${useRuntimeConfig().public.SUPA_KEY}`) - store.locations.push({ - name: props.city.name, - cityLocations: data as Array - }) -} - -// // if you want fetch when component mounts: -onMounted(getPosts) +// Fetch when component mounts: +onMounted(() => { + store.loadLocationsByCity(props.city.name) +}) const locations = computed(() => { const locations = store.getLocations(props.city.name) if (!locations) { return null } const numOfLocations = locations.length - if (numOfLocations.toString().length < 4) { + if (numOfLocations < 1000) { return numOfLocations } else { return Math.round(numOfLocations / 1000) + 'k+' diff --git a/components/block/MapBusiness.vue b/components/block/MapBusiness.vue index b6e0752..9f2b8ba 100644 --- a/components/block/MapBusiness.vue +++ b/components/block/MapBusiness.vue @@ -47,7 +47,7 @@
  • -
    +