From a4943dc4b90084faebbc9c40a1d407b9aff69204 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9=20Viricel?= Date: Mon, 23 Sep 2024 16:47:34 +0200 Subject: [PATCH] chore: improve types on map store + use store subscriber instead of watcher #362 --- components/Home/Embedded.vue | 38 +++++++++++++-- components/Home/Home.vue | 74 ++++++++++++++++++++---------- components/MainMap/MapFeatures.vue | 17 ++++--- pages/embedded.vue | 17 +------ pages/index.vue | 18 +------- stores/map.ts | 29 ++++++------ 6 files changed, 109 insertions(+), 84 deletions(-) diff --git a/components/Home/Embedded.vue b/components/Home/Embedded.vue index d6f52cd0..4683a967 100644 --- a/components/Home/Embedded.vue +++ b/components/Home/Embedded.vue @@ -22,6 +22,7 @@ import { flattenFeatures } from '~/utils/utilities' const props = defineProps<{ boundaryArea?: Polygon | MultiPolygon initialCategoryIds?: number[] + initialPoi?: string }>() // @@ -32,6 +33,7 @@ const { isModeExplorer, mode, selectedFeature } = storeToRefs(mapStore) const menuStore = useMenuStore() const { apiMenuCategory, features, selectedCategoryIds, menuItems } = storeToRefs(menuStore) const { config, settings } = useSiteStore() +const { API_ENDPOINT, API_PROJECT, API_THEME } = config! const route = useRoute() const router = useRouter() const { t } = useI18n() @@ -116,13 +118,16 @@ const poiFilters = computed(() => { ) }) +// Store Subscribers +mapStore.$onAction(({ name, after }) => { + if (name === 'setSelectedFeature') { + after(() => routerPushUrl()) + } +}) + // // Watchers // -watch(selectedFeature, () => { - routerPushUrl() -}) - watch(selectedCategoryIds, (a, b) => { if (a !== b) { routerPushUrl() @@ -152,6 +157,7 @@ function routerPushUrl() { const id = selectedFeature.value?.properties?.metadata?.id?.toString() || selectedFeature.value?.id?.toString() || null + router.push({ path: `/embedded${categoryIds ? `/${categoryIds}/` : '/'}${id ? `${id}` : ''}`, query: router.currentRoute.value.query, @@ -168,6 +174,28 @@ function toggleExploreAroundSelectedPoi() { mode.value = Mode.BROWSER } } + +// Fetch inital POI +const { data, error } = await useFetch( + () => `${API_ENDPOINT}/${API_PROJECT}/${API_THEME}/poi/${props.initialPoi}.geojson`, + { + query: { + geometry_as: 'bbox', + short_description: false, + }, + immediate: !!props.initialPoi, + }, +) + +if (props.initialPoi) { + if (error.value) + throw createError(error.value) + + if (!data.value) + throw createError({ statusCode: 404, message: 'Initial POI not found !' }) + + mapStore.setSelectedFeature(data.value) +}