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] Populate the map with project markers #13

Merged
merged 6 commits into from
Oct 18, 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
16 changes: 16 additions & 0 deletions api/maps/addMarker.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Marker } from '@react-google-maps/api';
import { Project } from '../../types/schema';

export default function addMarker(projects: Project[] | null) {
return projects?.map(project => {
return project.approved ? (
<Marker
key={project.id}
position={{
lat: project.latitude,
lng: project.longitude,
}}
/>
) : null;
});
}
7 changes: 5 additions & 2 deletions api/supabase/queries/query.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Project } from '../../../types/schema';
import supabase from '../createClient';

export async function queryProjects() {
const { data: projects, error } = await supabase.from('Projects').select('*');
export default async function queryProjects() {
const { data: projects, error } = await supabase
.from('Projects')
.select('*')
.eq('approved', true);

console.log('PROJECTS', projects, 'ERROR', error);

Expand Down
24 changes: 22 additions & 2 deletions app/page.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
import { CSSProperties } from 'react';
'use client';

import { CSSProperties, useEffect, useState } from 'react';
import Map from '@/components/Map/map';
import ProjectModal from '@/components/ProjectModal';
import queryProjects from '../api/supabase/queries/query';
import { Project } from '../types/schema';

export default function Home() {
const [projects, setProjects] = useState<Project[] | null>(null);
const [error, setError] = useState<string | null>(null);

useEffect(() => {
queryProjects()
.then(data => {
setProjects(data.projects);
})
.catch(err => setError(err));
}, []);

return (
<main style={mainStyles}>
<ProjectModal project_id={1}></ProjectModal>
<Map />
{error ? <div style={errorStyles}>{error}</div> : null}
{projects ? <Map projects={projects} /> : null}
</main>
);
}
Expand All @@ -21,3 +37,7 @@ const mainStyles: CSSProperties = {
alignItems: 'center',
justifyContent: 'center',
};

const errorStyles: CSSProperties = {
color: '#D22B2B',
};
24 changes: 4 additions & 20 deletions app/testing/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,9 @@
import { CSSProperties, useEffect, useState } from 'react';
import Image from 'next/image';
import BPLogo from '@/assets/images/bp-logo.png';
import { queryProjects } from '../../api/supabase/queries/query';

interface Project {
id: string;
project_name: string;
energy_category: string;
size: number;
developer: string;
longitude: number;
latitude: number;
project_statues: string;
county: string;
town: string;
region: string;
state_senate_district: number;
assembly_district: number;
project_image: string | null;
additional_information: string | null;
key_development_milestones: object | null;
}
import queryProjects from '../../api/supabase/queries/query';
import Map from '../../components/Map/map';
import { Project } from '../../types/schema';

export default function Home() {
const [projects, setProjects] = useState<Project[] | null>(null);
Expand Down Expand Up @@ -51,6 +34,7 @@ export default function Home() {
<div>Loading...</div>
)}
{error ? <div style={errorStyles}>{error}</div> : null}
{projects ? <Map projects={projects} /> : null}
</main>
);
}
Expand Down
12 changes: 8 additions & 4 deletions components/Map/map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import { useCallback, useState } from 'react';
import { GoogleMap, useJsApiLoader } from '@react-google-maps/api';
import addMarker from '../../api/maps/addMarker';
import { Project } from '../../types/schema';

const containerStyle = {
width: '700px',
height: '700px',
};

const center = {
lat: 40.7128,
lng: -74.006,
lat: 43.0481,
lng: -76.1474,
};

const zoom = 7;

export default function Map() {
export default function Map(props: { projects: Project[] | null }) {
const { isLoaded } = useJsApiLoader({
id: 'google-map-script',
googleMapsApiKey: process.env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY as string,
Expand All @@ -39,7 +41,9 @@ export default function Map() {
mapContainerStyle={containerStyle}
onLoad={onLoad}
onUnmount={onUnmount}
></GoogleMap>
>
{addMarker(props.projects)}
</GoogleMap>
) : (
<></>
);
Expand Down