Skip to content

Commit

Permalink
Add kakao maps SDK to the project for displaying project locations on…
Browse files Browse the repository at this point in the history
… a map.
  • Loading branch information
yeoularu committed Oct 19, 2023
1 parent aadf81e commit 0947537
Show file tree
Hide file tree
Showing 10 changed files with 155 additions and 9 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
<script
type="text/javascript"
src="//dapi.kakao.com/v2/maps/sdk.js?appkey=%VITE_KAKAOMAP_API_KEY%&libraries=services"
></script>
</body>
</html>
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
"react": "^18.2.0",
"react-cookie": "^4.1.1",
"react-dom": "^18.2.0",
"react-kakao-maps-sdk": "^1.1.21",
"react-redux": "^8.1.3",
"react-router-dom": "^6.16.0",
"vite-plugin-svgr": "^2.4.0"
Expand All @@ -40,6 +41,7 @@
"eslint": "^8.51.0",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.3.5",
"kakao.maps.d.ts": "^0.1.39",
"msw": "^1.3.2",
"typescript": "^5.2.2",
"vite": "^4.4.11",
Expand Down
21 changes: 21 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 1 addition & 7 deletions src/app/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,13 +258,7 @@ export const api = createApi({
// Project
createProject: build.mutation<
void,
{
name: string;
content: string;
field: string;
startAt: string;
endAt: string;
}
Omit<Project, "projectId" | "likes" | "teamUserIds" | "leaderId">
>({
query: (project) => ({
url: "/projects",
Expand Down
55 changes: 55 additions & 0 deletions src/components/project/MapInfo.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { useRef } from "react";
import { Map, MapMarker, MapTypeControl } from "react-kakao-maps-sdk";

interface MapInfoProps {
coord: [number, number];
setCoord: (coord: [number, number]) => void;
}

export default function MapInfo({ coord, setCoord }: MapInfoProps) {
const mapRef = useRef<kakao.maps.Map>(null);

const handleStop = () => {
const map = mapRef.current;
if (!map) return;

const center = map.getCenter();

setCoord([center.getLat(), center.getLng()]);
};

return (
<div style={{ position: "relative", width: "100%", height: "20rem" }}>
<Map
center={{ lat: 37.63346463214003, lng: 127.07804230975643 }}
style={{
width: "100%",
height: "100%",
position: "absolute",
top: 0,
left: 0,
}}
level={3}
onDragEnd={handleStop}
onZoomChanged={handleStop}
ref={mapRef}
>
<MapTypeControl position={"TOPRIGHT"} />
{coord && <MapMarker position={{ lat: coord[0], lng: coord[1] }} />}
</Map>
<div
style={{
position: "absolute",
top: "50%",
left: "50%",
width: "0.5rem",
height: "0.5rem",
backgroundColor: "#228BF9",
borderRadius: "50%",
transform: "translate(-50%, -50%)",
zIndex: 2,
}}
></div>
</div>
);
}
11 changes: 10 additions & 1 deletion src/mocks/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ const projects = [
likes: 0,
teamUserIds: ["0", "1"],
leaderId: "0",
coordLat: 37.6294393535107,
coordLng: 127.08155702932217,
usersCount: 2,
applicants: ["2", "3"],
},
Expand All @@ -164,6 +166,8 @@ const projects = [
likes: 21,
teamUserIds: ["0", "2", "3"],
leaderId: "2",
coordLat: 37.63538335258811,
coordLng: 127.07861220492845,
usersCount: 3,
applicants: [],
},
Expand All @@ -177,6 +181,8 @@ const projects = [
likes: 21,
teamUserIds: ["2", "3"],
leaderId: "3",
coordLat: 37.63089076860488,
coordLng: 127.08035068598026,
usersCount: 2,
applicants: [],
},
Expand Down Expand Up @@ -527,7 +533,8 @@ export const handlers = [

// Project
rest.post("/api/v1/projects", async (req, res, ctx) => {
const { name, content, field, startAt, endAt } = await req.json();
const { name, content, field, startAt, endAt, coordLat, coordLng } =
await req.json();
if (name === "error") return res(ctx.status(400));

const projectId = projects.at(-1)?.projectId
Expand All @@ -543,6 +550,8 @@ export const handlers = [
likes: 0,
teamUserIds: ["0"],
leaderId: "0",
coordLat,
coordLng,
usersCount: 1,
applicants: [],
});
Expand Down
24 changes: 24 additions & 0 deletions src/pages/NewProject.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
Text,
MultiSelect,
Flex,
Checkbox,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { useDisclosure } from "@mantine/hooks";
Expand All @@ -15,6 +16,8 @@ import { useNavigate } from "react-router-dom";
import { DateInput } from "@mantine/dates";
import dayjs from "dayjs";
import usePage from "hooks/usePage";
import MapInfo from "components/project/MapInfo";
import { useState } from "react";

const data = [
"기계자동차",
Expand All @@ -32,19 +35,29 @@ export default function NewProject() {
const { initPage } = usePage();
const navigate = useNavigate();
const [opened, { open, close }] = useDisclosure(false);
const [coord, setCoord] = useState<[number, number]>([0, 0]);
const form = useForm({
initialValues: {
name: "",
content: "",
field: [],
startAt: "",
endAt: "",
coordLat: 0,
coordLng: 0,
hasLocation: true,
},
validate: {
endAt: (endAt, values) =>
dayjs(values.startAt) > dayjs(endAt)
? "시작일이 종료일보다 늦습니다."
: null,

hasLocation: (hasLocation, values) => {
if (hasLocation) {
return coord[0] === 0 ? "지도를 이동해 위치를 지정해주세요." : null;
}
},
},
});

Expand Down Expand Up @@ -76,6 +89,8 @@ export default function NewProject() {
startAt,
endAt,
field,
coordLat: coord[0],
coordLng: coord[1],
}).unwrap();
initPage("recentProject");
navigate(`/home/recent/project`);
Expand Down Expand Up @@ -122,6 +137,15 @@ export default function NewProject() {
{...form.getInputProps("endAt")}
/>

<Checkbox
my={10}
label="진행 장소"
{...form.getInputProps("hasLocation", { type: "checkbox" })}
/>
<div hidden={!form.values.hasLocation}>
<MapInfo coord={coord} setCoord={setCoord} />
</div>

<Group position="right" mt="md">
<Button type="submit">Submit</Button>
</Group>
Expand Down
35 changes: 35 additions & 0 deletions src/pages/ProjectDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { useDisclosure } from "@mantine/hooks";
import ProjectLike from "components/project/ProjectLike";
import dayjs from "dayjs";
import ApplicantBar from "components/user/ApplicantBar";
import { StaticMap } from "react-kakao-maps-sdk";

const avatars = [
"https://avatars.githubusercontent.com/u/10353856?s=460&u=88394dfd67727327c1f7670a1764dc38a8a24831&v=4",
Expand Down Expand Up @@ -56,6 +57,11 @@ export default function ProjectDetail() {
const startAt = dayjs(project?.startAt);
const dday = Math.floor(today.diff(startAt, "day", true));

const staticMapCoords = {
lat: project?.coordLat || 0,
lng: project?.coordLng || 0,
};

const handleDelete = async () => {
try {
await deleteProject({ id }).unwrap();
Expand Down Expand Up @@ -139,6 +145,35 @@ export default function ProjectDetail() {
</Avatar.Group>
</Group>

{staticMapCoords.lat !== 0 && (
<>
<Text c="dimmed" fz="sm" mt="md">
진행 장소
</Text>
<div
style={{
position: "relative",
width: "100%",
paddingBottom: "56.25%", // This maintains a 1:1 aspect ratio
overflow: "hidden",
}}
>
<StaticMap
center={staticMapCoords}
style={{
position: "absolute",
top: 0,
left: 0,
width: "100%",
height: "100%",
}}
marker={{ position: staticMapCoords }}
level={4}
/>
</div>
</>
)}

{!isTeamMember && (
<Flex justify="right" mt={10}>
<Button onClick={handleApply}>
Expand Down
2 changes: 2 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ interface Project {
likes: number;
teamUserIds: string[];
leaderId: string;
coordLat: number;
coordLng: number;
}

interface User {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"baseUrl": "./src",

/* Types */
"types": ["vite-plugin-svgr/client"]
"types": ["vite-plugin-svgr/client", "kakao.maps.d.ts"]
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
Expand Down

0 comments on commit 0947537

Please sign in to comment.