Skip to content

Commit ee75e5b

Browse files
Merge pull request #5 from cryptocity-network/soeren/locations
Fetch city locations only once per website visit
2 parents cfad753 + f8060ff commit ee75e5b

4 files changed

Lines changed: 35 additions & 66 deletions

File tree

components/CityCard.vue

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,18 @@ const cityLink = computed(() => {
9292
return useLocale(`/cities/${props.city.name.toLowerCase()}`)
9393
})
9494
95-
// useAsyncData('getLocationsByCity', () => store.getLocationsByCity(props.city.name).then(() => true))
95+
// useAsyncData('loadLocationsByCity', () => store.loadLocationsByCity(props.city.name).then(() => true))
9696
97-
interface Locations {
98-
name: string
99-
}
100-
// const data = await $fetch(`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${props.city.name}&apikey=${useRuntimeConfig().public.SUPA_KEY}`)
101-
// store.locations.push({
102-
// name: props.city.name,
103-
// cityLocations: data as Array<Locations>
104-
// })
105-
const getPosts = async () => {
106-
const data = await $fetch(`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${props.city.name}&apikey=${useRuntimeConfig().public.SUPA_KEY}`)
107-
store.locations.push({
108-
name: props.city.name,
109-
cityLocations: data as Array<Locations>
110-
})
111-
}
112-
113-
// // if you want fetch when component mounts:
114-
onMounted(getPosts)
97+
// Fetch when component mounts:
98+
onMounted(() => {
99+
store.loadLocationsByCity(props.city.name)
100+
})
115101
116102
const locations = computed(() => {
117103
const locations = store.getLocations(props.city.name)
118104
if (!locations) { return null }
119105
const numOfLocations = locations.length
120-
if (numOfLocations.toString().length < 4) {
106+
if (numOfLocations < 1000) {
121107
return numOfLocations
122108
} else {
123109
return Math.round(numOfLocations / 1000) + 'k+'

components/block/MapBusiness.vue

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
</li>
4848
<transition-group name="fade" mode="out-in">
4949
<li
50-
v-for="(location) in locations"
50+
v-for="location in locations"
5151
:key="`card-${location.name}`"
5252
ref="slides"
5353
class="w-[clamp(320px,370px,calc(100vw-40px))] shrink-0 snap-center snap-always"
@@ -79,7 +79,7 @@
7979
</svg>
8080
</button>
8181
<button
82-
v-if="activeIndex < locations?.length - visibleCards"
82+
v-if="activeIndex < (locations?.length || 0) - visibleCards"
8383
class="hocus:bg-blue-dark/30 absolute right-32 top-1/2 z-10 -mt-24 hidden size-48 cursor-pointer items-center justify-center rounded bg-blue-dark/20 text-white transition-[background-color] active:bg-blue-dark/40 sm:flex"
8484
@click="goToNext"
8585
>
@@ -92,7 +92,7 @@
9292
</button>
9393
</div>
9494
</div>
95-
<div v-if="visibleCards < locations?.length" class="flex flex-col">
95+
<div v-if="visibleCards < (locations?.length || 0)" class="flex flex-col">
9696
<div class="relative mx-auto mt-8 flex">
9797
<button
9898
v-for="(_, i) in locations"
@@ -141,30 +141,14 @@ const cityName = computed(() => {
141141
return name.charAt(0).toUpperCase() + name.slice(1)
142142
})
143143
144-
// await useAsyncData('getLocationsByCity', () => store.getLocationsByCity(cityName.value).then(() => true),
145-
// {
146-
// lazy: true
147-
// })
148-
interface LocationItem {
149-
address: string
150-
category: string
151-
enabled: boolean
152-
gmaps: string
153-
name: string
154-
photo: string
155-
id: number
156-
}
157-
const getPosts = async () => {
158-
const data: Array<LocationItem> = await $fetch(`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${cityName.value}&apikey=${useRuntimeConfig().public.SUPA_KEY}`)
159-
const filteredData = data.filter((location: LocationItem) => location.enabled === true)
160-
store.locations.push({
161-
name: cityName.value,
162-
cityLocations: filteredData as Array<LocationItem>
163-
})
164-
}
144+
// await useAsyncData('loadLocationsByCity', () => store.loadLocationsByCity(cityName.value).then(() => true), {
145+
// lazy: true
146+
// })
165147
166-
// // if you want fetch when component mounts:
167-
onMounted(getPosts)
148+
// Fetch when component mounts:
149+
onMounted(() => {
150+
store.loadLocationsByCity(cityName.value)
151+
})
168152
169153
const locations = computed(() => {
170154
return store.getLocations(cityName.value)

nuxt.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// https://nuxt.com/docs/api/configuration/nuxt-config
22
export default defineNuxtConfig({
33
devtools: {
4-
enabled: false
4+
enabled: true,
55
},
66
nitro: {
77
compressPublicAssets: {

store/store.ts

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import type { Page } from '@/types/dato-models/Page'
88
import type { Region } from '@/types/dato-models/Region'
99
import type { Global } from '@/types/dato-models/Global'
1010

11-
interface Locations {
11+
interface Location {
1212
name: string
13-
}
14-
interface CityLocations {
15-
name: string,
16-
cityLocations: Array<Locations>
13+
gmaps: string
14+
photo: string
15+
rating: number
16+
address: string
17+
enabled: boolean
18+
category: string
1719
}
1820
export const useWebsiteStore = defineStore('websiteStore', {
1921
state: () => {
@@ -26,15 +28,15 @@ export const useWebsiteStore = defineStore('websiteStore', {
2628
siteLocales: undefined as Array<string> | undefined,
2729
translations: undefined as JSON | undefined
2830
},
29-
locations: [] as Array<CityLocations>
31+
locations: new Map<string, Location[]>()
3032
}
3133
},
3234
getters: {
3335
getGlobalData (state): Global | null { return state.global },
3436
getCurrentRegion (state): Region | null { return state.region },
3537
getPages (state): Array<Page> | null { return state.pages },
36-
getLocations (state): any {
37-
return (cityName: string) => state.locations.find(x => x.name === cityName)?.cityLocations || null
38+
getLocations (state): (cityName: string) => (Location[] | null) {
39+
return (cityName: string) => (state.locations.get(cityName) || null)
3840
}
3941
},
4042
actions: {
@@ -131,18 +133,15 @@ export const useWebsiteStore = defineStore('websiteStore', {
131133
this.localization.siteLocales = body.region._locales
132134
}
133135
},
134-
async getLocationsByCity (cityName: string) {
135-
if (this.locations.find(x => x.name === cityName)) { return }
136-
const { data: { value: response } } = await useFetch(
137-
`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${cityName}&apikey=${useRuntimeConfig().public.SUPA_KEY}`) as AsyncData<Array<Locations>, RTCError>
138-
if (response) {
139-
this.locations.push(
140-
{
141-
name: cityName,
142-
cityLocations: response as Array<Locations>
143-
}
144-
)
136+
async loadLocationsByCity (cityName: string) {
137+
if (this.locations.has(cityName)) {
138+
console.debug("Already have locations for city", cityName, this.locations.get(cityName))
139+
return
145140
}
141+
console.debug("Fetching locations for city", cityName)
142+
const locations = await $fetch<Array<Location>>(
143+
`https://mycbdmurjytbdahjljoh.supabase.co/rest/v1/rpc/get_cryptocity_locations?cryptocity_name=${cityName}&only_enabled=1&apikey=${useRuntimeConfig().public.SUPA_KEY}`)
144+
this.locations.set(cityName, locations)
146145
}
147146
}
148147
})

0 commit comments

Comments
 (0)