49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import { ApiError } from "@/lib/types"
|
|
import { useState } from "react"
|
|
import { OWNER_ID } from "@/hooks/constants"
|
|
import { root } from '@repo/core'
|
|
import { CategoryController, ListCategoriesResult } from "@repo/sdk"
|
|
import { useError } from "./use-error"
|
|
|
|
export const useCategories = () => {
|
|
const [loading, setLoading] = useState(false)
|
|
const [error, setError] = useState<ApiError | null>(null)
|
|
const [data, setData] = useState<ListCategoriesResult>()
|
|
|
|
const load = async (params?: {
|
|
page?: number
|
|
limit?: number
|
|
isActive?: boolean
|
|
search?: string
|
|
categoryIds?: string[]
|
|
withChildren?: boolean
|
|
ownerId?: string
|
|
}) => {
|
|
try {
|
|
setLoading(true)
|
|
const category = root.get(CategoryController)
|
|
const { data, error } = await useError(async () => await category.list({
|
|
page: params?.page || 1,
|
|
limit: params?.limit || 20,
|
|
orderBy: 'createdAt',
|
|
order: 'desc',
|
|
...params,
|
|
ownerId: OWNER_ID
|
|
}))
|
|
|
|
if (error) {
|
|
setError(error)
|
|
return
|
|
}
|
|
|
|
setData(data)
|
|
} catch (e) {
|
|
setError(e as ApiError)
|
|
} finally {
|
|
setLoading(false)
|
|
}
|
|
}
|
|
|
|
return { load, loading, error, data }
|
|
}
|