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

add pin #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion src/api/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export const Y_COORD = "y";
export const GROUP = "group";
export const USER_ID = "userId";
export const LAT = "lat";
export const LONG = "long";
export const LNG = "lng";
export const TITLE = "title";
export const USERNAME = "username";
2 changes: 1 addition & 1 deletion src/api/methods/pins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export function createPin(pin: Pin) {
.set({
description: pin.description,
lat: pin.lat,
long: pin.long,
lng: pin.lng,
creator: pin.creator,
});
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions src/api/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TITLE,
USER_ID,
LAT,
LONG,
LNG,
CREATOR,
} from "../constants";

Expand All @@ -24,7 +24,7 @@ export type Pin = {
[TITLE]: string;
[DESCRIPTION]: string;
[LAT]: number;
[LONG]: number;
[LNG]: number;
[GROUP_NAME]: string;
[CREATOR]: User;
};
132 changes: 107 additions & 25 deletions src/components/map/map.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { useState, useCallback, memo } from "react";
import { useState, useCallback, memo, useRef, useContext } from "react";
import {
GoogleMap,
useJsApiLoader,
MarkerClusterer,
Marker,
} from "@react-google-maps/api";
import { Button, Form, Modal, TextArea } from "semantic-ui-react";
import useGeolocation from "../../custom-hooks/use-geolocation";
import PlaceholderWrapper from "../placeholder-wrapper";
import styles from "./map.module.scss";
import { createPin } from "../../api/methods/pins";
import { AuthContext } from "../../context/AuthContext";
import { User } from "../../api/models";
import { GroupContext } from "../../context-provider/group-provider";

const DEFAULT_CENTER: google.maps.LatLngLiteral = {
lat: 1.2949,
Expand Down Expand Up @@ -35,10 +40,82 @@ function Map() {
googleMapsApiKey: `${process.env.REACT_APP_GOOGLE_API_KEY}`,
});
const [map, setMap] = useState<google.maps.Map>();
const [isModalOpen, setModalOpen] = useState(false);
const [currLatLong, setCurrLatLong] = useState(DEFAULT_CENTER);

const inputRef = useRef<HTMLInputElement>();
const descRef = useRef<HTMLTextAreaElement>();
const { user } = useContext(AuthContext);
const { selectedGroup } = useContext(GroupContext);

const onUnmount = useCallback(() => setMap(undefined), []);

const openModal = ({ latLng }: google.maps.MapMouseEvent) => {
if (latLng?.lat && latLng.lng) {
setCurrLatLong({ lat: latLng.lat(), lng: latLng.lng() });
} else {
alert("cannot get coordinates.");
}
setModalOpen(true);
};

const closeModal = () => {
setModalOpen(false);
};

const addPin = () => {
const newtitle = inputRef?.current?.value;
const desc = descRef?.current?.value;

if (newtitle && desc) {
const currentUser: User = {
userId: user?.user.uid ?? "",
name: user?.name ?? "",
};
const newPin = {
title: newtitle,
description: desc,
...currLatLong,
creator: currentUser,
group_name: "",
};
createPin(newPin);
closeModal();
} else {
alert("Fields must be entered.");
}
};

return (
<>
<Modal size="tiny" open={isModalOpen}>
<Modal.Header>
Adding a pin to {currLatLong.lat}, {currLatLong.lng}
</Modal.Header>
<Modal.Content>
<Form.Field>
<Form.Input
input={{ ref: inputRef }}
size="large"
placeholder="Title"
/>
</Form.Field>
<Form.Field>
<Form.Input
input={{ ref: descRef }}
rows="3"
placeholder="Description ..."
/>
</Form.Field>
</Modal.Content>
<Modal.Actions>
<Button onClick={closeModal}>Cancel</Button>
<Button positive type="submit" onClick={addPin}>
Start
</Button>
</Modal.Actions>
</Modal>

<PlaceholderWrapper
className={styles.placeholder}
placeholder
Expand All @@ -49,31 +126,36 @@ function Map() {
loadError?.message ?? "An error has occurred while loading the map"
}
>
<GoogleMap
mapContainerClassName={styles.map}
onLoad={setMap}
onUnmount={onUnmount}
zoom={DEFAULT_ZOOM}
center={
geolocation
? { lat: geolocation.latitude, lng: geolocation.longitude }
: DEFAULT_CENTER
}
>
<MarkerClusterer options={MARKER_CLUSTERER_OPTIONS}>
{(clusterer) =>
locations.map((location, index) => (
<Marker
key={index}
position={location}
label={labels[index % labels.length]}
clusterer={clusterer}
/>
))
<GoogleMap
mapContainerClassName={styles.map}
onLoad={setMap}
onUnmount={onUnmount}
onClick={openModal}
zoom={DEFAULT_ZOOM}
center={
geolocation
? { lat: geolocation.latitude, lng: geolocation.longitude }
: DEFAULT_CENTER
}
</MarkerClusterer>
</GoogleMap>
</PlaceholderWrapper>
>
{selectedGroup && (
<MarkerClusterer options={MARKER_CLUSTERER_OPTIONS}>
{(clusterer) =>
Object.values(selectedGroup.pins).map(({ lat, lng }, index) => (
<Marker
key={index}
position={{ lat, lng }}
label={labels[index % labels.length]}
clusterer={clusterer}
/>
))
}
</MarkerClusterer>
)}
<Marker position={currLatLong} />
</GoogleMap>
</PlaceholderWrapper>
</>
);
}

Expand Down