expo-duooomi-app/hooks/data/use-categories.ts

47 lines
1.2 KiB
TypeScript

import { root } from '@repo/core'
import { CategoryController, type ListCategoriesInput, type ListCategoriesResult } from '@repo/sdk'
import { useState } from 'react'
import { type ApiError } from '@/lib/types'
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?: ListCategoriesInput) => {
const OWNER_ID = process.env.EXPO_PUBLIC_OWNER_ID || ''
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 }
}