Skip to content

Commit

Permalink
refactor: API 엔드포인트 주소 상수로 분리 (#93)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanafromjeju committed Jan 26, 2025
1 parent 4c6cf03 commit 8d9b4df
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
3 changes: 2 additions & 1 deletion app/(main)/log/hooks/useLogData.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { API_ENDPOINTS } from '@/constants/api'
import { useData } from '@/hooks/useData'

import { LogListResponse } from '../types'

export const useLogData = () => {
const { data, isLoading, error } = useData<LogListResponse>({
url: '/api/v1/driving-log?page=1&size=8',
url: `${API_ENDPOINTS.LOG}`,
})
return { logData: data, isLoading, error }
}
9 changes: 9 additions & 0 deletions constants/api.ts
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
export const API_URL = process.env.NEXT_PUBLIC_API_URL

export interface UseDataRequest {
url: string
params?: Record<string, string | number>
}

export const API_ENDPOINTS = {
LOG: '/api/v1/driving-log',
} as const
11 changes: 3 additions & 8 deletions hooks/useData.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { useEffect, useState } from 'react'

import { UseDataRequest } from '@/constants/api'
import { apiClient } from '@/lib/apis/client'

export interface UseDataProps {
url: string
params?: Record<string, string | number>
path?: string
}

export const useData = <T>({ url, params, path }: UseDataProps) => {
export const useData = <T>({ url, params }: UseDataRequest) => {
const [data, setData] = useState<T>()
const [isLoading, setIsLoading] = useState(true)
const [error, setError] = useState<string | null>(null)
Expand All @@ -27,7 +22,7 @@ export const useData = <T>({ url, params, path }: UseDataProps) => {
}
}
getData()
}, [url, params, path])
}, [url, params])

return { data, isLoading, error }
}

0 comments on commit 8d9b4df

Please sign in to comment.