Products를 가져올때 필터를 걸기 위해 get-products에 따로 처리를 해준다.
import { Product } from '@/types';
import qs from 'query-string';
const URL = `${process.env.NEXT_PUBLIC_API_URL}/products`;
// 필터를 위함
interface Query {
categoryId?: string;
colorId?: string;
sizeId?: string;
isFeatured?: boolean;
}
const getProducts = async (query: Query): Promise<Product[]> => {
const url = qs.stringifyUrl({
url: URL,
query: {
colorId: query.colorId,
sizeId: query.sizeId,
categoryId: query.categoryId,
isFeatured: query.isFeatured,
},
});
const res = await fetch(url);
return res.json();
};
export default getProducts;
실제 최상단에서 Fetch 해옴
import getBillboard from '@/actions/get-billboard';
import Billboard from '@/components/billboard';
import Container from '@/components/ui/container';
import getProducts from '@/actions/get-products';
import ProductList from '@/components/ProductList';
export const revalidate = 0;
export default async function HomePage() {
const products = await getProducts({ isFeatured: true });
const billboard = await getBillboard('834c7420-7dc2-43fa-bd02-0a036933ff53');
return (
<Container>
<div className='space-y-10 pb-10'>
<Billboard data={billboard} />
<div className='flex flex-col gap-y-8 px-4 sm:px-6 lg:px-8'>
<ProductList title='Featured Products' items={products} />
</div>
</div>
</Container>
);
}
증거 사진 미쳤다…