Skip to content

Commit

Permalink
Fix: Apply click event to new marker
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonghun authored and Yonghun committed Feb 21, 2024
1 parent 8056d06 commit be5e7ea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
32 changes: 30 additions & 2 deletions frontend/src/components/AddChinupBarForm/AddChinupBarForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,31 @@ import useInput from "../../hooks/useInput";
import useUploadFormDataStore from "../../store/useUploadFormDataStore";
import type { KakaoMap, KakaoMarker } from "../../types/KakaoMap.types";
import Input from "../Input/Input";
import type { MarkerInfo } from "../Map/Map";
import UploadImage from "../UploadImage/UploadImage";
import * as Styled from "./AddChinupBarForm.style";

interface Props {
setState: React.Dispatch<React.SetStateAction<boolean>>;
setIsMarked: React.Dispatch<React.SetStateAction<boolean>>;
setMarkerInfoModal: React.Dispatch<React.SetStateAction<boolean>>;
setCurrentMarkerInfo: React.Dispatch<React.SetStateAction<MarkerInfo | null>>;
setMarkers: React.Dispatch<React.SetStateAction<KakaoMarker[]>>;
map: KakaoMap | null;
markers: KakaoMarker[];
marker: KakaoMarker | null;
}

const AddChinupBarForm = ({ setState, setIsMarked, map, marker }: Props) => {
const AddChinupBarForm = ({
setState,
setIsMarked,
setMarkerInfoModal,
setCurrentMarkerInfo,
setMarkers,
map,
markers,
marker,
}: Props) => {
const formState = useUploadFormDataStore();

const descriptionValue = useInput("");
Expand All @@ -42,7 +56,7 @@ const AddChinupBarForm = ({ setState, setIsMarked, map, marker }: Props) => {
imageOption
);

new window.kakao.maps.Marker({
const newMarker = new window.kakao.maps.Marker({
map: map,
position: new window.kakao.maps.LatLng(
formState.latitude,
Expand All @@ -52,6 +66,20 @@ const AddChinupBarForm = ({ setState, setIsMarked, map, marker }: Props) => {
image: markerImage2,
});

window.kakao.maps.event.addListener(newMarker, "click", () => {
setMarkerInfoModal(true);
setCurrentMarkerInfo({
...res?.data,
index: markers.length,
});
});

setMarkers((prev) => {
const copy = [...prev];
copy.push(newMarker);
return copy;
});

setState(false);
setIsMarked(false);
marker?.setMap(null);
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/components/Map/Map.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ const Map = () => {
<AddChinupBarForm
setState={setOpenForm}
setIsMarked={setIsMarked}
setMarkerInfoModal={setMarkerInfoModal}
setCurrentMarkerInfo={setCurrentMarkerInfo}
setMarkers={setMarkers}
markers={markers}
map={map}
marker={marker}
/>
Expand Down

0 comments on commit be5e7ea

Please sign in to comment.