Skip to content

Commit

Permalink
fix(pickup): fix map not mounting again after having been unmounted
Browse files Browse the repository at this point in the history
Fixes an issue where the map would not work again after removal.

Updates the `usePickupLocationsMap`-composable to use a global state
where possible and not use memoization to achieve the same.

Additionally fixes an issue where the map could be panned before being
fully loaded.

Fixes: INT-641
  • Loading branch information
FreekVR committed Sep 16, 2024
1 parent 0727e9a commit a41b9ba
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const css = asyncComputed(async () => {
const styleTag = useStyleTag(css);
const scriptTag = useScriptTag('https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.js');
const {initializeMap, activeMarker, center, map} = usePickupLocationsMap();
const {initializeMap, activeMarker, center, map, loaded} = usePickupLocationsMap();
onMounted(async () => {
styleTag.load();
Expand All @@ -43,6 +43,8 @@ onUnmounted(() => {
});
onActivated(() => {
map.value?.panTo(toValue(activeMarker)?.getLatLng() ?? toValue(center));
if (loaded.value) {
map.value?.panTo(toValue(activeMarker)?.getLatLng() ?? toValue(center));
}
});
</script>
115 changes: 59 additions & 56 deletions apps/delivery-options/src/composables/usePickupLocationsMap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {type Ref, ref, computed, watch, toRef, toValue, type ComputedRef} from 'vue';
import {isString} from 'radash';
import {type Map, type TileLayer, type Control} from 'leaflet';
import {isDef, useDebounceFn, useMemoize} from '@vueuse/core';
import {isDef, useDebounceFn} from '@vueuse/core';
import {type MapTileLayerData, ConfigSetting} from '@myparcel-do/shared';
import {type LeafletMapProps, type MapMarker} from '../types';
import {useConfigStore} from '../stores';
Expand All @@ -26,47 +26,64 @@ interface UsePickupLocationsMap {

const LEAFLET_DEFAULT_ZOOM = 14;

// eslint-disable-next-line max-lines-per-function
export const usePickupLocationsMap = useMemoize((): UsePickupLocationsMap => {
const {carriersWithPickup} = useResolvedPickupLocations();
const config = useConfigStore();

/**
* The element that the map is rendered in.
*/
const container = ref<HTMLElement | undefined>();

/**
* The Leaflet map instance.
*/
const map = ref<Map>();

/**
* The Leaflet tile layer instance.
*/
const tileLayer = ref<TileLayer>();

/**
* The markers that are displayed on the map.
*/
const markers = ref<MapMarker[]>([]);
/**
* The Leaflet map instance.
*/
const map = ref<Map>();

/**
* The Leaflet tile layer instance.
*/
const tileLayer = ref<TileLayer>();

/**
* The markers that are displayed on the map.
*/
const markers = ref<MapMarker[]>([]);

/**
* The Leaflet scale control instance.
*/
const scaleControl = ref<Control.Scale>();

/**
* The center of the map.
*/
const center = ref<NonNullable<LeafletMapProps['center']>>([0, 0]);

/**
* The zoom level of the map.
*/
const zoom = ref<NonNullable<LeafletMapProps['zoom']>>(LEAFLET_DEFAULT_ZOOM);

/**
* The element that the map is rendered in.
*/
const container = ref<HTMLElement | undefined>();

const loaded = computed(() => {
return (
Boolean(toValue(container)) &&
Boolean(toValue(map)) &&
Boolean(toValue(tileLayer)) &&
Boolean(toValue(scaleControl))

Check warning on line 69 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L65-L69

Added lines #L65 - L69 were not covered by tests
);
});

Check warning on line 71 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L71

Added line #L71 was not covered by tests

/**
* The Leaflet scale control instance.
*/
const scaleControl = ref<Control.Scale>();
const activeMarker = computed<MapMarker | undefined>(() => {
return toValue(markers).find((marker) => {
const element = marker.getElement();

Check warning on line 75 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L74-L75

Added lines #L74 - L75 were not covered by tests

/**
* The center of the map.
*/
const center = ref<NonNullable<LeafletMapProps['center']>>([0, 0]);
return element?.classList.contains(MAP_MARKER_CLASS_ACTIVE);
}) as MapMarker | undefined;
});

Check warning on line 79 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L77-L79

Added lines #L77 - L79 were not covered by tests

/**
* The zoom level of the map.
*/
const zoom = ref<NonNullable<LeafletMapProps['zoom']>>(LEAFLET_DEFAULT_ZOOM);
const mutableShowLoadMoreButton = ref<boolean>(false);

const mutableShowLoadMoreButton = ref<boolean>(false);
// eslint-disable-next-line max-lines-per-function
export const usePickupLocationsMap = (): UsePickupLocationsMap => {
const {carriersWithPickup} = useResolvedPickupLocations();
const config = useConfigStore();

Check warning on line 86 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L85-L86

Added lines #L85 - L86 were not covered by tests

const showLoadMoreButton = computed({
get() {
Expand Down Expand Up @@ -125,6 +142,9 @@ export const usePickupLocationsMap = useMemoize((): UsePickupLocationsMap => {

return () => {
map.value?.remove();
// Reset any leaflet-bound values to their initial state.
tileLayer.value = undefined;
scaleControl.value = undefined;

Check warning on line 147 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L146-L147

Added lines #L146 - L147 were not covered by tests
unwatchZoom();
};
};
Expand All @@ -144,23 +164,6 @@ export const usePickupLocationsMap = useMemoize((): UsePickupLocationsMap => {
}
}, 100);

const loaded = computed(() => {
return (
Boolean(toValue(container)) &&
Boolean(toValue(map)) &&
Boolean(toValue(tileLayer)) &&
Boolean(toValue(scaleControl))
);
});

const activeMarker = computed<MapMarker | undefined>(() => {
return toValue(markers).find((marker) => {
const element = marker.getElement();

return element?.classList.contains(MAP_MARKER_CLASS_ACTIVE);
}) as MapMarker | undefined;
});

return {
fitBounds,
initializeMap,
Expand All @@ -174,4 +177,4 @@ export const usePickupLocationsMap = useMemoize((): UsePickupLocationsMap => {
showLoadMoreButton,
tileLayer,
};
});
};

Check warning on line 180 in apps/delivery-options/src/composables/usePickupLocationsMap.ts

View check run for this annotation

Codecov / codecov/patch

apps/delivery-options/src/composables/usePickupLocationsMap.ts#L180

Added line #L180 was not covered by tests

0 comments on commit a41b9ba

Please sign in to comment.