Skip to content

About

๐Ÿ”ต๐ŸŸฃ๐ŸŸกSuperCoding_Project(3)_Restaurant Recommendation Service

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

ย 

History

27 Commits

Folders and files

NameName
Last commit message
Last commit date
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

refactoring ์ค‘์ž…๋‹ˆ๋‹ค.

Main page ๋””์ž์ธ ์ˆ˜์ •๊ณผ ํ•จ๊ป˜ ๊ธฐ๋Šฅ ์ถ”๊ฐ€ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

๐Ÿด ๋˜๊ฐ„์ง€๋„ ( ๋ง›์ง‘ ์†Œ๊ฐœ ์„œ๋น„์Šค)

  • ํ”„๋กœ์ ํŠธ ๊ธฐ๊ฐ„ : 2024/01 ~ 2024/02
  • ํ”„๋กœ์ ํŠธ ์ธ์› : ํ”„๋ก ํŠธ์—”๋“œ 5๋ช…, ๋ฒก์—”๋“œ 9๋ช…

โœ… ๋ชฉํ‘œ ๋ฐ ์†Œ๊ฐœ ( ํ”„๋ก ํŠธ์—”๋“œ ์ง€๋„ ํŽ˜์ด์ง€ ๋‹ด๋‹น )

  • Kakao Map API๋ฅผ ํ™œ์šฉํ•˜์—ฌ 'ํ‚ค์›Œ๋“œ๋กœ ์žฅ์†Œ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋ชฉ๋ก์œผ๋กœ ํ‘œ์ถœํ•˜๊ธฐ' ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„
  • useState ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ง›์ง‘ ์ •๋ณด์™€ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ๋ฅผ ๊ด€๋ฆฌ
  • useEffect๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ฒ€์ƒ‰์–ด๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ๋งˆ๋‹ค ์ง€๋„๋ฅผ ๊ฐฑ์‹ ํ•˜๊ณ  ๋ง›์ง‘ ์ •๋ณด ๊ด€๋ฆฌ
  • ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ์ง€๋„์— ํ‘œ์‹œํ•˜๊ณ  ํŽ˜์ด์ง€๋„ค์ด์…˜์„ ๊ตฌํ˜„
  • ๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅ์ด ๋ฐœ์ƒํ• ๋•Œ๋งˆ๋‹ค handleInputChange ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ ์—…๋ฐ์ดํŠธ
  • JSX๋ฅผ ํ†ตํ•ด UI๋ฅผ ๋ Œ๋”๋ง ํ•˜๊ณ  ์Šคํƒ€์ผ๋ง์€ styled-components๋ฅผ ์ด์šฉํ•˜์—ฌ ์ ์šฉย ย 


0211PJT ํ”„๋กœ์ ํŠธ ๊ฐœ์ธ ํด๋” ์ƒ์„ฑํ•˜์—ฌ ๋ฏธ๋ฆฌ ํ…Œ์ŠคํŠธ ํ›„ ํ”„๋ก ํŠธ์—”๋“œ ํ”„๋กœ์ ํŠธ์— ์˜ฎ๊ธด ํ›„ ๋‹ค์‹œ ํ…Œ์ŠคํŠธ ํ•ด๋ณด๋Š” ์ž‘์—… ์ง„ํ–‰

SuperCoding PJT03 Figma

image

$ npm install typescript @types/node @types/react @types/react-dom @types/jest

โ—พ TypeScript์™€ ๊ด€๋ จ ํƒ€์ž… ์ •์˜ ํŒŒ์ผ์„ ์„ค์น˜

TypeScript๋กœ ์ž‘์„ฑ๋œ ์ฝ”๋“œ์—์„œ ๊ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ TypeScript ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ํƒ€์ž… ์ฒดํฌ๋ฅผ ์ˆ˜ํ–‰ ๊ฐ€๋Šฅ

TypeScript ๋ฐ ๊ด€๋ จ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ํ”„๋กœ์ ํŠธ์— ์„ค์น˜

ํ•ด๋‹น ํŒจํ‚ค์ง€๋“ค์ด ํ”„๋กœ์ ํŠธ์˜ node_modules ํด๋”์— ์„ค์น˜

$ npx create-react-app mapmain โ€”template typescript

โ—พ TypeScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑ

mapmain์€ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„์ด๋ฉฐ --template typescript๋Š” TypeScript๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑ

React ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๊ธฐ๋ณธ ๊ตฌ์กฐ์™€ TypeScript ๊ตฌ์„ฑ์ด ์„ค์ •๋œ ํ”„๋กœ์ ํŠธ๊ฐ€ ์ƒ์„ฑ

$ npm install --save @types/kakaomaps
$ npm i --save-dev @types/styled-components

typescript์—์„œ ์˜ค๋ฅ˜์—†์ด ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” styled-component์˜ type๋“ค์„ ๊ฐ€์ง€๊ณ  ์™€์•ผํ•œ๋‹ค.

$ npm install react-icons --save
import { FaMapLocationDot } from "react-icons/fa6";
...
<FaMapLocationDot />
...

https://react-icons.github.io/react-icons/icons/fa6/

  • ๊ธฐ์กด ํ”„๋กœ์ ํŠธ์—์„œ ์ถ”๊ฐ€ ์„ค์น˜
   "@fortawesome/react-fontawesome": "^0.2.0",
    "@types/kakaomaps": "^1.1.5",
    "react-icon": "^1.0.0",
    "react-kakao-maps-sdk": "^1.1.26"

