Skip to content
johnpk1007 edited this page Aug 1, 2023 · 6 revisions

πŸ“Œ μ£Όμš” κΈ°λŠ₯

βœ”μˆ™μ†Œ λͺ©λ‘ 쑰회

  • μˆ™μ†Œ 등둝 νŽ˜μ΄μ§€λ₯Ό 톡해 λ§Œλ“€μ–΄μ§„ μˆ™μ†Œ 쑰회
  • μˆ™μ†Œ λͺ©λ‘μ˜ 끝에 λ„λ‹¬ν–ˆμ„ λ•Œ Intersection Observer을 μ΄μš©ν•΄ μƒˆλ‘œμš΄ μˆ™μ†Œ λͺ©λ‘ νŽ˜μ΄μ§€λ₯Ό μš”μ²­ν•˜μ—¬ λ¬΄ν•œ 슀크둀 κ΅¬ν˜„
  • 검색을 ν†΅ν•œ μˆ™μ†Œ 필터링
Main.tsx
import useHttpRequest from '../hooks/useHttpRequest';
import { MAIN_PAGE_HOME_LIST_API_PATH } from '../constants/api/homeListApi';

function Main() {
const location = useLocation();
const isSearchPage = location.pathname.includes('/search/');
const [page, setPage] = useState(0);
const [nextData, setNextData] = useState(true)
const { responseData, sendRequest} = useHttpRequest();

//메인 νŽ˜μ΄μ§€ 데이터 μš”μ²­
useEffect(() => {
  if (nextData && !isSearchPage)
    sendRequest({
      url: `${MAIN_PAGE_HOME_LIST_API_PATH}`,
      method: "POST",
      body: {
        page: page
      }
    })
   }, [page])

//Intersection Observer을 μ΄μš©ν•΄ μƒˆλ‘œμš΄ νŽ˜μ΄μ§€ μš”μ²­
 useEffect(() => {
  const handleIntersection = (entries: IntersectionObserverEntry[]) => {
    const entry = entries[0];
    if (entry.isIntersecting && nextData) {
      setPage((prevPage) => prevPage + 1);
    }
  };

  const observer = new IntersectionObserver(
    handleIntersection,
    {
      root: null,
      rootMargin: '0px',
      threshold: 0.1,
    });

  if (observerRef.current) {
    observer.observe(observerRef.current);
  }

  return () => {
    if (observerRef.current) {
      observer.unobserve(observerRef.current);
    }
  };
}, [nextData]);

//검색을 ν†΅ν•œ μˆ™μ†Œ 필터링
useEffect(() => {
  if (nextData && isSearchPage)
    sendRequest({
      url: `${MAIN_PAGE_HOME_LIST_API_PATH}`,
      method: "POST",
      body: {
        page: page,
        city: city === "null" ? null : city,
        district: district === "null" ? null : district,
        checkin: firstPickedDate === "Invalid date" ? null : firstPickedDate,
        checkout: secondPickedDate === "Invalid date" ? null : secondPickedDate,
        people: totalguestnumber === "0" ? null : totalguestnumber,
        type: houseTypeValue === "null" ? null : houseTypeValue
      }
    })
}, [page])
}

βœ”β€ ν΄λ¦­μ‹œ 관심 μˆ™μ†Œ μ €μž₯

  • 관심 μˆ™μ†Œλ‘œ 등둝 후에 관심 μˆ™μ†Œ νŽ˜μ΄μ§€μ— 확인 κ°€λŠ₯.
ToggleFavorite.tsx
import useAuthorizedRequest from "../../../hooks/useAuthorizedRequest";
import { WISHLIST_API_PATH } from "../../../constants/api/homeListApi";

interface ToggleFavoriteProps {
  houseId: number
}

interface ResultData {
  accessToken: string;
}

const ToggleFavorite = ({ houseId }: ToggleFavoriteProps) => {
  const { responseData, sendRequest } = useAuthorizedRequest<ResultData>({
      onUnauthorized: handleUnAutorization,
  });

//관심 μˆ™μ†Œ 등둝 μš”μ²­
const handleClick = async (event: React.MouseEvent<HTMLElement>) => {
      event.stopPropagation()
      if (!loginState) {
          setLoginModal(true)
          return
      }
      if (!favorite) {
          await sendRequest({
              url: `${WISHLIST_API_PATH}`,
              method: "POST",
              withCredentials: true,
              body: {
                  houseId: houseId
              }
          });
      } else {
          await sendRequest({
              url: `${WISHLIST_API_PATH}/${houseId}`,
              method: "DELETE",
              withCredentials: true,
          });
      }
      setFavorite((prevState) => !prevState)
  };
}
Clone this wiki locally