Skip to content

Commit

Permalink
feat(location): lat, lng 데이터 정규화 처리 유틸함수 구현 (#97)
Browse files Browse the repository at this point in the history
- 현재 위/경도의 1,000,000이 곱한 데이터로 응답
- 위/경도 데이터 변환 유틸 함수 추가
- 차량 정보 API 응답 coordinate 정규화 로직 적용
  • Loading branch information
red-dev-Mark committed Jan 25, 2025
1 parent a8c8fb2 commit 6752f56
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 9 deletions.
13 changes: 6 additions & 7 deletions hooks/useSearchSingleVehicle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,16 @@ export const useSearchSingleVehicle = () => {
return
}

const response = await vehicleAPI.getVehicleInfo(searchTerm)
const result = await vehicleAPI.getVehicleInfo(searchTerm)

console.log(response)

// TODO: 등록되지 않은 차량의 경우 어떤 에러코드가 오는지 확인 후 처리
if (!response.isValid) {
showMessage(response.value)
if (!result.isValid) {
showMessage(result.value as string)
return
}

const vehicleInfo = response.value
if (typeof result.value === 'string') return

const vehicleInfo = result.value

updateMapLocation(
{
Expand Down
10 changes: 8 additions & 2 deletions lib/apis/vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { API_URL } from '@/constants/api'
import { apiClient } from '@/lib/apis/client'
import { normalizeVehicleResponse } from '@/lib/utils/normalize'
// import { DateTime } from '@/app/route/types/date'
// import { formatToISODate } from '@/lib/utils/date'
import mockRoutesData from '@/mock/vehicle_route_data.json'
Expand All @@ -8,15 +9,20 @@ import { VehicleDetailsModel, VehicleStatusType } from '@/types/vehicle'
// 예시 API 구조
export const vehicleAPI = {
getVehicleInfo: async (vehicleNumber: string) => {
const response = await apiClient.get(`${API_URL}/api/v1/vehicles/search?vehicle-number=${vehicleNumber}`)
const response = await apiClient.get(`${API_URL}/api/v1/vehicles/search`, {
params: {
'vehicle-number': vehicleNumber,
},
})

if (!response.data.isSuccess) {
if (response.data.errorCode === 1003) {
return { isValid: false, value: '등록되지 않은 차량입니다.' }
}
}

return { isValid: true, value: response.data.result }
const normalizeResult = normalizeVehicleResponse(response.data.result)
return { isValid: true, value: normalizeResult }
},
// fetchVehicleDetails: async (vehicleId: string) => {
fetchVehicleDetails: async (): Promise<VehicleDetailsModel> => {
Expand Down
15 changes: 15 additions & 0 deletions lib/utils/normalize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { VehicleInfoModel } from '@/types/vehicle'

export const normalizeCoordinate = (coordinateValue: number): number => coordinateValue / 1000000
export const denormalizeCoordinate = (coordinateValue: number): number => coordinateValue * 1000000

export const normalizeVehicleResponse = (data: VehicleInfoModel) => {
return {
...data,
recentCycleInfo: {
...data.recentCycleInfo,
lat: normalizeCoordinate(data.recentCycleInfo.lat),
lng: normalizeCoordinate(data.recentCycleInfo.lng),
},
}
}

0 comments on commit 6752f56

Please sign in to comment.