Skip to content
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: 4 additions & 0 deletions apps/web/app/_apis/services/place.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,25 @@ export const getPlacesByMap = async ({

export const getPlacesByNameSearch = async (
keyword: string,
campus: CampusType,
): Promise<PlaceByNameSearch[]> => {
const { data } = await axiosInstance.get(API_PATH.PLACES.SEARCH.BY_NAME, {
params: {
keyword,
campus,
},
})
return PlaceByNameSearchSchema.array().parse(data)
}

export const getPlacesByMenuSearch = async (
keyword: string,
campus: CampusType,
): Promise<PlaceByMenuSearch[]> => {
const { data } = await axiosInstance.get(API_PATH.PLACES.SEARCH.BY_MENU, {
params: {
keyword,
campus,
},
})
return PlaceByMenuSearchSchema.array().parse(data)
Expand Down
29 changes: 22 additions & 7 deletions apps/web/app/places/search/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,34 @@ import {
getPlacesByNameSearch,
} from '@/_apis/services/place'
import { CLIENT_PATH } from '@/_constants/path'
import type { CampusType } from '@/_constants/campus'
import { useCampusStore } from '@/_store/campus'

const SEARCH_TYPE = {
NAME: 'NAME',
MENU: 'MENU',
} as const

type SearchType = keyof typeof SEARCH_TYPE
type TypeConfig = Record<
SearchType,
{
label: string
placeholder: string
searchFunc: (
campus: CampusType,
) => (
query: string,
) => Promise<Array<{ id: string; name: string; address: string }>>
}
>

const TYPE_CONFIG = {
const TYPE_CONFIG: TypeConfig = {
[SEARCH_TYPE.NAME]: {
label: '가게',
placeholder: '식당 이름을 검색해주세요',
searchFunc: async (query: string) => {
const result = await getPlacesByNameSearch(query)
searchFunc: (campus: CampusType) => async (query: string) => {
const result = await getPlacesByNameSearch(query, campus)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

커링 패턴을 사용하여 캠퍼스 값을 먼저 바인딩하고, 검색 쿼리만 받는 함수 반환
이 방식을 통해 SearchPage 컴포넌트를 전혀 수정하지 않고, 기존에 요구하던 (query: string) => Promise<Result[]> Props 타입을 그대로 호환할 수 있습니다.

return result.map((place) => ({
id: place.placeId,
name: place.placeName,
Expand All @@ -33,8 +47,8 @@ const TYPE_CONFIG = {
[SEARCH_TYPE.MENU]: {
label: '메뉴',
placeholder: '메뉴 이름을 검색해주세요',
searchFunc: async (query: string) => {
const result = await getPlacesByMenuSearch(query)
searchFunc: (campus: CampusType) => async (query: string) => {
const result = await getPlacesByMenuSearch(query, campus)
return result.map((place) => ({
id: place.placeId,
name: place.menuName,
Expand All @@ -48,6 +62,7 @@ const Page = () => {
const { replace } = useRouter()
const [searchType, setSearchType] = useState<SearchType>('NAME')
const currentConfig = TYPE_CONFIG[searchType]
const { campus } = useCampusStore()

return (
<>
Expand All @@ -60,10 +75,10 @@ const Page = () => {
}))}
/>
<SearchPage
key={searchType}
key={`${searchType}-${campus}`}
useBackHandler={true}
placeholder={currentConfig.placeholder}
searchFunc={currentConfig.searchFunc}
searchFunc={currentConfig.searchFunc(campus)}
onSelectPlace={(id) => {
replace(CLIENT_PATH.PLACE_DETAIL(id))
}}
Expand Down