58 lines
1.3 KiB
TypeScript
58 lines
1.3 KiB
TypeScript
import { root } from '@repo/core'
|
|
import { type TemplateGeneration, TemplateGenerationController } from '@repo/sdk'
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { type ApiError } from '@/lib/types'
|
|
|
|
import { handleError } from './use-error'
|
|
|
|
interface GetParams {
|
|
id: string
|
|
}
|
|
|
|
export const useTemplateGenerationDetail = () => {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<ApiError | null>(null)
|
|
const [data, setData] = useState<TemplateGeneration | undefined>()
|
|
|
|
const load = useCallback(async (params: GetParams) => {
|
|
setLoading(true)
|
|
setError(null)
|
|
|
|
const templateGeneration = root.get(TemplateGenerationController)
|
|
const { data, error } = await handleError(async () => await templateGeneration.get(params.id))
|
|
|
|
if (error) {
|
|
setError(error)
|
|
setLoading(false)
|
|
return { data: undefined, error }
|
|
}
|
|
|
|
setData(data)
|
|
setLoading(false)
|
|
return { data, error: null }
|
|
}, [])
|
|
|
|
const refetch = useCallback(
|
|
(params: GetParams) => {
|
|
return load(params)
|
|
},
|
|
[load],
|
|
)
|
|
|
|
const reset = useCallback(() => {
|
|
setData(undefined)
|
|
setError(null)
|
|
setLoading(false)
|
|
}, [])
|
|
|
|
return {
|
|
data,
|
|
loading,
|
|
error,
|
|
load,
|
|
refetch,
|
|
reset,
|
|
}
|
|
}
|