-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from emiliosheinz/feat/maps
feat: add maps
- Loading branch information
Showing
17 changed files
with
373 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
prisma/migrations/20240514165705_add_lat_long_to_shelter/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
-- AlterTable | ||
ALTER TABLE "Shelter" ADD COLUMN "latitude" DOUBLE PRECISION, | ||
ADD COLUMN "longitude" DOUBLE PRECISION; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Please do not edit this file manually | ||
# It should be added in your version-control system (i.e. Git) | ||
provider = "postgresql" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"use client"; | ||
import { type LatLngTuple } from "leaflet"; | ||
import { Loader2 } from "lucide-react"; | ||
import dynamic from "next/dynamic"; | ||
import { useEffect, useState } from "react"; | ||
import { api } from "~/trpc/react"; | ||
|
||
const DEFAULT_LOCATION: LatLngTuple = [-30.0346, -51.2177]; // Porto Alegre | ||
|
||
const MapComponent = dynamic(() => import("~/components/map/"), { | ||
loading: () => <Loader2 className="size-8 animate-spin" />, | ||
ssr: false, | ||
}); | ||
|
||
export default function Map() { | ||
const { data, isLoading } = api.shelter.findAll.useQuery(); | ||
const [userLocation, setUserLocation] = | ||
useState<LatLngTuple>(DEFAULT_LOCATION); | ||
|
||
useEffect(() => { | ||
navigator.geolocation.getCurrentPosition( | ||
(position) => { | ||
const { latitude, longitude } = position.coords; | ||
setUserLocation([latitude, longitude]); | ||
}, | ||
(error) => { | ||
console.error("Error getting location: ", error); | ||
}, | ||
); | ||
}, []); | ||
|
||
if (isLoading && !data) { | ||
return ( | ||
<div className="flex w-full justify-center pt-28"> | ||
<Loader2 className="size-8 animate-spin" /> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<main className="flex w-full justify-center pt-8"> | ||
<MapComponent userLocation={userLocation} shelter={data ?? []} /> | ||
</main> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { useEffect } from "react"; | ||
import { MapContainer, TileLayer, Marker, Popup, useMap } from "react-leaflet"; | ||
import "leaflet/dist/leaflet.css"; | ||
import "leaflet-defaulticon-compatibility/dist/leaflet-defaulticon-compatibility.css"; | ||
import "leaflet-defaulticon-compatibility"; | ||
import { type LatLngTuple } from "leaflet"; | ||
import { type Shelter } from "@prisma/client"; | ||
import { Card } from "../card"; | ||
|
||
function UserLocationMap({ userLocation }: { userLocation: LatLngTuple }) { | ||
const map = useMap(); | ||
|
||
useEffect(() => { | ||
if (userLocation) { | ||
map.setView(userLocation, 13); | ||
} | ||
}, [userLocation, map]); | ||
|
||
return null; | ||
} | ||
|
||
export default function Map({ | ||
userLocation, | ||
shelter, | ||
}: { | ||
userLocation: LatLngTuple; | ||
shelter: Shelter[]; | ||
}) { | ||
return ( | ||
<MapContainer | ||
style={{ height: "800px", width: "100%", maxWidth: "1280px" }} | ||
center={userLocation} | ||
zoom={13} | ||
> | ||
<UserLocationMap userLocation={userLocation} /> | ||
<TileLayer | ||
attribution='© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors' | ||
url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" | ||
/> | ||
{shelter?.map((shelter) => { | ||
if (!shelter.latitude || !shelter.longitude) return null; | ||
|
||
return ( | ||
<Marker | ||
key={shelter.id} | ||
position={[shelter.latitude, shelter.longitude]} | ||
> | ||
<Popup minWidth={390}> | ||
<Card | ||
shelter={shelter} | ||
className="border-none p-2 text-black shadow-none" | ||
/> | ||
</Popup> | ||
</Marker> | ||
); | ||
})} | ||
</MapContainer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import axios from "redaxios"; | ||
import { env } from "~/env"; | ||
|
||
type CoordinatesParams = { | ||
street: string; | ||
number: string; | ||
city: string; | ||
state: string; | ||
}; | ||
|
||
type GeocodeResponse = { | ||
results: Array<{ | ||
geometry: { | ||
location: { | ||
lat: number; | ||
lng: number; | ||
}; | ||
}; | ||
}>; | ||
}; | ||
|
||
const coordinates = async ({ | ||
city, | ||
number, | ||
state, | ||
street, | ||
}: CoordinatesParams) => { | ||
const response = await axios.get<GeocodeResponse>( | ||
`https://maps.googleapis.com/maps/api/geocode/json`, | ||
{ | ||
params: { | ||
address: `${street} ${number}, ${city} - ${state}`, | ||
key: env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY, | ||
}, | ||
}, | ||
); | ||
|
||
const { lat, lng } = response.data?.results?.[0]?.geometry.location ?? {}; | ||
|
||
return { lat, lng }; | ||
}; | ||
|
||
export const googleMaps = { | ||
coordinates, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.