Resolve "Revert to old previous zoom level when going back from details page of office to overview"
see #14
The changes have been take from https://gitlab.alt.coop/wahlkampf-app/wk-frontend/-/merge_requests/447/diffs, since it really much requires the same changes here.
- User starts at
OfficeOverviewMap
an sees all office - User clicks on an Office
- This triggers the
onBeforeUnmount
since the click on an Office leads to and unmount and forwarding to the zoomed inOfficeDetailMap
- inside of the
onBeforeUnmount
the current zoom level of the overview mapOfficeOverviewMap
is stored inside the uiStore - After
onBeforeUnmount
is executed, the user will be forwarded toOfficeDetailMap
where they can see the single office - If user navigates back to
OfficeOverviewMap
the block inside ofonBeforeMount
is executed, which tries then to load the stored zoomlevel from the uiStore and clears the stored zoom level
Chat.GPT
i also tried to let chat.gpt to explain the diff:
The diff provided shows changes made to the OfficeOverviewMap.vue
file, which is a Vue.js component. Here's an explanation of the changes:
Changes Overview:
-
Import Statements:
-
Modified: Added imports for
onBeforeMount
andonBeforeUnmount
from 'vue'.
-
Modified: Added imports for
-
New Functionality:
-
Added: Code within
onBeforeMount
andonBeforeUnmount
lifecycle hooks to manage the map zoom level usinguiStore
.
-
Added: Code within
Detailed Changes:
1. Import Statements
-
Old Code:
import { onMounted, onUnmounted, ref } from 'vue'
-
New Code:
import { onBeforeMount, onBeforeUnmount, onMounted, onUnmounted, ref } from 'vue'
Explanation: The new code includes additional imports for onBeforeMount
and onBeforeUnmount
lifecycle hooks. These hooks are used for executing code before the component mounts and before it unmounts, respectively.
uiStore
2. Use of -
New Imports:
import { uiStore } from 'src/store/UiStore'
Explanation: The uiStore
is now being imported to manage the user interface state, specifically to store and retrieve the map's zoom level.
onBeforeMount
Lifecycle Hook
3. -
New Code:
onBeforeMount(() => { const previousZoom = uiStore.getState().mapZoom if (previousZoom != null) { map.value.setZoom(previousZoom, {}) } uiStore.setMapZoom(null) })
Explanation: Before the component mounts, the code retrieves the previous zoom level from uiStore
. If a previous zoom level exists, it sets the map's zoom to that level. Then it resets the stored zoom level in uiStore
to null
.
onBeforeUnmount
Lifecycle Hook
4. -
New Code:
onBeforeUnmount(() => { uiStore.setMapZoom(map.value.getZoom()) })
Explanation: Before the component unmounts, the current zoom level of the map is stored in uiStore
.
onMounted
and onUnmounted
Hooks
5. Existing -
Unchanged Code:
onMounted(() => { void apiClient.officeGeometry.list().then((offices) => { officeFeatureCollection.value = offices.payload.data }) map.value.on('zoomend', updateBounds) map.value.on('moveend', updateBounds) }) onUnmounted(() => { map.value.off('zoomend', updateBounds) map.value.off('moveend', updateBounds) })
Explanation: The existing onMounted
and onUnmounted
hooks remain unchanged, continuing to handle API calls and event listeners for the map.
Summary:
The changes introduce a mechanism to persist and restore the map's zoom level using Vue.js lifecycle hooks and a store (uiStore
). This ensures a consistent user experience by retaining the map's zoom level between component mounts and unmounts.