Skip to content

Commit

Permalink
[feat] done hiding
Browse files Browse the repository at this point in the history
  • Loading branch information
Neha committed Nov 24, 2024
1 parent cfbcef6 commit 18f9458
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
42 changes: 40 additions & 2 deletions api/maps/AddMarkers.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { useMemo, useState } from 'react';
import React, { useEffect, useMemo, useRef, useState } from 'react';
import ReactDOM from 'react-dom/client';
import { Cluster, MarkerClusterer } from '@googlemaps/markerclusterer';
import { useMap } from '@vis.gl/react-google-maps';
import { useAdvancedMarkerRef, useMap } from '@vis.gl/react-google-maps';

Check failure on line 4 in api/maps/AddMarkers.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint, Prettier, and TypeScript compiler

'useAdvancedMarkerRef' is defined but never used

Check failure on line 4 in api/maps/AddMarkers.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint, Prettier, and TypeScript compiler

'useAdvancedMarkerRef' is defined but never used
import { ClusterIcon } from '@/assets/Clusters/icons';
import ProjectModal from '@/components/ProjectModal';
import { Project } from '../../types/schema';
import { MarkerInfoWindow } from './MarkerInfoWindow';

// REMOVE ^^

export default function AddMarker({
projects,
filteredProjects,
}: {
projects: Project[] | null;
filteredProjects: Project[] | null;
}) {
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
null,
Expand Down Expand Up @@ -129,6 +133,39 @@ export default function AddMarker({
return setClusterer;
}, [map]);

Check warning on line 134 in api/maps/AddMarkers.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint, Prettier, and TypeScript compiler

React Hook useMemo has a missing dependency: 'getMinZoom'. Either include it or remove the dependency array

const markerMap = useRef<Map<number, google.maps.Marker>>(new Map());

const hideMarker = (marker: google.maps.Marker) => {
marker.setMap(null);
};

const showMarker = (marker: google.maps.Marker, map: google.maps.Map) => {
marker.setMap(map);
};

useEffect(() => {
console.log('filteredProjects', filteredProjects);

// Iterate through the filtered projects to update the visibility of each marker
projects?.forEach(project => {
const marker = markerMap.current.get(project.id);
console.log('markerMap', markerMap);
console.log('marker', marker);
if (marker) {
// Check if the project is in the filtered list
const isInFilteredProjects = filteredProjects?.some(
filteredProject => filteredProject.id === project.id,
);

if (isInFilteredProjects && map) {
showMarker(marker, map);
} else {
hideMarker(marker);
}
}
});
}, [filteredProjects, map, projects]);

return (
<>
{projects?.map((project: Project) => {
Expand All @@ -145,6 +182,7 @@ export default function AddMarker({
onMarkerClick={handleMarkerClick}
clusterer={clusterer}
selectedProjectId={selectedProjectId}
markerMap={markerMap.current}
/>
);
})}
Expand Down
3 changes: 3 additions & 0 deletions api/maps/MarkerInfoWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export const MarkerInfoWindow = ({
onMarkerClick,
clusterer,
selectedProjectId,
markerMap,
}: {
position: { lat: number; lng: number };
projectId: number;
Expand All @@ -45,6 +46,7 @@ export const MarkerInfoWindow = ({
) => void;
clusterer: MarkerClusterer | null;
selectedProjectId: number | null;
markerMap: Map<number, google.maps.Marker>;
}) => {
const [markerRef, marker] = useAdvancedMarkerRef();
const [infoWindowShown, setInfoWindowShown] = useState(false);
Expand Down Expand Up @@ -87,6 +89,7 @@ export const MarkerInfoWindow = ({
useEffect(() => {
if (marker && clusterer) {
clusterer.addMarker(marker);
markerMap.set(projectId, marker);
}
});

Expand Down
13 changes: 10 additions & 3 deletions components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { APIProvider, Map as GoogleMap } from '@vis.gl/react-google-maps';
import AddMarkers from '../../api/maps/AddMarkers';
import { Project } from '../../types/schema';
import './styles.css';
import { CSSProperties } from 'react';
import React, { CSSProperties } from 'react';

//REMOVE ^^
const containerStyle: CSSProperties = {
width: '100%',
height: '100%',
Expand All @@ -21,7 +22,10 @@ const center = {

const mapId = '54eb1c7baba5a715'; // needed for AdvancedMarker

export default function Map(props: { projects: Project[] | null }) {
export default function Map(props: {
projects: Project[] | null;
filteredProjects: Project[] | null;
}) {
return (
<APIProvider apiKey={process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY as string}>
<GoogleMap
Expand All @@ -34,7 +38,10 @@ export default function Map(props: { projects: Project[] | null }) {
mapTypeId={'roadmap'}
clickableIcons={false}
>
<AddMarkers projects={props.projects} />
<AddMarkers
projects={props.projects}
filteredProjects={props.filteredProjects}
/>
</GoogleMap>
</APIProvider>
);
Expand Down
4 changes: 2 additions & 2 deletions components/MapViewScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import React, { useEffect, useState } from 'react';
import {
LocationIcon,
ProjectSizeIcon,
Expand Down Expand Up @@ -75,7 +75,7 @@ export default function MapViewScreen({
selectedFilters={selectedFilters}
setSelectedFilters={setSelectedFilters}
/>
<Map projects={projects} />
<Map projects={projects} filteredProjects={filteredProjects} />
<ProjectsListingModal projects={filteredProjects} />
</>
);
Expand Down

0 comments on commit 18f9458

Please sign in to comment.