Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] Project Item & Project Modal Functionality Fixes #87

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 10 additions & 20 deletions api/maps/AddMarkers.tsx
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
import { useMemo, useState } from 'react';
import { useMemo } from 'react';
import ReactDOM from 'react-dom/client';
import { Cluster, MarkerClusterer } from '@googlemaps/markerclusterer';
import { useMap } from '@vis.gl/react-google-maps';
import { ClusterIcon } from '@/assets/Clusters/icons';
import ProjectModal from '@/components/ProjectModal';
import { Project } from '../../types/schema';
import { MarkerInfoWindow } from './MarkerInfoWindow';

export default function AddMarker({
projects,
selectedProjectId,
map,
setSelectedProjectId,
setMap,
}: {
projects: Project[] | null;
selectedProjectId: number | null;
map: google.maps.Map | null;
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
setMap: React.Dispatch<React.SetStateAction<google.maps.Map | null>>;
}) {
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
null,
); // track currently open modal

const map = useMap();
setMap(useMap());

const handleMarkerClick = (
projectId: number,
Expand All @@ -28,11 +31,6 @@ export default function AddMarker({
document.title = 'ACE NY';
}
};

const closeModal = () => {
document.title = 'ACE NY';
setSelectedProjectId(null); // close modal
};
/*
function euclideanDistance(point1: number[], point2: number[]): number {
const [x1, y1] = point1;
Expand Down Expand Up @@ -164,14 +162,6 @@ export default function AddMarker({
/>
);
})}

{selectedProjectId && (
<ProjectModal
project_id={selectedProjectId}
closeModal={closeModal}
openFirst={true}
/>
)}
</>
);
}
11 changes: 5 additions & 6 deletions api/maps/MarkerInfoWindow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ export const MarkerInfoWindow = ({
}) => {
const [markerRef, marker] = useAdvancedMarkerRef();
const [infoWindowShown, setInfoWindowShown] = useState(false);
const [modalOpen, setModalOpen] = useState(false);

// open infowindow when marker is hovered and not already open
const handleMarkerEnter = useCallback(() => {
Expand All @@ -59,17 +58,16 @@ export const MarkerInfoWindow = ({

// close infowindow when modal is closed
const handleClose = useCallback(() => {
if (!modalOpen) {
if (selectedProjectId !== projectId) {
setInfoWindowShown(false);
}
}, [modalOpen]);
}, [projectId, selectedProjectId]);

const handleMarkerClick = () => {
onMarkerClick(projectId, position);
setModalOpen(!modalOpen);

// toggle infowindow when marker is clicked
if (!modalOpen) {
if (selectedProjectId === null) {
setInfoWindowShown(true);
} else {
setInfoWindowShown(false);
Expand All @@ -80,7 +78,8 @@ export const MarkerInfoWindow = ({
// close infowindow and modal if new marker is clicked
if (selectedProjectId !== projectId) {
setInfoWindowShown(false);
setModalOpen(false);
} else {
setInfoWindowShown(true);
}
}, [selectedProjectId, projectId]);

Expand Down
23 changes: 0 additions & 23 deletions app/testing/page.tsx

This file was deleted.

22 changes: 20 additions & 2 deletions components/Map/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ const center = {

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

export default function Map(props: { projects: Project[] | null }) {
export default function Map({
projects,
selectedProjectId,
map,
setMap,
setSelectedProjectId,
}: {
projects: Project[] | null;
selectedProjectId: number | null;
map: google.maps.Map | null;
setMap: React.Dispatch<React.SetStateAction<google.maps.Map | null>>;
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
}) {
return (
<APIProvider apiKey={process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY as string}>
<GoogleMap
Expand All @@ -42,7 +54,13 @@ export default function Map(props: { projects: Project[] | null }) {
clickableIcons={false}
minZoom={7}
>
<AddMarkers projects={props.projects} />
<AddMarkers
projects={projects}
map={map}
setMap={setMap}
selectedProjectId={selectedProjectId}
setSelectedProjectId={setSelectedProjectId}
/>
</GoogleMap>
</APIProvider>
);
Expand Down
27 changes: 24 additions & 3 deletions components/MapViewScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useState } from 'react';
import { useMap } from '@vis.gl/react-google-maps';
import {
LocationIcon,
ProjectSizeIcon,
Expand All @@ -10,6 +11,7 @@ import Map from '@/components/Map';
import { SearchBar } from '@/components/SearchBar';
import { Filters, FilterType } from '@/types/schema';
import { Project } from '../../types/schema';
import ProjectModal from '../ProjectModal';
import ProjectsListingModal from '../ProjectsListingModal';

export default function MapViewScreen({
Expand Down Expand Up @@ -43,7 +45,10 @@ export default function MapViewScreen({
icon: <LocationIcon />,
},
];

const [map, setMap] = useState<google.maps.Map | null>(useMap());
const [selectedProjectId, setSelectedProjectId] = useState<number | null>(
null,
);
const [searchTerm, setSearchTerm] = useState('');
const [selectedFilters, setSelectedFilters] = useState<Filters>({
status: [],
Expand Down Expand Up @@ -75,8 +80,24 @@ export default function MapViewScreen({
selectedFilters={selectedFilters}
setSelectedFilters={setSelectedFilters}
/>
<Map projects={projects} />
<ProjectsListingModal projects={filteredProjects} />
<Map
projects={projects}
map={map}
setMap={setMap}
selectedProjectId={selectedProjectId}
setSelectedProjectId={setSelectedProjectId}
/>
<ProjectsListingModal
projects={filteredProjects}
map={map}
setSelectedProjectId={setSelectedProjectId}
/>
{selectedProjectId && (
<ProjectModal
selectedProjectId={selectedProjectId}
setSelectedProjectId={setSelectedProjectId}
/>
)}
</>
);
}
30 changes: 14 additions & 16 deletions components/ProjectItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
import COLORS from '@/styles/colors';
import { Heading2, TagText1 } from '@/styles/texts';
import { Project } from '@/types/schema';
import ProjectModal from '../ProjectModal';
import {
projectImageStyles,
ProjectInfo,
Expand All @@ -35,10 +34,17 @@ import {
StyledProjectItem,
} from './styles';

export default function ProjectItem({ project_id }: { project_id: number }) {
export default function ProjectItem({
project_id,
map,
setSelectedProjectId,
}: {
project_id: number;
map: google.maps.Map | null;
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
}) {
const [project, setProject] = useState<Project | null>(null);
const [defaultImage, setDefaultImage] = useState<string | null>(null);
const [modalOpen, setModalOpen] = useState(false);

useEffect(() => {
queryProjectbyId(project_id).then(data => {
Expand Down Expand Up @@ -69,8 +75,8 @@ export default function ProjectItem({ project_id }: { project_id: number }) {
renewable_energy_technology,
size,
// developer,
// longitude,
// latitude,
longitude,
latitude,
project_status,
// county,
// town,
Expand Down Expand Up @@ -141,19 +147,11 @@ export default function ProjectItem({ project_id }: { project_id: number }) {
};

const handleProjectClick = () => {
setModalOpen(true);
const position = new google.maps.LatLng(latitude ?? 0, longitude ?? 0);
map?.panTo(position);
setSelectedProjectId(project_id);
};

if (modalOpen) {
return (
<ProjectModal
project_id={project_id}
closeModal={() => setModalOpen(false)}
openFirst={true}
/>
);
}

return (
<StyledProjectItem onClick={handleProjectClick}>
<ProjectInfo>
Expand Down
43 changes: 22 additions & 21 deletions components/ProjectModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,22 +37,20 @@ import {
} from './styles';

export default function ProjectModal({
project_id,
closeModal,
openFirst,
selectedProjectId,
setSelectedProjectId,
}: {
project_id: number;
closeModal: () => void;
openFirst: boolean;
selectedProjectId: number | null;
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
}) {
const [project, setProject] = useState<Project | null>(null);
const [defaultImage, setDefaultImage] = useState<string | null>(null);

useEffect(() => {
queryProjectbyId(project_id).then(data => {
queryProjectbyId(selectedProjectId ?? 0).then(data => {
setProject(data);
});
}, [project_id]);
}, [selectedProjectId]);

useEffect(() => {
// Fetch default image when project data is available
Expand Down Expand Up @@ -117,9 +115,14 @@ export default function ProjectModal({
? `${renewable_energy_technology} default image`
: 'No image available';

const closeModal = () => {
document.title = 'ACE NY';
setSelectedProjectId(null); // close modal
};

return (
<Modal
isOpen={openFirst}
isOpen={selectedProjectId !== null}
style={{
overlay: modalOverlayStyles,
content: modalContentStyles,
Expand All @@ -135,7 +138,7 @@ export default function ProjectModal({
/>
<ProjectOverview>
<Developer>
{developer ? <BodyText1>Developer ‣ {developer}</BodyText1> : null}
<BodyText1>{developer ? 'Developer ‣ ' + developer : ''}</BodyText1>
<CloseButton onClick={closeModal}>
<FiX size={20} color="#000" />
</CloseButton>
Expand All @@ -157,17 +160,15 @@ export default function ProjectModal({
</ProjectSize>
<Divider />
<AllKDMS>{KDMs}</AllKDMS>
{additional_information ? (
<AdditionalInfo>
<DetailsContainer>
<BodyText1>DETAILS</BodyText1>
<Divider />
</DetailsContainer>
<AdditionalText>
<BodyText1>{additional_information}</BodyText1>
</AdditionalText>
</AdditionalInfo>
) : null}
<AdditionalInfo>
<DetailsContainer $isEmpty={!additional_information}>
<BodyText1>DETAILS</BodyText1>
<Divider />
</DetailsContainer>
<AdditionalText>
<BodyText1>{additional_information}</BodyText1>
</AdditionalText>
</AdditionalInfo>
</ProjectDetails>
</Modal>
);
Expand Down
3 changes: 2 additions & 1 deletion components/ProjectModal/styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ export const AdditionalInfo = styled.div`
padding: 1.25rem;
`;

export const DetailsContainer = styled.div`
export const DetailsContainer = styled.div<{ $isEmpty: boolean }>`
display: flex;
visibility: ${({ $isEmpty }) => ($isEmpty ? 'hidden' : 'visible')};
align-items: center;
width: 100%;
gap: 0.5rem;
Expand Down
13 changes: 12 additions & 1 deletion components/ProjectsListingModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,22 @@ import {

export default function ProjectsListingModal({
projects,
map,
setSelectedProjectId,
}: {
projects: Project[] | null;
map: google.maps.Map | null;
setSelectedProjectId: React.Dispatch<React.SetStateAction<number | null>>;
}) {
const projectItems = projects?.map((project: Project) => {
return <ProjectItem key={project.id} project_id={project.id} />;
return (
<ProjectItem
key={project.id}
project_id={project.id}
setSelectedProjectId={setSelectedProjectId}
map={map}
/>
);
});

return (
Expand Down