-
Notifications
You must be signed in to change notification settings - Fork 3
Main
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)
};
}