Skip to content

Commit 1d28481

Browse files
committed
refactor: router and menu behavior #508
1 parent f628559 commit 1d28481

File tree

10 files changed

+91
-127
lines changed

10 files changed

+91
-127
lines changed

components/Home/Home.vue

+13-41
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,10 @@ import useDevice from '~/composables/useDevice'
3131
import type { ApiAddrSearchResult, ApiSearchResult } from '~/lib/apiSearch'
3232
import IsochroneStatus from '~/components/Isochrone/IsochroneStatus.vue'
3333
34-
//
35-
// Props
36-
//
3734
const props = defineProps<{
3835
boundaryArea?: Polygon | MultiPolygon
3936
}>()
4037
41-
//
42-
// Composables
43-
//
4438
const mapStore = useMapStore()
4539
const { center, isModeFavorites, isModeExplorer, isModeExplorerOrFavorites, mode, selectedFeature, teritorioCluster } = storeToRefs(mapStore)
4640
const menuStore = useMenuStore()
@@ -56,10 +50,8 @@ const router = useRouter()
5650
const device = useDevice()
5751
const { isochroneCurrentFeature } = useIsochrone()
5852
59-
//
60-
// Data
61-
//
62-
const allowRegionBackZoom = ref<boolean>(false)
53+
// CHECK: maybe unused
54+
// const allowRegionBackZoom = ref<boolean>(false)
6355
const isFilterActive = ref<boolean>(false)
6456
const initialBbox = ref<LngLatBounds>()
6557
const isMenuItemOpen = ref<boolean>(false)
@@ -68,9 +60,6 @@ const showFavoritesOverlay = ref<boolean>(false)
6860
const isPoiCardShown = ref<boolean>(false)
6961
const mapFeaturesRef = ref<InstanceType<typeof MapFeatures>>()
7062
71-
//
72-
// Hooks
73-
//
7463
onBeforeMount(async () => {
7564
const favs = getHashPart(router, 'favs')
7665
if (favs) {
@@ -125,9 +114,6 @@ onMounted(async () => {
125114
}
126115
})
127116
128-
//
129-
// Computed
130-
//
131117
const isBottomMenuOpened = computed(() => {
132118
return ((device.value.smallScreen && isPoiCardShown.value) || isMenuItemOpen.value)
133119
})
@@ -203,9 +189,6 @@ const siteName = computed(() => {
203189
return settings!.themes[0]?.title.fr || ''
204190
})
205191
206-
//
207-
// Watchers
208-
//
209192
watch(selectedFeature, (newFeature) => {
210193
isPoiCardShown.value = !!newFeature
211194
@@ -223,23 +206,15 @@ watch(selectedFeature, (newFeature) => {
223206
})
224207
}
225208
}
226-
}, { immediate: true })
209+
})
227210
228-
watch(selectedCategoryIds, async (a, b) => {
229-
if (a !== b) {
211+
watch(selectedCategoryIds, async (newValue, oldValue) => {
212+
if (newValue.toString() !== oldValue.toString()) {
230213
routerPushUrl()
231-
await menuStore.fetchFeatures({
232-
vidoConfig: config!,
233-
categoryIds: selectedCategoryIds.value,
234-
clipingPolygonSlug: route.query.clipingPolygonSlug?.toString(),
235-
})
236-
allowRegionBackZoom.value = true
237214
}
238215
})
239216
240217
watch(mode, () => {
241-
allowRegionBackZoom.value = false
242-
243218
const hash = {
244219
mode: mode.value !== Mode.BROWSER ? mode.value : null,
245220
}
@@ -262,13 +237,10 @@ watch(isModeFavorites, async (isEnabled) => {
262237
}
263238
})
264239
265-
//
266-
// Methods
267-
//
268-
async function goToSelectedFeature(feature?: ApiPoi): Promise<void> {
240+
function goToSelectedFeature(feature?: ApiPoi): void {
269241
if (mapFeaturesRef.value) {
270242
if (feature)
271-
await mapStore.setSelectedFeature(feature)
243+
mapStore.setSelectedFeature(feature)
272244
mapFeaturesRef.value.goToSelectedFeature()
273245
}
274246
}
@@ -337,9 +309,9 @@ function onBottomMenuButtonClick() {
337309
isMenuItemOpen.value = !isMenuItemOpen.value
338310
}
339311
340-
async function onQuitExplorerFavoriteMode(): Promise<void> {
312+
function onQuitExplorerFavoriteMode(): void {
341313
if (mapFeaturesRef.value)
342-
await mapStore.setSelectedFeature()
314+
mapStore.setSelectedFeature()
343315
344316
mode.value = Mode.BROWSER
345317
}
@@ -378,7 +350,7 @@ function routerPushUrl(hashUpdate: { [key: string]: string | null } = {}) {
378350
if (hashUpdate)
379351
hash = setHashParts(hash, hashUpdate)
380352
381-
router.push({
353+
navigateTo({
382354
path: mode.value !== Mode.BROWSER
383355
? '/'
384356
: (categoryIds ? `/${categoryIds}/` : '/') + (id ? `${id}` : ''),
@@ -396,7 +368,7 @@ function toggleExploreAroundSelectedPoi(feature?: ApiPoi) {
396368
isPoiCardShown.value = false
397369
}
398370
else {
399-
allowRegionBackZoom.value = false
371+
// allowRegionBackZoom.value = false
400372
mode.value = Mode.BROWSER
401373
}
402374
}
@@ -428,9 +400,9 @@ function scrollTop() {
428400
header.scrollTop = 0
429401
}
430402
431-
async function handlePoiCardClose(): Promise<void> {
403+
function handlePoiCardClose(): void {
432404
if (mapFeaturesRef.value) {
433-
await mapStore.setSelectedFeature()
405+
mapStore.setSelectedFeature()
434406
}
435407
}
436408
</script>

components/Home/Menu.vue

-3
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,6 @@ export default defineNuxtComponent({
111111
...mapActions(menuStore, [
112112
'addSelectedCategoryIds',
113113
'delSelectedCategoryIds',
114-
'toggleSelectedCategoryId',
115114
]),
116115
117116
getMenuItemByParentId(
@@ -213,7 +212,6 @@ export default defineNuxtComponent({
213212
display-mode-default="compact"
214213
class="tw-flex-1 tw-pointer-events-auto tw-h-full"
215214
@menu-group-click="onMenuGroupClick"
216-
@category-click="toggleSelectedCategoryId($event)"
217215
@filter-click="onCategoryFilterClick"
218216
/>
219217
</component>
@@ -264,7 +262,6 @@ export default defineNuxtComponent({
264262
display-mode-default="large"
265263
class="tw-flex-1 tw-pointer-events-auto tw-h-full"
266264
@menu-group-click="onMenuGroupClick"
267-
@category-click="toggleSelectedCategoryId($event)"
268265
@filter-click="onCategoryFilterClick"
269266
/>
270267
</component>

components/MainMap/MapFeatures.vue

+11-16
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,14 @@ const availableStyles = computed((): MapStyleEnum[] => {
9292
return styles
9393
})
9494
95-
watch(
96-
() => props.features,
97-
() => {
98-
if (!map.value)
99-
return
100-
101-
showVectorSelectedFeature()
102-
renderPois()
103-
handleResetMapZoom(t('snack.noPoi.issue'), t('snack.noPoi.action'))
104-
},
105-
)
95+
watch(() => props.features, () => {
96+
if (!map.value)
97+
return
10698
107-
watch(selectedFeature, () => showVectorSelectedFeature())
99+
showVectorSelectedFeature()
100+
renderPois()
101+
handleResetMapZoom(t('snack.noPoi.issue'), t('snack.noPoi.action'))
102+
})
108103
109104
watch(selectedBackground, () => {
110105
if (!map.value)
@@ -142,7 +137,7 @@ function onMapInit(mapInstance: MapGL): void {
142137
pinMarkerRenderFn: pinMarkerRender,
143138
})
144139
145-
teritorioCluster.value.addEventListener('feature-click', async (e: Event) => await mapStore.setSelectedFeature((e as CustomEvent).detail.selectedFeature))
140+
teritorioCluster.value.addEventListener('feature-click', (e: Event) => mapStore.setSelectedFeature((e as CustomEvent).detail.selectedFeature))
146141
147142
map.value.on('click', onClick)
148143
@@ -194,7 +189,7 @@ function onMapStyleLoad(): void {
194189
}
195190
196191
// Map interactions
197-
async function onClick(e: MapMouseEvent): Promise<void> {
192+
function onClick(e: MapMouseEvent): void {
198193
if (!map.value)
199194
return
200195
@@ -203,8 +198,8 @@ async function onClick(e: MapMouseEvent): Promise<void> {
203198
})
204199
205200
vectorSelectedFeatures.length > 0
206-
? await mapStore.setSelectedFeature(vectorTilesPoi2ApiPoi(vectorSelectedFeatures[0]))
207-
: await mapStore.setSelectedFeature()
201+
? mapStore.setSelectedFeature(vectorTilesPoi2ApiPoi(vectorSelectedFeatures[0]))
202+
: mapStore.setSelectedFeature()
208203
}
209204
210205
function goToSelectedFeature(): void {

components/Menu/Category.vue

+22-4
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ export default defineNuxtComponent({
4444
isFiltered(): boolean {
4545
return this.filters && filterValuesIsSet(this.filters)
4646
},
47+
categoryUrl(): string {
48+
const id = this.category.id.toString()
49+
// Get current categories from the route
50+
const currentCategories = this.$route.params.p1?.toString().split(',') ?? []
51+
52+
// Check if the category is already selected
53+
const categoryIndex = currentCategories.indexOf(id)
54+
55+
if (categoryIndex === -1) {
56+
// Add the category if it doesn't exist
57+
currentCategories.push(id)
58+
}
59+
else {
60+
// Remove the category if it exists
61+
currentCategories.splice(categoryIndex, 1)
62+
}
63+
64+
// Generate the new route
65+
return currentCategories.length ? `/${currentCategories.join(',')}/` : '/'
66+
},
4767
},
4868
4969
emits: {
@@ -59,8 +79,6 @@ export default defineNuxtComponent({
5979
categoryId: this.category.id,
6080
title: this.category.category.name.fr,
6181
})
62-
63-
this.$emit('click', this.category.id)
6482
},
6583
onFilterClick() {
6684
this.$tracking({
@@ -78,14 +96,14 @@ export default defineNuxtComponent({
7896
<template>
7997
<MenuItem
8098
:id="`MenuItem-${category.id}`"
81-
:href="`/${category.id}`"
99+
:href="categoryUrl"
82100
:display-mode="category.category.display_mode || displayModeDefault"
83101
:color-fill="category.category.color_fill"
84102
:icon="category.category.icon"
85103
:size="size"
86104
:name="category.category.name"
87105
badge-class="tw-bg-white tw-text-zinc-700 tw-rounded-full tw-border-solid tw-border-2 tw-border-white"
88-
@click.prevent="onClick"
106+
@click="onClick"
89107
>
90108
<template v-if="category.category.display_mode === 'compact'" #badge>
91109
<FontAwesomeIcon

components/Menu/Group.vue

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export default defineNuxtComponent({
5656
<template>
5757
<MenuItem
5858
:id="`MenuGroup-${menuGroup.id}`"
59-
:href="`/${menuGroup.id}/`"
59+
href=""
6060
:display-mode="menuGroup.menu_group.display_mode || displayModeDefault"
6161
:color-fill="menuGroup.menu_group.color_fill"
6262
:icon="menuGroup.menu_group.icon"
@@ -68,7 +68,7 @@ export default defineNuxtComponent({
6868
size === '2xl' ? 'tw-w-6 tw-h-6' : 'tw-w-5 tw-h-5',
6969
].join(' ')
7070
"
71-
@click.prevent="onClick"
71+
@click="onClick"
7272
>
7373
<template v-if="categoriesActivesCount > 0" #badge>
7474
{{ categoriesActivesCount }}

components/Menu/Item.vue

+7-4
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,13 @@ export default defineNuxtComponent({
5454

5555
<template>
5656
<div class="tw-flex tw-flex-col tw-items-start">
57-
<a
57+
<NuxtLink
5858
:id="id"
59-
:href="href"
60-
target="_blank"
59+
:to="{
60+
path: href,
61+
query: $route.query,
62+
hash: $route.hash,
63+
}"
6164
class="tw-flex focus:tw-outline-none tw-outline-none tw-items-center tw-text-center tw-self-stretch tw-justify-start tw-leading-none tw-transition-colors tw-rounded-lg tw-relative hover:tw-bg-zinc-100" :class="[
6265
displayMode === 'large'
6366
? 'tw-px-4 tw-py-2 tw-col-start-1 tw-col-span-4'
@@ -101,7 +104,7 @@ export default defineNuxtComponent({
101104
class="tw-text-zinc-700 tw-shrink-0"
102105
size="sm"
103106
/>
104-
</a>
107+
</NuxtLink>
105108

106109
<slot name="more" />
107110
</div>

components/Menu/ItemList.vue

+1-9
Original file line numberDiff line numberDiff line change
@@ -44,17 +44,10 @@ export default defineNuxtComponent({
4444
4545
emits: {
4646
menuGroupClick: (_menuItemId: ApiMenuItem['id']) => true,
47-
categoryClick: (_menuItemId: ApiMenuItem['id']) => true,
4847
filterClick: (_categoryId: ApiMenuCategory['id']) => true,
4948
},
5049
5150
methods: {
52-
onMenuGroupClick(menuItem: MenuItem) {
53-
this.$emit('menuGroupClick', menuItem.id)
54-
},
55-
onCategoryClick(menuItem: MenuItem) {
56-
this.$emit('categoryClick', menuItem.id)
57-
},
5851
onFilterClick(categoryId: ApiMenuCategory['id']) {
5952
this.$emit('filterClick', categoryId)
6053
},
@@ -78,7 +71,7 @@ export default defineNuxtComponent({
7871
(menuItem.menu_group.display_mode || displayModeDefault)
7972
=== 'large' && ['tw-col-start-1 tw-col-span-4']
8073
"
81-
@click="onMenuGroupClick(menuItem)"
74+
@click="$emit('menuGroupClick', $event)"
8275
/>
8376
<LinkItem
8477
v-else-if="menuItem.link"
@@ -102,7 +95,6 @@ export default defineNuxtComponent({
10295
(menuItem.category.display_mode || displayModeDefault)
10396
=== 'large' && ['tw-col-start-1 tw-col-span-4']
10497
"
105-
@click="onCategoryClick(menuItem)"
10698
@filter-click="onFilterClick($event)"
10799
/>
108100
</template>

pages/embedded.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ if (error.value)
8787
createError(error.value)
8888
8989
if (status.value === 'success' && data.value)
90-
await mapStore.setSelectedFeature(data.value)
90+
mapStore.setSelectedFeature(data.value)
9191
9292
// Disable Favorite Mode
9393
favoritesModeEnabled.value = false

0 commit comments

Comments
 (0)