Skip to content

Commit

Permalink
fix: map responsive crash because context is not present (#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
spaenleh authored Jan 29, 2025
1 parent 5b53d72 commit 66c4754
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 94 deletions.
54 changes: 0 additions & 54 deletions src/modules/builder/map/components/map/InitialSetup.tsx

This file was deleted.

28 changes: 2 additions & 26 deletions src/modules/builder/map/components/map/ItemsMarkers.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type JSX, useRef } from 'react';
import { type JSX } from 'react';
import { FeatureGroup } from 'react-leaflet';
import MarkerClusterGroup from 'react-leaflet-cluster';

Expand All @@ -19,36 +19,12 @@ const ItemsMarkers = ({
lng2: number;
};
}): JSX.Element | JSX.Element[] | undefined => {
const groupRef = useRef<any>(null);
// const map = useMap();
const { useItemsInMap, item } = useQueryClientContext();
const { data: itemGeolocations, isFetching } = useItemsInMap({
...bounds,
parentItemId: item?.id,
keywords: tags,
});
// const [prevState, setPrevState] = useState(itemGeolocations);

// todo: fix this code.
// this use effect causes the storybook tests to hang forever
// useEffect(() => {
// if (!isFetching) {
// if (JSON.stringify(itemGeolocations) !== JSON.stringify(prevState)) {
// // on positive search, focus on items
// if (
// itemGeolocations?.length &&
// itemGeolocations.length !== prevState?.length &&
// tags.length
// ) {
// map.fitBounds(groupRef.current.getBounds());
// }

// // eslint-disable-next-line @eslint-react/hooks-extra/no-direct-set-state-in-use-effect
// setPrevState(itemGeolocations);
// }
// }
// // eslint-disable-next-line react-hooks/exhaustive-deps
// }, [tags, itemGeolocations, isFetching]);

if (isFetching) {
return (
Expand All @@ -68,7 +44,7 @@ const ItemsMarkers = ({
}
// color of clusters is defined by number of markers grouped together
return (
<FeatureGroup ref={groupRef}>
<FeatureGroup>
<MarkerClusterGroup chunkedLoading showCoverageOnHover={false}>
{itemGeolocations?.map((geoloc) => (
<ItemMarker key={geoloc.id} geolocation={geoloc} />
Expand Down
27 changes: 14 additions & 13 deletions src/modules/builder/map/components/topbar/MobileTopBar.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { type JSX, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useMap } from 'react-leaflet';

import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
Expand All @@ -19,10 +18,17 @@ import GeolocationPicker, {
import { useQueryClientContext } from '../context/QueryClientContext';
import Search from './Search';

type Props = { onChange: (tags: string[]) => void; tags: string[] };
type Props = {
onChange: (tags: string[]) => void;
tags: string[];
onChangeOption: GeolocationPickerProps['onChangeOption'];
};

const MobileTopBar = ({ onChange, tags }: Props): JSX.Element => {
const map = useMap();
const MobileTopBar = ({
onChange,
tags,
onChangeOption,
}: Props): JSX.Element => {
const { useSuggestionsForAddress, currentMember } = useQueryClientContext();
const { t } = useTranslation(NS.Map);
const { t: commonT } = useTranslation(NS.Common);
Expand All @@ -36,14 +42,6 @@ const MobileTopBar = ({ onChange, tags }: Props): JSX.Element => {
setOpen(false);
};

const onChangeOption: GeolocationPickerProps['onChangeOption'] = ({
lat,
lng,
}) => {
map.flyTo({ lat, lng }, 10);
setOpen(false);
};

return (
<>
<Fab
Expand All @@ -63,7 +61,10 @@ const MobileTopBar = ({ onChange, tags }: Props): JSX.Element => {
<br />
<GeolocationPicker
useSuggestionsForAddress={useSuggestionsForAddress}
onChangeOption={onChangeOption}
onChangeOption={(v) => {
onChangeOption?.(v);
setOpen(false);
}}
/>
<br />
</>
Expand Down
8 changes: 7 additions & 1 deletion src/modules/builder/map/components/topbar/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ const TopBar = ({
const { isMobile } = useMobileView();

if (isMobile) {
return <MobileTopBar tags={tags} onChange={onChange} />;
return (
<MobileTopBar
tags={tags}
onChange={onChange}
onChangeOption={onChangeOption}
/>
);
}

return (
Expand Down

0 comments on commit 66c4754

Please sign in to comment.