image

โœ… ์ „์ฒด ์ฝ”๋“œ

import React, { useEffect, useState } from "react";
import styled from "styled-components";
import { FaMapLocationDot } from "react-icons/fa6";
import MapGuide from "./MapGuide";

declare global {
  interface Window {
    kakao: any;
  }
}

interface Pagination {
  last: number;
  current: number;
  gotoPage: (pageNumber: number) => void;
}

interface Place {
  x: number;
  y: number;
  place_name: string;
  road_address_name?: string;
  // address_name?: string;
  phone?: number;
}

// MapContainer ์ปดํฌ๋„ŒํŠธ ์ •์˜
const MapContainer: React.FC = () => {
  // useState ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ง›์ง‘ ์ •๋ณด places ์™€ ๊ฒ€์ƒ‰ ํ‚ค์›Œ๋“œ searchKeyword ๊ด€๋ฆฌ
  const [places, setPlaces] = useState<Place[]>([]);
  const [searchKeyword, setSearchKeyword] =
    useState<string>("๋ง›์ง‘์„ ๊ฒ€์ƒ‰ํ•ด๋ณด์„ธ์š”");

  useEffect(() => {
    const infowindow = new window.kakao.maps.InfoWindow({ zIndex: 1 });
    // const markers = [];
    const mapOption = {
      center: new window.kakao.maps.LatLng(37.566826, 126.9786567),
      level: 3,
    };

    const map = new window.kakao.maps.Map(
      document.getElementById("map"),
      mapOption
    );
    const ps = new window.kakao.maps.services.Places();

    ps.keywordSearch(searchKeyword, placesSearchCB);

    function placesSearchCB(data: any[], status: any, pagination: any) {
      if (status === window.kakao.maps.services.Status.OK) {
        const bounds = new window.kakao.maps.LatLngBounds();
        setPlaces(data);

        for (let i = 0; i < data.length; i++) {
          displayMarker(data[i]);
          bounds.extend(new window.kakao.maps.LatLng(data[i].y, data[i].x));
        }

        map.setBounds(bounds);
        displayPagination(pagination);
        setPlaces(data);
      }
    }

    function displayPagination(pagination: Pagination): void {
      const paginationEl = document.getElementById("pagination");
      const fragment = document.createDocumentFragment();

      while (paginationEl?.hasChildNodes()) {
        paginationEl.removeChild(paginationEl.lastChild!);
      }

      for (let i = 1; i <= pagination.last; i++) {
        const el = document.createElement("a");
        el.href = "#";
        el.innerHTML = i.toString();

        if (i === pagination.current) {
          el.className = "on";
        } else {
          el.onclick = (function (pageNumber: number) {
            return function () {
              pagination.gotoPage(pageNumber);
            };
          })(i);
        }

        fragment.appendChild(el);
      }

      paginationEl?.appendChild(fragment);
    }

    function displayMarker(place: Place) {
      const marker = new window.kakao.maps.Marker({
        map: map,
        position: new window.kakao.maps.LatLng(place.y, place.x),
      });

      window.kakao.maps.event.addListener(marker, "click", function () {
        infowindow.setContent(
          '<div style="text-decoration:none; padding:5px; text-align:center; font-size:12px; font-weight:bold;">' +
            '<a href="https://map.kakao.com/link/map/' +
            place.place_name +
            "," +
            place.y +
            "," +
            place.x +
            '">' +
            place.place_name +
            "</a>" +
            "</div>"
        );
        infowindow.open(map, marker);
      });
    }
  }, [searchKeyword]);
  // ๊ฒ€์ƒ‰์–ด๊ฐ€ ๋ณ€๊ฒฝ๋ ๋•Œ๋งˆ๋‹ค  handleInputChange ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ ์šฉํ•˜๊ธฐ
  const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
    setSearchKeyword(e.target.value);
  };

  return (
    <div>
      <StyledMapMainContainer>
        <StyledMapLocationDot>
          <FaMapLocationDot />
        </StyledMapLocationDot>
        <StyledMapTitle>
          <span>๋˜๊ฐ„์ง€๋„</span>
          <MapGuide />
        </StyledMapTitle>

        <StyledInput
          type="text"
          value={searchKeyword}
          onChange={handleInputChange}
          placeholder="๋ง›์ง‘์„ ๊ฒ€์ƒ‰ํ•ด๋ณด์„ธ์š”"
        />
        <StyledMap id="map">
          <div></div>
        </StyledMap>
        <StyledList>
          <div id="result-list">
            {places.map((place, index) => (
              <div key={index} style={{ marginTop: "20px" }}>
                {/* <span>{index + 1}</span> */}
                <Info>
                  <p className="place_name">๐Ÿด{place.place_name}</p>
                  {place.road_address_name ? (
                    <div>
                      <p className="place_address">{place.road_address_name}</p>
                    </div>
                  ) : (
                    <p>{place.road_address_name}</p>
                  )}
                  <p className="place_phone">{place.phone}</p>
                </Info>
              </div>
            ))}
          </div>
        </StyledList>
        <StyledPagination>
          <div id="pagination"></div>
        </StyledPagination>
      </StyledMapMainContainer>
    </div>
  );
};

export default MapContainer;

About

๐Ÿ”ต๐ŸŸฃ๐ŸŸกSuperCoding_Project(3)_Restaurant Recommendation Service

Topics

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages