54 lines
1.3 KiB
TypeScript
54 lines
1.3 KiB
TypeScript
import { ApiError } from "@/lib/types"
|
|
import { useState, useCallback } from "react"
|
|
import type { TemplateGeneration } from "./use-template-generations"
|
|
import { root } from '@repo/core'
|
|
import { TemplateGenerationController } from "@repo/sdk"
|
|
import { useError } 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 useError(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,
|
|
}
|
|
}
|