let's get IT with DAVINA π»
μ° κΈ°λ₯ (μμ리μνΈ) λ³Έλ¬Έ
- νΈν
μΉ΄ν
κ³ λ¦¬ list νλ©΄μμ μ’μμλ νΈν
μ΄ μ±μμ§ννΈλ‘ νμ νμ
→ BEμμ isLike μ κ°μνλλͺ μΌλ‘ ꡬλΆν΄μ€μΌν¨ - νΈν list μ‘°ννλ©΄μμ likeκ° μ μμΌλ‘ κ΄λ¦¬λκ² λλ©΄ νΈν λ³ "μ’μμ" μν νμκ°(νκ° λλ₯΄λ©΄ μ 체 μνκ° λ€ λ°λκ² λ¨) νλλ―λ‘ κ°λ³ μνλ‘ κ΄λ¦¬κ°λ₯νλλ‘ μμ ν΄μΌνλ μν©
- μμ리μ€νΈ νμ΄μ§μμλ νΈν ννΈ μ±μμ§μνλ‘ νμ
λ©μΈ νλ‘μ νΈλμ μν©
→ BEμ isLike ꡬλΆμ΄ μλμ΄ μμμΌλ©°,
→ wishlistμμ dataμ 보λ€μ΄ μμμ
⇒ λ°λΌμ, μ’μμλ₯Ό λλ λμ§ μλλ λμ§λ₯Ό νλ¨νλκ²λ³΄λ€
μμ리μ€νΈμ μλ νΈν μ΄ μ 체리μ€νΈμ νΈν idκ°κ³Ό μΌμΉν κ²½μ° μ’μμκ° λμ΄μλ μν & μμ리μ€νΈμ μμΌλ©΄ μ’μμκ° μλμ΄μλ μνλ‘ κ΅¬λΆ
1. WishList data λΆλ¬μ€κΈ°
- store/WishList.js
import axios from "axios";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import { accessToken } from "../utils/localStorage";
export const getWishList = createAsyncThunk("GET_WISH", async () => {
const wishList = await (
await axios.get(`/member/wishlists`, {
headers: {
Authorization: accessToken,
},
})
).data;
return wishList.data;
});
const initialState = [];
export const WishState = createSlice({
name: "wishstate",
initialState,
reducers: {},
extraReducers: {
[getWishList.fulfilled]: (state, { payload }) => [...payload],
},
});
2. μ 체 νΈν 리μ€νΈ μ‘°ν νμ΄μ§μμ wishList data λΆλ¬μ€κΈ°
- pages/AllProducts.jsx
export default function AllProducts() {
const wish = useSelector((state) => state.Wishlist);
const { isLoading, error, productsList } = useAllProducts(getUrl, pathname);
const likedHotelList = wish.map((el) => el.hotelId);
useEffect(() => {
dispatch(getWishList());
}, [dispatch]);
return (
{productsList &&
productsList.map((el) => (
<HotelCard
isLogin={isLogin}
key={el.hotelId}
id={el.hotelId}
title={el.hotelTitle}
price={el.price}
score={el.hotelReviewScore}
img={el.hotelImage}
reviewNum={el.reviewQuantity}
wish={wish}
isSelected={likedHotelList.includes(el.hotelId)} //wishListμ μλ hotelIdκ°κ³Ό μ 체리μ€νΈνμ΄μ§(productList)μ hotelidκ°μ΄ κ°μκ² μλμ§ μ°ΎκΈ° => boolean
/>
))}
)
3. κ°λ³ HotelCard componentμμ ν΄λΉ μ 보λ₯Ό μ΄μ©ν΄μ ννΈ μ±μ°κΈ°/λΉμ°κΈ°
- components/HotelCard.jsx
export default function HotelCard({
title,
price,
score,
img,
id,
reviewNum,
isSelected,
isLogin,
}) {
const handleNavigate = () => {
navigate(`/rooms/${id}`, { state: id });
};
const dispatch = useDispatch();
const [isLike, setIsLike] = useState(isSelected); //ν¬ν¨λμ΄μμΌλ©΄ true(μ’μμ λμνκ° λ¨) ν¬ν¨μλμ΄μμΌλ©΄ false(μ’μμ μλμνκ° λ¨)
const handleLike = async (id, e) => {
e.stopPropagation();
// λ‘κ·ΈμΈ μλ μ, λ¨Όμ μ‘κ³ λ‘κ·ΈμΈ
if (!isLogin) {
dispatch(modalOpen());
return;
}
if (isLike === false) {
try {
await axios.post(`/member/wishlists?hotelId=${id}`, undefined, {
headers: {
Authorization: localStorage.getItem("accessToken"),
},
});
} catch (error) {
console.error(error);
}
} else {
try {
await axios.delete(`/member/wishlists?hotelId=${id}`, {
headers: {
Authorization: localStorage.getItem("accessToken"),
},
});
} catch (error) {
console.error(error);
}
}
setIsLike(!isLike) //falseμ΄κ±΄ trueμ΄κ±΄ λκ°μ΄ λ‘μ§ λ€μ§μ΄ μ£Όλκ±°λκΉ ifλ¬Έ λλλ©΄ νκΊΌλ²μ μ²λ¦¬
};
return (
<CardBox onClick={(e) => handleNavigate(e)}>
<ImgBox img={img}>
<Icon onClick={(e) => handleLike(id, e)}>
{isLike ? <AiFillHeart className="heart" /> : <AiOutlineHeart />}
</Icon>
4. wishList pageμμ μ’μμ λ μνλ§ λ¨κΈ°κΈ°
- pages/WishLists.jsx
export default function WishLists() {
const wish = useSelector((state) => state.Wishlist);
const dispatch = useDispatch();
useEffect(() => {
dispatch(getWishList());
}, [dispatch]);
return (
{wish &&
wish.map((el) => (
<HotelCard
isLogin={isLogin}
key={el.hotelId}
id={el.hotelId}
title={el.hotelTitle}
price={el.price}
score={el.hotelReviewScore}
img={el.hotelImage}
reviewNum={el.reviewQuantity}
isSelected //trueκ°λ§ λ겨μ€μ μμ리μ€νΈ νμ΄μ§λ ννΈκ° μ°¬ κ²λ€λ§ μκ²λ¨
/>
))}
'Project > μ κ°?' μΉ΄ν κ³ λ¦¬μ λ€λ₯Έ κΈ
scroll prevent (0) | 2023.02.17 |
---|---|
λ λ μ§ μ¬μ΄ κ³μ° (0) | 2023.02.09 |
KakaoMap Api μ μ© (0) | 2023.02.09 |
button count μ‘°μ /μ ν (0) | 2023.02.09 |
[μ κ°?] νλ‘μ νΈ νκ³ (6) | 2023.02.08 |
Comments