generated from mbti-nf-team/nextjs-starter
-
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 #204 from mbti-nf-team/feat/place-detail-api
- Loading branch information
Showing
15 changed files
with
212 additions
and
123 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { NextRequest, NextResponse } from 'next/server'; | ||
|
||
import { Client } from '@googlemaps/google-maps-services-js'; | ||
|
||
export const runtime = 'nodejs'; | ||
|
||
const TEN_MINUTES = 600; | ||
|
||
export async function GET(request: NextRequest) { | ||
const client = new Client({}); | ||
|
||
const { searchParams } = new URL(request.url); | ||
const requestHeaders = new Headers(request.headers); | ||
|
||
const placeId = searchParams.get('placeId'); | ||
const sessionToken = requestHeaders.get('session-token'); | ||
|
||
if (!placeId) { | ||
return NextResponse.json(null, { | ||
headers: requestHeaders, | ||
status: 400, | ||
statusText: 'not found place', | ||
}); | ||
} | ||
|
||
const response = await client.placeDetails({ | ||
params: { | ||
key: process.env.NEXT_PUBLIC_GOOGLE_MAP_API_KEY, | ||
place_id: placeId, | ||
region: 'KR', | ||
sessiontoken: sessionToken ?? undefined, | ||
}, | ||
}); | ||
|
||
if (response.status !== 200) { | ||
return NextResponse.json(null, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: requestHeaders, | ||
}); | ||
} | ||
|
||
const thumbnailPhotoReference = response.data.result.photos?.[0].photo_reference; | ||
|
||
if (!thumbnailPhotoReference) { | ||
return NextResponse.json(response.data, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: requestHeaders, | ||
}); | ||
} | ||
|
||
const thumbnailPhotoUrl = `https://maps.googleapis.com/maps/api/place/photo?maxwidth=800&photoreference=${thumbnailPhotoReference}&key=${process.env.NEXT_PUBLIC_GOOGLE_MAP_API_KEY}`; | ||
|
||
return NextResponse.json({ | ||
...response.data, | ||
result: { | ||
...response.data.result, | ||
thumbnail: thumbnailPhotoUrl, | ||
}, | ||
}, { | ||
status: response.status, | ||
statusText: response.statusText, | ||
headers: { | ||
...requestHeaders, | ||
'Cache-Control': 'public, s-maxage=1', | ||
'CDN-Cache-Control': 'public, s-maxage=60', | ||
'Vercel-CDN-Cache-Control': `public, s-maxage=${TEN_MINUTES}`, | ||
}, | ||
}); | ||
} |
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
23 changes: 6 additions & 17 deletions
23
src/components/detail/PlaceDetailWindowContainer/index.tsx
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
This file was deleted.
Oops, something went wrong.
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,39 @@ | ||
import { PlaceDetailsResponseData } from '@googlemaps/google-maps-services-js'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
import { fetchPlaceDetail } from '@/lib/apis/search'; | ||
import { PlaceDetail } from '@/lib/types/google.maps'; | ||
|
||
const hasPlaceId = ( | ||
placeDetail: PlaceDetailsResponseData, | ||
): placeDetail is PlaceDetail => { | ||
if ( | ||
!placeDetail?.result | ||
|| !placeDetail.result?.geometry?.location | ||
|| !placeDetail.result?.place_id | ||
|| !placeDetail.result?.name | ||
) { | ||
return false; | ||
} | ||
|
||
return true; | ||
}; | ||
|
||
function useGetPlaceDetail({ | ||
placeId, sessionToken, | ||
}: { placeId?: string; sessionToken?: string; }) { | ||
const query = useQuery<PlaceDetailsResponseData, undefined, PlaceDetail | undefined>(['placeDetail', placeId], () => fetchPlaceDetail({ placeId: placeId as string, sessionToken }), { | ||
enabled: !!placeId, | ||
select: (placeDetail) => { | ||
if (hasPlaceId(placeDetail)) { | ||
return placeDetail; | ||
} | ||
|
||
return undefined; | ||
}, | ||
}); | ||
|
||
return query; | ||
} | ||
|
||
export default useGetPlaceDetail; |
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
Oops, something went wrong.
214c0df
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
offbeat – ./
offbeat-git-main-nf-team.vercel.app
offbeat-nf-team.vercel.app
unnamed-k-place-frontend.vercel.app
www.offbeat.place
offbeat.place