-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #291 from Team-TenTen/develop
[Release] v0.1.11
- Loading branch information
Showing
6 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { PAGE_SIZE } from '@/constants' | ||
import { SpaceResBody } from '@/types' | ||
import { useInfiniteQuery } from '@tanstack/react-query' | ||
import { SpaceListProps } from '../SpaceList' | ||
|
||
interface MainSpacePageType { | ||
metaData?: { | ||
hasNext: boolean | ||
lastId: number | ||
lastFavoriteCount: number | ||
} | ||
responses: SpaceResBody[] | ||
} | ||
|
||
const useMainSpacesQuery = ({ | ||
queryKey, | ||
memberId, | ||
sort, | ||
category, | ||
keyword, | ||
fetchFn, | ||
}: SpaceListProps) => { | ||
const sortValue = | ||
queryKey === 'main' || queryKey === 'search' | ||
? sort === 'favorite' | ||
? 'favorite_count' | ||
: 'created_at' | ||
: undefined | ||
const categoryValue = category === 'all' ? '' : category.toUpperCase() | ||
const { data, fetchNextPage, hasNextPage, isLoading } = useInfiniteQuery({ | ||
queryKey: [ | ||
'spaces', | ||
queryKey, | ||
{ | ||
...(memberId && { memberId: memberId }), | ||
...(sortValue && { sort: sortValue }), | ||
category: categoryValue, | ||
keyword, | ||
}, | ||
], | ||
queryFn: ({ pageParam }) => | ||
fetchFn({ | ||
memberId, | ||
lastFavoriteCount: pageParam.lastFavoriteCount, | ||
lastSpaceId: pageParam.lastSpaceId, | ||
pageSize: PAGE_SIZE, | ||
sort: sortValue, | ||
filter: categoryValue, | ||
keyWord: keyword, | ||
}), | ||
initialPageParam: { lastSpaceId: undefined, lastFavoriteCount: undefined }, | ||
getNextPageParam: ( | ||
lastPage: MainSpacePageType, | ||
): | ||
| { | ||
lastSpaceId: number | undefined | ||
lastFavoriteCount: number | undefined | ||
} | ||
| undefined => { | ||
return lastPage.metaData?.hasNext | ||
? { | ||
lastSpaceId: lastPage.metaData.lastId, | ||
lastFavoriteCount: lastPage.metaData.lastFavoriteCount, | ||
} | ||
: undefined | ||
}, | ||
}) | ||
|
||
return { | ||
spaces: data, | ||
fetchNextPage, | ||
hasNextPage, | ||
isSpacesLoading: isLoading, | ||
} | ||
} | ||
|
||
export default useMainSpacesQuery |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
'use client' | ||
|
||
import { Fragment } from 'react' | ||
import { NONE_SEARCH_RESULT } from '@/components/SpaceList/constants' | ||
import useMainSpacesQuery from '@/components/SpaceList/hooks/useMainSpacesQuery' | ||
import { CATEGORIES_RENDER } from '@/constants' | ||
import useInfiniteScroll from '@/hooks/useInfiniteScroll' | ||
import { SearchSpaceReqBody, SpaceResBody } from '@/types' | ||
import { Spinner } from '../..' | ||
import Space from '../Space/Space' | ||
|
||
export interface SpaceListProps { | ||
memberId?: number | ||
queryKey: string | ||
sort?: string | ||
category: string | ||
keyword?: string | ||
fetchFn: ({ | ||
memberId, | ||
pageNumber, | ||
lastFavoriteCount, | ||
lastSpaceId, | ||
pageSize, | ||
sort, | ||
filter, | ||
keyWord, | ||
}: SearchSpaceReqBody) => Promise<any> | ||
} | ||
|
||
const MainSpaceList = ({ | ||
queryKey, | ||
memberId, | ||
sort, | ||
category, | ||
keyword, | ||
fetchFn, | ||
}: SpaceListProps) => { | ||
const { spaces, fetchNextPage, hasNextPage, isSpacesLoading } = | ||
useMainSpacesQuery({ | ||
queryKey, | ||
memberId, | ||
sort, | ||
category, | ||
keyword, | ||
fetchFn, | ||
}) | ||
|
||
const { target } = useInfiniteScroll({ hasNextPage, fetchNextPage }) | ||
|
||
return isSpacesLoading ? ( | ||
<Spinner /> | ||
) : ( | ||
<> | ||
<ul className="flex flex-col gap-y-2 px-4 pt-2"> | ||
{spaces?.pages[0].responses.length | ||
? spaces?.pages.map((group, i) => ( | ||
<Fragment key={i}> | ||
{group.responses?.map((space: SpaceResBody) => ( | ||
<li key={space.spaceId}> | ||
<Space | ||
userName={space.ownerNickName} | ||
spaceId={space.spaceId} | ||
type="Card" | ||
spaceName={space.spaceName} | ||
spaceImage={space.spaceImagePath} | ||
description={space.description} | ||
category={CATEGORIES_RENDER[space.category]} | ||
scrap={space.scrapCount} | ||
favorite={space.favoriteCount} | ||
/> | ||
</li> | ||
))} | ||
</Fragment> | ||
)) | ||
: !isSpacesLoading && ( | ||
<div className="py-5 text-center text-sm font-medium text-gray9"> | ||
{NONE_SEARCH_RESULT} | ||
</div> | ||
)} | ||
|
||
<div ref={target}></div> | ||
</ul> | ||
</> | ||
) | ||
} | ||
|
||
export default MainSpaceList |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters