-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: ์๋์์ฑ ์ปดํฌ๋ํธ ๊ตฌํ * feat: getPoi ๊ตฌํ * feat: ์๋์์ฑ ์ปดํฌ๋ํธ ๊ตฌํ * feat: ํ ์ถ๊ฐ ํ์ด์ง์ ์๋์์ฑ ์ ๋ ฅ์ฐฝ ์ถ๊ฐ * refactor: ์ค ๋ ๋ฒจ ์ค์ ์กฐ์ ๋ฐ ํ์ ์ถ๊ฐ * refactor: tmap api key ์ถ๊ฐ * feat: Poi ํ์ ์ค์ * feat: ์๋์์ฑ debounce ์ ์ฉ * refactor: ๋ฆฌ๋ทฐ ๋ฐ์ * refactor: 2์ฐจ ๋ฆฌ๋ทฐ ๋ฐ์
- Loading branch information
Showing
7 changed files
with
258 additions
and
50 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,19 @@ | ||
import { PoiApiResponse } from '../types/Poi'; | ||
|
||
export const getPoiApi = async (query: string): Promise<PoiApiResponse> => { | ||
const response = await fetch( | ||
`https://apis.openapi.sk.com/tmap/pois?version=1&format=json&callback=result&searchKeyword=${query}&resCoordType=WGS84GEO&reqCoordType=WGS84GEO&count=10`, | ||
{ | ||
method: 'GET', | ||
headers: { appKey: process.env.TMAP_API_KEY || '' }, | ||
}, | ||
); | ||
|
||
if (response.status >= 400) { | ||
throw new Error('[POI] GET ์์ฒญ์ ์คํจํ์ต๋๋ค.'); | ||
} | ||
|
||
const responseData = await response.json(); | ||
|
||
return responseData; | ||
}; |
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,133 @@ | ||
/* eslint-disable react/function-component-definition */ | ||
import { memo, useEffect, useRef, useState } from 'react'; | ||
import styled from 'styled-components'; | ||
|
||
import { getPoiApi } from '../../../apis/getPoiApi'; | ||
import { Poi } from '../../../types/Poi'; | ||
import Input from '.'; | ||
import Text from '../Text'; | ||
|
||
interface AutocompleteProps { | ||
defaultValue?: string; | ||
onSuggestionSelected: (suggestion: Poi) => void; | ||
} | ||
|
||
const Autocomplete = ({ | ||
defaultValue = '', | ||
onSuggestionSelected, | ||
}: AutocompleteProps) => { | ||
const [inputValue, setInputValue] = useState<string>(defaultValue); | ||
const [suggestions, setSuggestions] = useState<Poi[]>([]); | ||
const [selectedSuggestion, setSelectedSuggestion] = useState< | ||
Poi['name'] | null | ||
>(null); | ||
|
||
const debounceTimeoutRef = useRef<NodeJS.Timeout | null>(null); | ||
const fetchData = async (query: string) => { | ||
try { | ||
const fetchedSuggestions = await getPoiApi(query); | ||
|
||
if (!fetchedSuggestions) | ||
throw new Error('์ถ์ฒ ๊ฒ์์ด๋ฅผ ๋ถ๋ฌ์ค์ง ๋ชปํ์ต๋๋ค.'); | ||
|
||
setSuggestions(fetchedSuggestions.searchPoiInfo.pois.poi); | ||
} catch (error) { | ||
setSuggestions([]); | ||
console.error(error); | ||
} | ||
}; | ||
|
||
const onInputChange = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
if (e.target.value.trim() === '') { | ||
setSuggestions([]); | ||
setInputValue(''); | ||
return; | ||
} | ||
|
||
setInputValue(e.target.value); | ||
|
||
if (debounceTimeoutRef.current !== null) { | ||
clearTimeout(debounceTimeoutRef.current); | ||
} | ||
|
||
debounceTimeoutRef.current = setTimeout( | ||
() => fetchData(e.target.value), | ||
500, | ||
); | ||
}; | ||
|
||
const onClickSuggestion = (suggestion: Poi) => { | ||
const { name } = suggestion; | ||
setInputValue(name); | ||
setSelectedSuggestion(name); | ||
onSuggestionSelected(suggestion); | ||
}; | ||
|
||
useEffect(() => { | ||
setInputValue(defaultValue); | ||
}, [defaultValue]); | ||
|
||
return ( | ||
<> | ||
<AutocompleteInput | ||
type="text" | ||
value={inputValue} | ||
onChange={onInputChange} | ||
placeholder="์ฅ์๋ฅผ ์ง์ ์ ๋ ฅํ๊ฑฐ๋ ์ง๋์์ ํด๋ฆญํ์ธ์." | ||
onClick={() => setSelectedSuggestion(null)} | ||
/> | ||
|
||
{!selectedSuggestion && ( | ||
<SuggestionsList> | ||
{suggestions?.map((suggestion: Poi, index: number) => ( | ||
<SuggestionItem | ||
key={index} | ||
onClick={() => { | ||
onClickSuggestion(suggestion); | ||
}} | ||
> | ||
{suggestion.name} | ||
<Address $fontSize="small" color="gray" $fontWeight="normal"> | ||
{suggestion.upperAddrName} {suggestion.middleAddrName}{' '} | ||
{suggestion.roadName} | ||
</Address> | ||
</SuggestionItem> | ||
))} | ||
</SuggestionsList> | ||
)} | ||
</> | ||
); | ||
}; | ||
|
||
export default memo(Autocomplete); | ||
|
||
const AutocompleteInput = styled(Input)` | ||
width: 100%; | ||
`; | ||
|
||
const SuggestionsList = styled.ul` | ||
border: 1px solid #ccc; | ||
border-top: none; | ||
border-bottom: none; | ||
border-radius: 4px; | ||
box-shadow: 0px 4px 5px -2px rgba(0, 0, 0, 0.3); | ||
`; | ||
|
||
const SuggestionItem = styled.li` | ||
padding: ${({ theme }) => theme.spacing['2']}; | ||
cursor: pointer; | ||
&:hover { | ||
background-color: #f7f7f7; | ||
} | ||
`; | ||
|
||
const Address = styled(Text)``; | ||
|
||
const Description = styled.div` | ||
font-size: ${({ theme }) => theme.fontSize.small}; | ||
white-space: nowrap; | ||
overflow: hidden; | ||
text-overflow: ellipsis; | ||
`; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
export interface EvCharger { | ||
evCharger: any[]; | ||
} | ||
|
||
export interface NewAddress { | ||
centerLat: string; | ||
centerLon: string; | ||
frontLat: string; | ||
} | ||
|
||
export interface NewAddressList { | ||
newAddress: NewAddress[]; | ||
} | ||
|
||
export interface Poi { | ||
id: string; | ||
pkey: string; | ||
navSeq: string; | ||
collectionType: string; | ||
name: string; | ||
|
||
adminDongCode: string; | ||
bizName: string; | ||
dataKind: string; | ||
desc: string; | ||
|
||
evChargers: EvCharger; | ||
firstBuildNo: string; | ||
firstNo: string; | ||
frontLat: string; | ||
frontLon: string; | ||
|
||
legalDongCode: string; | ||
|
||
lowerAddrName: string; | ||
|
||
middleAddrName: string; | ||
mlClass: string; | ||
|
||
newAddressList: NewAddressList; | ||
|
||
noorLat: string; | ||
noorLon: string; | ||
|
||
parkFlag: string; | ||
|
||
radius: string; | ||
|
||
roadName: string; | ||
|
||
rpFlag: string; | ||
|
||
secondBuildNo: string; | ||
|
||
secondNo: string; | ||
|
||
telNo: string; | ||
|
||
upperAddrName: string; | ||
|
||
zipCode: String; | ||
} | ||
|
||
export interface Pois { | ||
poi: Poi[]; | ||
} | ||
|
||
export interface SearchPoiInfo { | ||
totalCount: string; | ||
count: string; | ||
page: string; | ||
pois: Pois; | ||
} | ||
|
||
export interface PoiApiResponse { | ||
searchPoiInfo: SearchPoiInfo; | ||
} |
Oops, something went wrong.