Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 3단계 완료
# 3단계 완료 (리뷰 반영)

# 목표

Expand Down
21 changes: 9 additions & 12 deletions src/hooks/useThemeProducts.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useEffect, useCallback } from 'react';
import { useState, useEffect, useCallback, useRef } from 'react';
import { apiClient } from '@/api/apiClient';
import { GiftItem } from '@/constants/GiftItem';

Expand All @@ -18,10 +18,10 @@ export function useThemeProducts(themeId: number) {
const [products, setProducts] = useState<GiftItem[]>([]);
const [loading, setLoading] = useState<boolean>(false);
const [error, setError] = useState<Error | null>(null);
const [cursor, setCursor] = useState<number>(0);
const [hasMore, setHasMore] = useState<boolean>(true);
const cursorRef = useRef<number>(0);

const fetchProducts = useCallback(async () => {
const fetchMore = useCallback(async () => {
if (!themeId || !hasMore || loading) return;

setLoading(true);
Expand All @@ -31,7 +31,7 @@ export function useThemeProducts(themeId: number) {
const res = await apiClient.get<ApiResponse<ThemeProductsResponse>>(
`/api/themes/${themeId}/products`,
{
params: { cursor, limit: PRODUCTS_LIMIT },
params: { cursor: cursorRef.current, limit: PRODUCTS_LIMIT },
}
);

Expand All @@ -42,26 +42,23 @@ export function useThemeProducts(themeId: number) {
);
return [...prevProducts, ...newProducts];
});
setCursor(res.data.data.cursor);
cursorRef.current = res.data.data.cursor;
setHasMore(res.data.data.hasMoreList);
}
} catch (err) {
setError(err as Error);
} finally {
setLoading(false);
}
}, [themeId, hasMore, loading, cursor]);
}, [themeId, hasMore, loading]);

useEffect(() => {
setProducts([]);
setCursor(0);
cursorRef.current = 0;
setHasMore(true);
setError(null);
fetchMore();
}, [themeId]);

useEffect(() => {
fetchProducts();
}, [fetchProducts]);

return { products, loading, error, fetchMore: fetchProducts, hasMore };
return { products, loading, error, fetchMore, hasMore };
}
7 changes: 7 additions & 0 deletions src/pages/ThemeProductListPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const ThemeProductListPage: React.FC = () => {

const observerRef = useRef<HTMLDivElement>(null);
const observer = useRef<IntersectionObserver | null>(null);


useEffect(() => {
if (themeError && themeError.message.includes('404')) {
Expand Down Expand Up @@ -66,6 +67,8 @@ const ThemeProductListPage: React.FC = () => {
};
}, [handleObserver]);



if (themeLoading) {
return <Spinner />;
}
Expand Down Expand Up @@ -139,6 +142,10 @@ const ProductGrid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
gap: 16px;

@media (min-width: 768px) {
grid-template-columns: repeat(3, 1fr);
}
`;

const EmptyState = styled.div`
Expand Down