Skip to content

Commit

Permalink
refactor: 장소 상세 api node.js bff로 마이그레이션 (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
saseungmin authored Oct 24, 2023
1 parent 3680528 commit 309cea0
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 26 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';

import { PlaceDetailsResponseData } from '@googlemaps/google-maps-services-js';

import { fetchNaverSearchBlog } from '@/app/api';
import { fetchNaverSearchBlog } from '@/app/api/handler';
import { paramsSerializer } from '@/lib/apis';
import { PlaceDetail } from '@/lib/types/google.maps';

Expand Down
2 changes: 1 addition & 1 deletion src/app/api/search/places/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { checkEmpty } from '@nf-team/core';
import { paramsSerializer } from '@/lib/apis';
import { filteredPlaces } from '@/utils';

import { fetchAllSettledSearchNaverBlogs } from '../..';
import { fetchAllSettledSearchNaverBlogs } from '../../handler';

export const runtime = 'edge';

Expand Down
6 changes: 3 additions & 3 deletions src/components/detail/PlaceDetailWindow/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ type Props = {
isVisible: boolean;
isLoading: boolean;
onClose: () => void;
placeDetail: PlacesWithSearchResult<true> | null;
placeDetail?: PlacesWithSearchResult<true>;
};

function PlaceDetailWindow({
Expand Down Expand Up @@ -197,11 +197,11 @@ function PlaceDetailWindow({
))}
</div>
</Accordion>
{placeDetail?.searchBlogPost.status === 'fulfilled' && (
{placeDetail?.searchBlogPost?.status === 'fulfilled' && (
<Accordion
title="네이버 검색결과"
counterColor={blogCount ? 'danger' : 'positive'}
counter={placeDetail?.searchBlogPost.value.total_count}
counter={checkNumber(placeDetail?.searchBlogPost.value?.total_count)}
wrapperClassName={styles.reviewAccordionWrapper}
>
<div className={styles.resultWrapper}>
Expand Down
16 changes: 4 additions & 12 deletions src/components/detail/PlaceDetailWindowContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import useGetPlaceDetail from '@/hooks/queries/useGetPlaceDetail';
import useGetSearchBlog from '@/hooks/queries/useGetSearchBlog';
import { PlaceResult } from '@/lib/types/google.maps';
import useGetSearchPlace from '@/hooks/queries/useGetSearchPlace';
import usePlaceDetailWindowStore from '@/stores/placeDetailWindow';

import PlaceDetailWindow from '../PlaceDetailWindow';
Expand All @@ -9,23 +7,17 @@ function PlaceDetailWindowContainer() {
const {
isOpenPlaceDetailWindow, onClosePlaceDetailWindow, placeId,
} = usePlaceDetailWindowStore(['isOpenPlaceDetailWindow', 'onClosePlaceDetailWindow', 'placeId']);
const { data: placeDetail, isSuccess } = useGetPlaceDetail({ placeId });

const { data: placesWithSearchResult, isLoading } = useGetSearchBlog<true>({
placesResult: [placeDetail?.result as PlaceResult],
includePost: true,
enabled: isSuccess && !!placeDetail?.result,
});
const { data: placesWithSearchResult, isFetching } = useGetSearchPlace({ placeId });

const onCloseDetailWindow = () => {
onClosePlaceDetailWindow();
};

return (
<PlaceDetailWindow
isLoading={isLoading}
isLoading={isFetching}
isVisible={isOpenPlaceDetailWindow}
placeDetail={placesWithSearchResult?.[0]}
placeDetail={placesWithSearchResult?.result}
onClose={onCloseDetailWindow}
/>
);
Expand Down
2 changes: 1 addition & 1 deletion src/components/map/PlaceBottomSheet/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function PlaceBottomSheet({
onClick={onClickPlaceItem}
/>
))}
{isFetchingNextPage && (
{(isFetchingNextPage || isFetching) && (
<div className={styles.loadingWrapper}>
<Spinner isLoading />
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/map/PlaceBottomSheetItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ function PlaceBottomSheetItem({ place, onClick, wrapperRef }: Props) {
<StarRating rating={place?.rating} type="list" />
<div className={styles.placeUserRatingsTotal}>{`(${checkNumber(user_ratings_total)})`}</div>
</div>
{place.searchBlogPost.status === 'fulfilled' && (
<div className={styles.searchTotal}>{`네이버 검색결과 ${numberWithComma(place.searchBlogPost.value.total_count)}개`}</div>
{place.searchBlogPost?.status === 'fulfilled' && (
<div className={styles.searchTotal}>{`네이버 검색결과 ${numberWithComma(checkNumber(place.searchBlogPost?.value?.total_count))}개`}</div>
)}
</li>
);
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/queries/useGetSearchPlace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useQuery } from '@tanstack/react-query';

import { fetchPlaceDetailV2 } from '@/lib/apis/search';
import { SearchPlace } from '@/lib/types/search';

const TEN_MINUTES = 600000;

function useGetSearchPlace({
placeId, sessionToken,
}: { placeId?: string; sessionToken?: string; }) {
const query = useQuery<SearchPlace>(['placeDetailV2', placeId], () => fetchPlaceDetailV2({ placeId: placeId as string, sessionToken }), {
enabled: !!placeId,
staleTime: TEN_MINUTES,
cacheTime: TEN_MINUTES,
});

return query;
}

export default useGetSearchPlace;
36 changes: 35 additions & 1 deletion src/lib/apis/search/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import FIXTURE_NAVER_SEARCH_BLOG from '@/mocks/fixtures/searchBlog';
import { api, paramsSerializer } from '..';

import { NaverSearchBlogResponse } from './model';
import { fetchGoogleSearch, fetchNaverSearchBlog, fetchPlaceDetail } from '.';
import {
fetchGoogleSearch, fetchNaverSearchBlog, fetchPlaceDetail, fetchPlaceDetailV2,
} from '.';

jest.mock('..');

Expand Down Expand Up @@ -108,4 +110,36 @@ describe('search API', () => {
});
});
});

describe('fetchPlaceDetailV2', () => {
const placeId = 'placeId';
const sessionToken = 'sessionToken';
const mockResponse: TextSearchPlace = {
error_message: '',
status: Status.OK,
results: [],
};

beforeEach(() => {
(api as jest.Mock).mockReturnValueOnce(mockResponse);
});

it(`GET /search/places/${placeId}`, async () => {
const response = await fetchPlaceDetailV2({
placeId,
sessionToken,
});

expect(response).toEqual(mockResponse);
expect(api).toBeCalledWith({
method: 'GET',
headers: {
'session-token': sessionToken,
},
url: `/search/places/${placeId}`,
paramsSerializer,
isBFF: true,
});
});
});
});
22 changes: 21 additions & 1 deletion src/lib/apis/search/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { api, paramsSerializer } from '..';

import {
NaverSearchBlogResponse, PlaceDetailsResponse, SearchPlacesResponse, TextSearchPlaceResponse,
NaverSearchBlogResponse,
PlaceDetailsResponse,
SearchPlaceResponse,
SearchPlacesResponse,
TextSearchPlaceResponse,
} from './model';

const BATCH_SIZE = 10;
Expand Down Expand Up @@ -107,3 +111,19 @@ export const fetchPlaceDetail = async ({

return response;
};

export const fetchPlaceDetailV2 = async ({
placeId, sessionToken,
}: { placeId: string; sessionToken?: string; }) => {
const response = await api<SearchPlaceResponse>({
method: 'GET',
url: `/search/places/${placeId}`,
headers: {
'session-token': sessionToken,
},
paramsSerializer,
isBFF: true,
});

return response;
};
4 changes: 3 additions & 1 deletion src/lib/apis/search/model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { TextSearchResponseData } from '@googlemaps/google-maps-services-js';

import { PlaceDetail } from '@/lib/types/google.maps';
import { NaverSearchBlog, SearchPlaces } from '@/lib/types/search';
import { NaverSearchBlog, SearchPlace, SearchPlaces } from '@/lib/types/search';

export type NaverSearchBlogResponse<T> = NaverSearchBlog<T>;

Expand All @@ -10,3 +10,5 @@ export type TextSearchPlaceResponse = TextSearchResponseData;
export type PlaceDetailsResponse = PlaceDetail;

export type SearchPlacesResponse = SearchPlaces;

export type SearchPlaceResponse = SearchPlace;
10 changes: 7 additions & 3 deletions src/lib/types/search.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TextSearchResponseData } from '@googlemaps/google-maps-services-js';
import { PlaceDetailsResponseData, TextSearchResponseData } from '@googlemaps/google-maps-services-js';

import { PlaceResult } from './google.maps';

Expand All @@ -10,7 +10,7 @@ export type BlogPost = {
};

export interface NaverSearchBlog<T = boolean> {
total_count: number;
total_count: number | null;
posts: T extends true ? BlogPost[] : null;
}

Expand All @@ -20,9 +20,13 @@ export type SelectedPlace = {
};

export interface PlacesWithSearchResult<T = false> extends PlaceResult {
searchBlogPost: PromiseSettledResult<NaverSearchBlog<T>>;
searchBlogPost: PromiseSettledResult<NaverSearchBlog<T>> | null;
}

export interface SearchPlaces extends TextSearchResponseData {
results: PlacesWithSearchResult[];
}

export interface SearchPlace extends PlaceDetailsResponseData {
result: PlacesWithSearchResult<true>;
}

1 comment on commit 309cea0

@vercel
Copy link

@vercel vercel bot commented on 309cea0 Oct 24, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.