"use client"; import { useState } from "react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; import { Button, Dialog, FormField, Input, PageHeader, EmptyState } from "@/components/ds"; import { loyaltyApi } from "@/modules/loyalty/services/loyalty-api"; import { useTenantId } from "@/hooks/useTenantId"; import { LoyaltyBreadcrumbs } from "@/modules/loyalty/components/LoyaltyBreadcrumbs"; import { LoyaltyPageLoader, LoyaltyPageError } from "@/modules/loyalty/pages/shared"; import { LoyaltyTablePage, LoyaltyStatusChip } from "@/modules/loyalty/design-system"; export default function ReferralsPage() { const { tenantId } = useTenantId(); const qc = useQueryClient(); const [open, setOpen] = useState(false); const [code, setCode] = useState(""); const [refereeId, setRefereeId] = useState(""); const q = useQuery({ queryKey: ["loyalty", tenantId, "referrals"], queryFn: async () => { const page = await loyaltyApi.referrals.list(tenantId!, { page: 1, page_size: 200 }); return "items" in page ? page.items : (page as never); }, enabled: !!tenantId, }); const attrM = useMutation({ mutationFn: () => loyaltyApi.referrals.attribute(tenantId!, { code, referee_member_id: refereeId, }), onSuccess: () => { toast.success("ارجاع ثبت شد"); setOpen(false); qc.invalidateQueries({ queryKey: ["loyalty", tenantId, "referrals"] }); }, onError: (e: Error) => toast.error(e.message), }); const act = useMutation({ mutationFn: ({ id, action }: { id: string; action: "convert" | "reward" | "cancel" }) => { if (action === "convert") return loyaltyApi.referrals.convert(tenantId!, id); if (action === "reward") return loyaltyApi.referrals.reward(tenantId!, id); return loyaltyApi.referrals.cancel(tenantId!, id, { reason: "cancelled from UI" }); }, onSuccess: () => { toast.success("به‌روز شد"); qc.invalidateQueries({ queryKey: ["loyalty", tenantId, "referrals"] }); }, onError: (e: Error) => toast.error(e.message), }); if (!tenantId || q.isLoading) return ; if (q.error) return q.refetch()} />; return (
setOpen(true)}>ثبت ارجاع} /> ( ), }, { key: "referrer_member_id", header: "معرف" }, { key: "referee_member_id", header: "معرفی‌شونده" }, { key: "actions", header: "عملیات", searchable: false, render: (r) => (
), }, ]} rows={(q.data ?? []) as unknown as Record[]} empty={} /> setOpen(false)} title="ثبت ارجاع">
setCode(e.target.value)} /> setRefereeId(e.target.value)} />
); }