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:결과페이지 api 및 스타일 작업 #174

Merged
merged 24 commits into from
Jan 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 0 additions & 4 deletions src/apis/post/apis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type {
GetCategoriesRes,
GetPostsReq,
GetPostsRes,
GetSortOptionsReq,
GetPostRes,
CreatePostReq,
CreatePostRes
Expand All @@ -20,6 +19,3 @@ export const getCategories = () =>

export const getPosts = (params: GetPostsReq) =>
http.get<GetPostsReq, GetPostsRes>('/posts', params)

export const getSortOption = (params: GetSortOptionsReq) =>
http.get<GetSortOptionsReq, GetPostsRes>(`/sorts?type=${params.type}`)
22 changes: 2 additions & 20 deletions src/apis/post/queries.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,6 @@
import { useMutation, useQuery, useInfiniteQuery } from '@tanstack/react-query'
import {
getCategories,
getPosts,
getSortOption,
getPost,
createPost
} from './apis'
import type {
GetPostsReq,
GetPostsRes,
GetSortOptionsReq,
CreatePostReq
} from './types'
import { getCategories, getPosts, getPost, createPost } from './apis'
import type { GetPostsReq, GetPostsRes, CreatePostReq } from './types'

export const useCreatePostMutation = () =>
useMutation({
Expand Down Expand Up @@ -40,16 +29,9 @@ export const useGetInfinitePostsQuery = (params: GetPostsReq) =>
useInfiniteQuery<GetPostsRes>({
queryKey: ['getPosts'],
queryFn: () => getPosts(params),
refetchOnMount: 'always',
initialPageParam: null,
getNextPageParam: lastPage =>
lastPage?.hasNext
? lastPage.posts[lastPage.posts.length - 1].id
: undefined
})

export const useGetSortOptions = (params: GetSortOptionsReq) =>
useQuery({
queryKey: ['getSortOption'],
queryFn: () => getSortOption(params)
})
7 changes: 0 additions & 7 deletions src/apis/post/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import type {
PostDetail,
PostSummaries,
ProductConditionCodes,
SortOptionsShape,
TradeStatusType,
TradeTypeCodes
} from '@types'
Expand Down Expand Up @@ -65,10 +64,4 @@ export type CreatePostReq = {
}
export type CreatePostRes = CommonCreation

// TODO: 정확한 타입 BE 확인 필요
export type GetSortOptionsReq = {
type: 'OFFER' | 'POST'
}
export type GetSortOptionsRes = SortOptionsShape

export type GetCategoriesRes = (OptionShape & { imageUrl: string })[]
7 changes: 2 additions & 5 deletions src/components/result/CategoryHeader/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,15 @@ import type { ResultHeaderProps } from './types'

const ResultHeader = ({
searchResult,
postData
postSummaries
}: ResultHeaderProps): ReactElement => {
const postCount =
postData && postData.reduce((acc, cur) => acc + cur.posts.length, 0)

return (
<Styled.CategoryHeaderWrapper>
<Styled.CategoryHeader>
&quot;{searchResult}&quot;의 검색결과
</Styled.CategoryHeader>
<Styled.CategoryHeaderResultCount>
{postCount}개
{postSummaries && postSummaries}개
</Styled.CategoryHeaderResultCount>
</Styled.CategoryHeaderWrapper>
)
Expand Down
4 changes: 1 addition & 3 deletions src/components/result/CategoryHeader/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import type { GetPostsRes } from '@apis/post'

export type ResultHeaderProps = {
searchResult: string
postData?: GetPostsRes[]
postSummaries: number
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
}
22 changes: 6 additions & 16 deletions src/components/result/FilterSelect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { ReactElement } from 'react'
import { Styled } from './styled'
import type { FilterSelectProps } from './types'
import { PriceDialog } from '../PriceDialog'
import { useGetSortOptions } from '@apis/post/queries'
import { SORT_OPTIONS } from '@constants/app'
const FilterSelect = ({
categoryItems,
tradePeriodItems,
Expand All @@ -11,26 +11,14 @@ const FilterSelect = ({
minPriceValue,
maxPriceValue,
applyPrice,
postData,
postSummaries,
handleSortPriceChange,
handleCategoryChange,
handleTradePeriodChange,
handleMinPriceInputChange,
handleMaxPriceInputChange,
handlePriceApplyClick
}: FilterSelectProps): ReactElement => {
const { data: priceSortOptions, isLoading } = useGetSortOptions({
type: 'POST'
})

const postCount =
postData && postData.reduce((acc, cur) => acc + cur.posts.length, 0)

if (isLoading) {
// 로딩
return <></>
}

return (
<>
<Styled.SelectWrapper>
Expand Down Expand Up @@ -59,10 +47,12 @@ const FilterSelect = ({
/>
</Styled.LeftSelectWrapper>
<Styled.RightSelectWrapper>
<Styled.ProductCount>전체 {postCount}개</Styled.ProductCount>
<Styled.ProductCount>
전체 {postSummaries && postSummaries}개
</Styled.ProductCount>
<Styled.PriceFilterSelect
colorType="none"
items={priceSortOptions}
items={SORT_OPTIONS}
placeholder="높은 가격순"
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
onChange={handleSortPriceChange}
/>
Expand Down
8 changes: 0 additions & 8 deletions src/components/result/FilterSelect/styled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ const CategorySelect = styled(SelectBox)`
display: inline;
}

${({ theme }): string => theme.mediaQuery.mobile} {
display: inline;
}

div:nth-of-type(1) {
span {
${({ theme }): string => theme.fonts.body02B};
Expand Down Expand Up @@ -78,10 +74,6 @@ const ProductCount = styled.div`
display: none;
}

${({ theme }): string => theme.mediaQuery.mobile} {
display: inline;
}

${({ theme }): string => theme.mediaQuery.tablet} {
display: inline;
}
Expand Down
11 changes: 5 additions & 6 deletions src/components/result/FilterSelect/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { SelectOnChangeHandler } from '@offer-ui/react'
import type { ChangeEventHandler } from 'react'
import type { GetPostsRes } from '@apis/post'
import type { ApplyPriceType } from '@hooks/result/useSelectBoxFilter'
import type { TRADE_TYPES } from '@constants/app'
import type { GetPostsRes } from '@apis'
import type { ApplyPriceType } from '@hooks'

export type FilterSelectProps = {
applyPrice: ApplyPriceType
Expand All @@ -11,10 +12,7 @@ export type FilterSelectProps = {
name: string
selected: boolean
}[]
tradePeriodItems: {
code: string
name: string
}[]
tradePeriodItems: typeof TRADE_TYPES
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
sortPriceItems: {
code: string
name: string
Expand All @@ -24,6 +22,7 @@ export type FilterSelectProps = {
selectedSortPriceValue: string
minPriceValue: string
maxPriceValue: string
postSummaries: number
handleSortPriceChange: SelectOnChangeHandler
handleCategoryChange: SelectOnChangeHandler
handleTradePeriodChange: SelectOnChangeHandler
Expand Down
3 changes: 2 additions & 1 deletion src/components/result/PriceDialog/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ const PriceDialog = ({
const [isOpen, setIsOpen] = useState<boolean>(false)
const { desktop } = useMedia()

const isApplied = applyPrice.maxPrice !== null && applyPrice.minPrice !== null
const isApplied =
applyPrice.maxPrice !== undefined && applyPrice.minPrice !== undefined

useEffect(() => {
setIsOpen(isOpen)
Expand Down
2 changes: 2 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export * from './useResponsive'
export * from './useAuth'
export * from './useModal'
export * from './useProfile'
export * from './result/useCategoryFilterList'
export * from './result/useSelectBoxFilter'
9 changes: 6 additions & 3 deletions src/hooks/result/useCategoryFilterList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { SelectOnChangeHandler } from '@offer-ui/react'
import { useSearchParams } from 'next/navigation'
import { useEffect, useState } from 'react'
import { useGetCategoriesQuery } from '@apis/post'
import { manipulateArray } from '@utils/result'
import { transRateUseToCategorySelectBoxData } from '@utils/result'
export type CheckItemType = {
code: string
name: string
Expand Down Expand Up @@ -34,8 +34,11 @@ const useCategoryFilterList = (): ReturnType => {

useEffect(() => {
if (isSuccess) {
const cateGoryList = manipulateArray(categories, defaultCategory)
setList(cateGoryList)
const categoryList = transRateUseToCategorySelectBoxData(
categories,
defaultCategory
)
setList(categoryList)
setSelectedCategoryValue(defaultCategory ? defaultCategory : 'ALL')
}
}, [categories, defaultCategory, isSuccess])
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/result/useSelectBoxFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export type ApplyPriceType = {
}

type ReturnType = {
tradePeriodItems: any
tradePeriodItems: typeof TRADE_TYPES
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
shinhyojeong marked this conversation as resolved.
Show resolved Hide resolved
sortPriceItems: SelectItemsType[]
selectedTradePeriodValue: string
selectedSortPriceValue: string
Expand Down Expand Up @@ -80,7 +80,6 @@ const useSelectBoxFilter = (): ReturnType => {
}

const handlePriceApplyClick = (): void => {
//필터적용
setApplyPrice({
minPrice: Number(minPriceValue.replace(/,/g, '')),
maxPrice: Number(maxPriceValue.replace(/,/g, ''))
Expand Down
30 changes: 17 additions & 13 deletions src/pages/result/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,7 @@ const Result: NextPage = () => {
const categoryFilterOption =
selectedCategoryValue === 'ALL' ? null : selectedCategoryValue

const {
data: postList,
fetchNextPage,
hasNextPage,
refetch
} = useGetInfinitePostsQuery({
const infinitePosts = useGetInfinitePostsQuery({
lastId: null,
limit: DEFAULT_POST_PAGE_NUMBER,
category: categoryFilterOption,
Expand All @@ -65,20 +60,29 @@ const Result: NextPage = () => {
sort: selectedSortPriceValue
})

const postSummaries = infinitePosts?.data?.pages?.reduce(
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
(acc, cur) => acc + cur?.posts?.length,
0
)

useEffect(() => {
refetch()
infinitePosts?.refetch()
}, [
applyPrice,
selectedCategoryValue,
selectedTradePeriodValue,
selectedSortPriceValue,
refetch
infinitePosts?.refetch,
infinitePosts
])

return (
<Layout>
<ResultWrapper>
<ResultHeader postData={postList?.pages} searchResult="###" />
<ResultHeader
postSummaries={Number(postSummaries)}
sonsurim marked this conversation as resolved.
Show resolved Hide resolved
searchResult="###"
/>
{isDesktop && (
<CategorySlideFilter
cateGoryList={checkFilterList}
Expand All @@ -96,17 +100,17 @@ const Result: NextPage = () => {
handleTradePeriodChange={handleTradePeriodSelectChange}
maxPriceValue={maxPriceValue}
minPriceValue={minPriceValue}
postData={postList?.pages}
postSummaries={Number(postSummaries)}
selectedCategoryValue={selectedCategoryValue}
selectedSortPriceValue={selectedSortPriceValue}
selectedTradePeriodValue={selectedTradePeriodValue}
sortPriceItems={sortPriceItems}
tradePeriodItems={tradePeriodItems}
/>
<ProductList
fetchNextPage={fetchNextPage}
hasNextPage={hasNextPage}
postData={postList?.pages}
fetchNextPage={infinitePosts?.fetchNextPage}
hasNextPage={infinitePosts?.hasNextPage}
postData={infinitePosts?.data?.pages}
/>
</ResultWrapper>
</Layout>
Expand Down
2 changes: 1 addition & 1 deletion src/utils/result/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { GetCategoriesRes } from '@apis/post'
import type { CheckItemType } from '@hooks/result/useCategoryFilterList'

export const manipulateArray = (
export const transRateUseToCategorySelectBoxData = (
shinhyojeong marked this conversation as resolved.
Show resolved Hide resolved
categories: GetCategoriesRes,
searchParams: string | null
) => {
Expand Down