Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 내 서랍장, 랭킹 페이지 무한 스크롤 구현 #185

Merged
merged 11 commits into from
May 27, 2024
Merged
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
4 changes: 2 additions & 2 deletions src/drawer/apis/getBookMarked.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { soomsilClient } from '@/apis';

import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses } from '../types/product.type';

export const getBookmarked = async ({ responseType, page }: RankingRequestParams) => {
export const getBookmarked = async ({ responseType, page }: ProductRequestParams) => {
const response = await soomsilClient.get<ProductResponses>('/v2/drawer/my-bookmarked', {
params: {
responseType,
Expand Down
4 changes: 2 additions & 2 deletions src/drawer/apis/getMyProduct.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { soomsilClient } from '@/apis';

import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses } from '../types/product.type';

export const getMyProduct = async ({ responseType, page }: RankingRequestParams) => {
export const getMyProduct = async ({ responseType, page }: ProductRequestParams) => {
const response = await soomsilClient.get<ProductResponses>('/v2/drawer/my-registered', {
params: {
responseType,
Expand Down
4 changes: 2 additions & 2 deletions src/drawer/apis/getNewRelease.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { soomsilClient } from '@/apis';

import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses } from '../types/product.type';

export const getNewRelease = async ({ responseType, category, page }: RankingRequestParams) => {
export const getNewRelease = async ({ responseType, category, page }: ProductRequestParams) => {
const response = await soomsilClient.get<ProductResponses>('/v2/drawer/new-release', {
params: {
responseType,
Expand Down
4 changes: 2 additions & 2 deletions src/drawer/apis/getRanking.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { soomsilClient } from '@/apis';

import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses } from '../types/product.type';

export const getRanking = async ({ responseType, category, page }: RankingRequestParams) => {
export const getRanking = async ({ responseType, category, page }: ProductRequestParams) => {
const response = await soomsilClient.get<ProductResponses>('/v2/drawer/rank', {
params: {
responseType,
Expand Down
1 change: 1 addition & 0 deletions src/drawer/constants/page.constant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const PRODUCTS_PER_PAGE = 21;
30 changes: 22 additions & 8 deletions src/drawer/hooks/useGetBookMarked.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { useSuspenseQuery } from '@tanstack/react-query';
import { InfiniteData, useSuspenseInfiniteQuery } from '@tanstack/react-query';

import { getBookmarked } from '../apis/getBookMarked';
import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { PRODUCTS_PER_PAGE } from '../constants/page.constant';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses, ProductResult } from '../types/product.type';

export const useGetBookmarked = ({ responseType, page }: RankingRequestParams) => {
return useSuspenseQuery({
queryKey: ['bookmarked', { responseType, page }],
queryFn: () => {
return getBookmarked({ responseType, page });
type ProductRequestParamsWithoutPage = Omit<ProductRequestParams, 'page'>;

export const useGetBookmarked = ({ responseType }: ProductRequestParamsWithoutPage) => {
return useSuspenseInfiniteQuery<
ProductResponses[],
Error,
InfiniteData<ProductResult[], number>,
string[],
number
>({
queryKey: ['bookmarked', responseType],
queryFn: ({ pageParam }) =>
getBookmarked({ responseType, page: pageParam }).then((data) => data.productList) as Promise<
ProductResponses[]
>,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
return lastPage.length === PRODUCTS_PER_PAGE ? allPages.length : undefined;
},
retry: false,
select: (data) => data.productList,
});
};
30 changes: 22 additions & 8 deletions src/drawer/hooks/useGetMyRegistered.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
import { useSuspenseQuery } from '@tanstack/react-query';
import { InfiniteData, useSuspenseInfiniteQuery } from '@tanstack/react-query';

import { getMyProduct } from '../apis/getMyProduct';
import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { PRODUCTS_PER_PAGE } from '../constants/page.constant';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses, ProductResult } from '../types/product.type';

export const useGetMyRegistered = ({ responseType, page }: RankingRequestParams) => {
return useSuspenseQuery({
queryKey: ['myRegistered', { responseType, page }],
queryFn: () => {
return getMyProduct({ responseType, page });
type ProductRequestParamsWithoutPage = Omit<ProductRequestParams, 'page'>;

export const useGetMyRegistered = ({ responseType }: ProductRequestParamsWithoutPage) => {
return useSuspenseInfiniteQuery<
ProductResponses[],
Error,
InfiniteData<ProductResult[], number>,
string[],
number
>({
queryKey: ['myRegistered', responseType],
queryFn: ({ pageParam }) =>
getMyProduct({ responseType, page: pageParam }).then((data) => data.productList) as Promise<
ProductResponses[]
>,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
return lastPage.length === PRODUCTS_PER_PAGE ? allPages.length : undefined;
},
retry: false,
select: (data) => data.productList,
});
};
32 changes: 22 additions & 10 deletions src/drawer/hooks/useGetNewRelease.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { useQuery } from '@tanstack/react-query';
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query';

import { getNewRelease } from '@/drawer/apis/getNewRelease';

import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { PRODUCTS_PER_PAGE } from '../constants/page.constant';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses, ProductResult } from '../types/product.type';

export const useGetNewRelease = ({ responseType, category, page }: RankingRequestParams) => {
return useQuery({
queryKey: ['newReleases', { responseType, category, page }],
queryFn: () => {
return getNewRelease({
type ProductRequestParamsWithoutPage = Omit<ProductRequestParams, 'page'>;

export const useGetNewRelease = ({ responseType, category }: ProductRequestParamsWithoutPage) => {
return useInfiniteQuery<
ProductResponses[],
Error,
InfiniteData<ProductResult[], number>,
string[],
number
>({
queryKey: ['newReleases', responseType, category ?? ''],
queryFn: ({ pageParam }) =>
getNewRelease({
responseType,
category,
page,
});
page: pageParam,
}).then((data) => data.productList) as Promise<ProductResponses[]>,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
return lastPage.length === PRODUCTS_PER_PAGE ? allPages.length : undefined;
},
select: (data) => data.productList,
});
};
3 changes: 1 addition & 2 deletions src/drawer/hooks/useGetProductByProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import { useRecoilValue } from 'recoil';
import { CategoryState } from '@/drawer/recoil/CategoryState';

import { getProductByProvider } from '../apis/getProductByProvider';
import { PRODUCTS_PER_PAGE } from '../constants/page.constant';
import { ProviderProductResponses } from '../types/product.type';

const PRODUCTS_PER_PAGE = 21;

export const useGetProductByProvider = ({ providerId }: { providerId: string }) => {
const selectedCategory = useRecoilValue(CategoryState);

Expand Down
32 changes: 22 additions & 10 deletions src/drawer/hooks/useGetStarRank.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
import { useQuery } from '@tanstack/react-query';
import { InfiniteData, useInfiniteQuery } from '@tanstack/react-query';

import { getRanking } from '../apis/getRanking';
import { RankingRequestParams } from '../types/RankingRequestParams.type';
import { PRODUCTS_PER_PAGE } from '../constants/page.constant';
import { ProductRequestParams } from '../types/ProductRequestParams.type';
import { ProductResponses, ProductResult } from '../types/product.type';

export const useGetStarRank = ({ responseType, category, page }: RankingRequestParams) => {
return useQuery({
queryKey: ['getRanking', { responseType, category, page }],
queryFn: () => {
return getRanking({
type ProductRequestParamsWithoutPage = Omit<ProductRequestParams, 'page'>;

export const useGetStarRank = ({ responseType, category }: ProductRequestParamsWithoutPage) => {
return useInfiniteQuery<
ProductResponses[],
Error,
InfiniteData<ProductResult[], number>,
string[],
number
>({
queryKey: ['getRanking', responseType, category ?? ''],
queryFn: ({ pageParam }) =>
getRanking({
responseType,
category,
page,
});
page: pageParam,
}).then((data) => data.productList) as Promise<ProductResponses[]>,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
return lastPage.length === PRODUCTS_PER_PAGE ? allPages.length : undefined;
},
select: (data) => data.productList,
});
};
48 changes: 41 additions & 7 deletions src/drawer/pages/MyDrawer/MyDrawer.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react';
import { useState, useCallback, useRef } from 'react';

import { useSearchParams } from 'react-router-dom';

Expand All @@ -16,15 +16,48 @@ export type TabType = 'STAR' | 'MYDRAWER';

export const MyDrawer = () => {
const [searchParams, setSearchParams] = useSearchParams();
const { data: bookmarkedData } = useGetBookmarked({ responseType: 'WEB', page: 0 });
const { data: myProductData } = useGetMyRegistered({ responseType: 'WEB', page: 0 });

const {
data: bookmarkedData,
fetchNextPage: fetchNextBookmarkedPage,
hasNextPage: hasNextBookmarkedPage,
isLoading: isLoadingBookmarked,
} = useGetBookmarked({ responseType: 'WEB' });

const {
data: myProductData,
fetchNextPage: fetchNextMyProductPage,
hasNextPage: hasNextMyProductPage,
isLoading: isLoadingMyProduct,
} = useGetMyRegistered({ responseType: 'WEB' });

const initialTab = searchParams.get('tab');

const [currentTab, setCurrentTab] = useState<TabType>(
isTabType(initialTab) ? initialTab : 'STAR'
);

const drawerData = currentTab === 'STAR' ? bookmarkedData : myProductData;
const fetchNextPage = currentTab === 'STAR' ? fetchNextBookmarkedPage : fetchNextMyProductPage;
const hasNextPage = currentTab === 'STAR' ? hasNextBookmarkedPage : hasNextMyProductPage;
const isLoading = currentTab === 'STAR' ? isLoadingBookmarked : isLoadingMyProduct;

const observer = useRef<IntersectionObserver>();

const lastElementRef = useCallback(
(node: HTMLDivElement) => {
if (isLoading) return;

if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && hasNextPage) {
fetchNextPage();
}
});
if (node) observer.current.observe(node);
},
[isLoading, hasNextPage, fetchNextPage]
);

function isTabType(tab: string | null): tab is TabType {
return tab === 'STAR' || tab === 'MYDRAWER';
Expand Down Expand Up @@ -55,13 +88,14 @@ export const MyDrawer = () => {
/>
</StyledTabWrapper>
<Spacing direction={'vertical'} size={14} />
{drawerData.length > 0 ? (
{drawerData.pages[0].length === 0 ? (
<EmptyScreen type={currentTab} />
) : (
<>
<StyledDescription>{TAB_DESCRIPTION[currentTab]}</StyledDescription>
<CardLayout data={drawerData} type={currentTab} />
<CardLayout data={drawerData.pages.flatMap((page) => page)} type={currentTab} />
nijuy marked this conversation as resolved.
Show resolved Hide resolved
<div ref={lastElementRef} />
</>
) : (
<EmptyScreen type={currentTab} />
)}
</StyledContainer>
);
Expand Down
57 changes: 43 additions & 14 deletions src/drawer/pages/NewRelease/NewRelease.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useCallback, useRef } from 'react';

import { useRecoilValue } from 'recoil';

import { CategoryDropdownMenu } from '@/drawer/components/Category/CategoryDropdownMenu/CategoryDropdownMenu';
Expand All @@ -20,11 +22,33 @@ import {

export const NewRelease = () => {
const selectedCategory = useRecoilValue(CategoryState);
const { data: newReleases } = useGetNewRelease({
const {
data: newReleases,
isLoading,
fetchNextPage,
hasNextPage,
} = useGetNewRelease({
responseType: 'WEB',
category: selectedCategory,
page: 0,
});

const observer = useRef<IntersectionObserver>();

const lastElementRef = useCallback(
(node: HTMLDivElement) => {
if (isLoading) return;

if (observer.current) observer.current.disconnect();
observer.current = new IntersectionObserver((entries) => {
if (entries[0].isIntersecting && hasNextPage) {
fetchNextPage();
}
});
if (node) observer.current.observe(node);
},
[isLoading, hasNextPage]
);

const isSmallDesktop = useMediaQuery(SMALL_DESKTOP_MEDIA_QUERY);

return (
Expand All @@ -41,18 +65,23 @@ export const NewRelease = () => {
</StyledBetweenContainer>
<StyledCardContainer>
{newReleases &&
newReleases.map((product) => (
<BigDrawerCard
key={product.productNo}
link={`/drawer/services/${product.productNo}`}
title={product.productTitle}
body={product.productSubTitle}
bookmarkCount={product.count}
isBookmarked={product.isBookmarked}
bigImgSrc={product.introductionImage[0]}
smallImgSrc={product.mainImage}
/>
))}
newReleases.pages.flat().map((product, productIndex, flatArray) => {
const isLastProduct = productIndex === flatArray.length - 1;
return (
<div key={product.productNo}>
<BigDrawerCard
link={`/drawer/services/${product.productNo}`}
title={product.productTitle}
body={product.productSubTitle}
bookmarkCount={product.count}
isBookmarked={product.isBookmarked}
bigImgSrc={product.introductionImage[0]}
smallImgSrc={product.mainImage}
/>
{isLastProduct && <div ref={lastElementRef} />}
</div>
);
})}
</StyledCardContainer>
</div>
</StyledRankingContainer>
Expand Down
Loading