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

57 lines
1.4 KiB
TypeScript

import { ApiError } from "@/lib/types"
import { useState } from "react"
import { OWNER_ID } from "@/hooks/constants"
import { root } from '@repo/core';
import { ActivityController } from "@repo/sdk";
import { useError } from "./use-error";
export const useActivities = () => {
const [loading, setLoading] = useState(false)
const [error, setError] = useState<ApiError | null>(null)
const [data, setData] = useState<{
activities: {
title: string
titleEn: string
desc: string
descEn: string
coverUrl: string
link: string
createdAt: Date
updatedAt: Date
id: string
videoUrl?: string | null
ownerId?: string | null
isActive?: boolean | null
sortOrder?: number | null
}[]
total: number
page: number
limit: number
totalPages: number
}>()
const load = async (params: {
page: number;
limit: number;
orderBy: string;
order: string;
} = { page: 1, limit: 10, orderBy: 'createdAt', order: 'desc' }) => {
try {
setLoading(true)
const activity = root.get(ActivityController)
const { data, error } = await useError(async () => await activity.list({ isActive: true, ownerId: OWNER_ID, ...params }))
if (error) {
setError(error)
return
}
setData(data)
} catch (e) {
setError(e as ApiError)
} finally {
setLoading(false)
}
}
return { load, loading, error, data }
}