๋๋ฅผ ํ์ฅํ๊ฒ ํ๋ ๋นจ๊ฐ์ค
์ค๋ ๋ญ๊ฐ ์์ฐจ์์ฐจํด์ ์ฅ๋ฐ๊ตฌ๋ ์ปดํฌ๋ํธ๋ฅผ ๋ง๋ค๋ ค๊ณ ํ๋ค. (์ค๋ ์์ฑํ๊ณ ์ถ์๋๋ฐ)
์ TIL์ฐ๋ ค๊ณ ์ฝ๋ ๋ณต์ฌํ๋ฉด์ ์?์ด๊ฑฐ ์ ์ด๋ ๊ฒ ์๊ฒผ๋? ์ถ์๊ฑธ ๊ณ ์ณค๋๋ ์๋ฌ๊ฐ 6๊ฐ์์ 2๊ฐ๋ก ์ค์๋ค..!!
๊ทธ๋์ ์ค๋ ๋ฌด์์ ํ๋๊ฐ
import { supabase } from "@/service/supabase";
import { NextResponse, NextRequest } from "next/server";
export const GET = async (res: NextResponse, context: { params: { userId: string } }) => {
const {
params: { userId }
} = context;
let { data: cart, error } = await supabase
.from("cart")
.select(`*, store(name), product(name, thumbnail, category(category_name), price, percentage_off)`)
.eq("user_id", userId);
if (error) {
return NextResponse.json({ error: error.message }, { status: 500 });
}
return NextResponse.json(cart);
};
http://localhost:3000/api/cart/`userId`๋ฅผ ์
๋ ฅํ๋ฉด ํด๋น ์ ์ ๊ฐ ์ฅ๋ฐ๊ตฌ๋์ ๋ด์ ์์ดํ
๋ฐ์ดํฐ๋ฅผ ์ ๊ฐ์ ธ์ค๋ ๊ฒ์ ํ์ธํ ์ ์๋ค.
import { useQuery } from "@tanstack/react-query";
interface CartBox {
cart: {
count: number | null;
id: string;
product_id: string | null;
store_id: string | null;
user_id: string;
product: {
name: string;
thumbnail: string;
price: number;
percentage_off: number;
category: {
category_name: string;
};
};
store: {
name: string;
};
};
}
const useCart = (id: string) => {
const {
data: cart,
isLoading,
isError
} = useQuery<CartBox[]>({
queryFn: async (): Promise<CartBox[]> => {
const response = await fetch(`/api/cart/${id}`, { method: "GET" });
const data = await response.json();
return data;
},
queryKey: ["cart"]
});
// console.log(cart);
return { cart, isLoading };
};
export default useCart;
์ ๋
์์ด ๊ฒฐ์ ํ์ด์ง์์ ๋์ผํ๊ฒ ๋ค์ด๊ฐ์ ์ปดํฌ๋ํธ๋ก ๋ถ๋ฆฌํ๊ธฐ๋ก ํ๋๋ฐ ์ผ๋จ,, ์ผ๋จ ์ปดํฌ๋ํธ๋ก ๋ถ๋ฆฌํ์ง ์๊ณ ,,
"use client";
import React, { useState } from "react";
import Image from "next/image";
// import CartItem from "./CartItem"; <- ์ปดํฌ๋ํธ๋ก ๋ง๋๋ ค๊ณ ํ๋ ํ์ ...
import useCart from "@/hooks/useCart";
interface CartBox {
cart: {
count: number | null;
id: string;
product_id: string | null;
store_id: string | null;
user_id: string;
product: {
name: string;
thumbnail: string;
price: number;
percentage_off: number;
category: {
category_name: string;
};
};
store: {
name: string;
};
};
}
// const CartItem = ({ cart, product, store }: Props) => {
const CartItem = (cart: CartBox) => {
const { count, id, product_id, store_id, user_id, store, product } = cart.cart;
const { name: product_name, thumbnail, price, percentage_off, category } = product;
console.log(price);
//์์ ํ
const [cnt, setCnt] = useState(count || 0);
return (
<>
<div className="border border-gray w-[60%] p-2">
<div className="flex flex-row my-2">
<div className="mx-5 ">
<Image src={thumbnail} width={160} height={120} alt="์ํ๋ํ์ด๋ฏธ์ง" />
</div>
<div className="mx-3">
<p>{category.category_name}</p>
<p>{store.name}</p>
<p>๋ ์ง</p>
<p>{price}์</p>
<div className="flex flex-row">
<button onClick={() => setCnt(cnt - 1)} className="bg-gray-300 w-[25px] h-[25px]">
-
</button>
<p className="w-[25px] h-[25px] text-center border border-gray-300">{cnt}</p>
<button onClick={() => setCnt(cnt + 1)} className="bg-gray-300 w-[25px] h-[25px]">
+
</button>
</div>
</div>
</div>
<div className="flex flex-row border-t">
<p>์ํ๊ธ์ก{price}์</p>
<p>์๋ {cnt}๊ฐ</p>
<p>์ด๊ธ์ก{cnt * price}์</p>
</div>
</div>
</>
);
};
const page = () => {
//useCart์ ์ฌ์ฉ์ id
const { cart, isLoading } = useCart("aba26c49-82c0-42b2-913c-c7676527b553");
// console.log(cart);
return (
<>
{!isLoading ? (
<div>
{(cart as CartBox[]).map((cartItem) => {
console.log("cartItem:", cartItem);
return cartItem && <CartItem cart={cartItem} key={cartItem.id} />;
//โ์ฌ๊ธฐ์ ์๋ฌ๊ฐ 2๊ฐ๋๋ค. ๋์ฆ๋ง ํ์ฅํด
})}
</div>
) : (
<div>Loading...</div>
)}
</>
);
};
export default page;
์ด๋ค ์๋ฌ 2๊ฐ๊ฐ ๋๋~~
์ฒซ๋ฒ์งธ ์๋ฌ๋ ์ ๋ชจ๋ฅด๊ฒ ์ง๋ง ๋๋ฒ์งธ ์๋ฌ๋ ์ ๋ง ๋ชจ๋ฅด๊ฒ ๋ค. ์ฝ์์ key๊ฐ ์๋ค๊ณ warning๋ ์๋จ๊ณ cartItem์ ์ฝ์์ ์ฐ์ด๋ดค์ ๋ ๋ถ๋ช
ํ id๊ฐ์ด ์๋๋ฐ ์,, ์ ์๋ค๊ณ ํ๋,,
์คํ์ ๋ฉ์ฉกํ๊ฒ ์ ๋๋ค. (๋ถ๊ธธํด๋ณด์ด๋ warning์ ์ฌ์ง ์ฌ์ด์ฆ ๋๋ฌธ์ด๋ค. ๋์ค์ ์์ ํด์ผ๋)
๊ธฐ๊ณ์น๊ตฌํํ
๋ฌผ์ด๋ด๋ ์์ํ ํด๊ฒฐ์ฑ
์ ์ป์ง ๋ชปํ๋ค.
๋ด์ผ ์ ์ฌ ๋จน๊ธฐ ์ ์ ๊ผญ ํด๊ฒฐํด์ผ์ง. ๊ทธ๋์ ์ ๋ ๋จน๊ธฐ์ ์ ์ปดํฌ๋ํธ ๋ถ๋ฆฌํ๊ณ cart ํ์ด์ง ํ๊น์ง ์ก๋๊ฒ ๋ชฉํ!!