Skip to content

Commit

Permalink
feat: Update Google Maps API integration in Shelter page component
Browse files Browse the repository at this point in the history
  • Loading branch information
RigottiG committed May 16, 2024
1 parent 5b38091 commit b8138f6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 19 deletions.
3 changes: 1 addition & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ 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 { Icon, type LatLngExpression, type LatLngTuple } from "leaflet";
import { Pin } from "lucide-react";
import { type LatLngTuple } from "leaflet";

function UserLocationMap({ userLocation }: { userLocation: LatLngTuple }) {
const map = useMap();
Expand Down
53 changes: 37 additions & 16 deletions src/app/shelter/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ import { Card as CardBase, CardContent } from "~/components/ui/card";
import axios from "redaxios";
import { env } from "~/env";

type GoogleMapsResponse = {
results: {
geometry: {
location: {
lat: number;
lng: number;
};
};
}[];
};

function Shelter() {
const { shelter } = useShelterContext();
const form = useForm<z.infer<typeof shelterSchema>>({
Expand Down Expand Up @@ -67,23 +78,33 @@ function Shelter() {
const hasModifiedInputs = Object.keys(form.formState.dirtyFields).length > 0;

async function onSubmit(values: z.infer<typeof shelterSchema>) {
const geocode = await axios.get(
`https://maps.googleapis.com/maps/api/geocode/json?address=${values.address.street} ${values.address.number}, ${values.address.city} - ${values.address.state}&key=${env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY}`,
);
const coordinates = geocode.data?.results[0].geometry.location;
const shelter = {
...values,
address: {
...values.address,
latitude: coordinates?.lat,
longitude: coordinates?.lng,
},
};
try {
const { street, number, city, state } = values.address;
const response = await axios.get<GoogleMapsResponse>(
`https://maps.googleapis.com/maps/api/geocode/json`,
{
params: {
address: `${street} ${number}, ${city} - ${state}`,
key: env.NEXT_PUBLIC_GOOGLE_MAPS_API_KEY,
},
},
);

const coordinates = response.data?.results?.[0]?.geometry.location;

const shelter = {
...values,
address: {
...values.address,
latitude: coordinates?.lat,
longitude: coordinates?.lng,
},
};

if (isEditing) {
updateCurrentUserShelter.mutate(shelter);
} else {
createShelter.mutate(shelter);
const mutation = isEditing ? updateCurrentUserShelter : createShelter;
mutation.mutate(shelter);
} catch (error) {
console.error("Error fetching coordinates or submitting shelter:", error);
}
}

Expand Down
1 change: 0 additions & 1 deletion src/app/signin/page.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getProviders } from "next-auth/react";
import { redirect } from "next/navigation";
import { getServerAuthSession } from "~/server/auth";
import { SigninProviderButton } from "./_components/SigninProviderButton";
Expand Down

0 comments on commit b8138f6

Please sign in to comment.