Skip to content
Merged
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
30 changes: 20 additions & 10 deletions apps/nowait-user/src/hooks/useInfiniteStores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,26 @@ interface ServerResponse {
hasNext: boolean;
storeReadDtos: Store[];
};
error: null;
}

// 서버에서 주점 데이터를 가져오는 함수
const fetchStores = async ({ pageParam = 0 }): Promise<Store[]> => {
const fetchStores = async ({
pageParam = 0,
}): Promise<{ stores: Store[]; hasNext: boolean }> => {
try {
const SERVER_URI = import.meta.env.VITE_SERVER_URI;
const currentToken = localStorage.getItem("accessToken");

const response = await axios.get<ServerResponse>(
`${SERVER_URI}/admin/stores/all-stores`,
`${SERVER_URI}/v1/stores/all-stores/infinite-scroll`,
{
headers: {
Authorization: `Bearer ${currentToken}`,
},
params: {
page: pageParam,
size: 20,
size: 5,
},
}
);
Expand All @@ -44,7 +52,10 @@ const fetchStores = async ({ pageParam = 0 }): Promise<Store[]> => {
// 서버 응답 구조에 맞게 데이터 추출
if (response.data.success && response.data.response?.storeReadDtos) {
const storeArray = response.data.response.storeReadDtos;
const hasNext = response.data.response.hasNext;

console.log("추출된 주점 배열:", storeArray);
console.log("hasNext:", hasNext);

// 삭제된 주점 필터링하고 실제 서버 데이터 그대로 반환
const stores: Store[] = storeArray
Expand All @@ -55,21 +66,21 @@ const fetchStores = async ({ pageParam = 0 }): Promise<Store[]> => {
}));

console.log("필터링된 주점 데이터:", stores);
return stores;
return { stores, hasNext };
} else {
console.warn(
"서버 응답이 성공하지 못했거나 데이터가 없습니다:",
response.data
);
return [];
return { stores: [], hasNext: false };
}
} catch (error) {
console.error("주점 데이터 로딩 실패:", error);
if (axios.isAxiosError(error)) {
console.error("응답 상태:", error.response?.status);
console.error("응답 데이터:", error.response?.data);
}
return [];
return { stores: [], hasNext: false };
}
};

Expand All @@ -86,9 +97,8 @@ export const useInfiniteStores = () => {
queryFn: fetchStores,
initialPageParam: 0,
getNextPageParam: (lastPage, allPages) => {
// 서버에서 hasNext를 제공하지만, 일단 기존 로직 유지
// 더 이상 데이터가 없으면 undefined 반환
if (lastPage.length < 20) {
// 서버에서 받은 hasNext를 기준으로 다음 페이지 여부 결정
if (!lastPage.hasNext) {
return undefined;
}
return allPages.length;
Expand All @@ -98,7 +108,7 @@ export const useInfiniteStores = () => {
});

// 모든 페이지의 stores를 하나의 배열로 합치기
const stores = data?.pages.flat() ?? [];
const stores = data?.pages.flatMap((page) => page.stores) ?? [];

// 에러 로깅
useEffect(() => {
Expand Down