expo-popcore-app/hooks/CLAUDE.md

1.4 KiB
Raw Permalink Blame History

使用apps\popcore\lib\auth.ts中的loomart接口 封装 hooks 供页面使用

实例代码:

import { loomart } from "@/lib/auth"
import { ApiError } from "@/lib/types"
import { useState } from "react"
export const useActivates = () => {
    const [loading, setLoading] = useState<boolean>(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 | undefined;
            isActive?: boolean | null | undefined;
            sortOrder?: number | null | undefined;
        }[];
        total: number;
        page: number;
        limit: number;
        totalPages: number;
    }>()
    const load = async () => {
        try {
            setLoading(true)
            const { data, error } = await loomart.activities.list({ isActive: true })
            if (error) {
                setError(error)
                return;
            }
            setData(data)
        } catch (e) {
            setLoading(false)
        }
    }

    return {
        load,
        loading,
        error,
        data
    }
}