'use client'; import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; import { ColumnDef } from '@tanstack/react-table'; import { format } from 'date-fns'; import { Edit, Gift, MoreHorizontal, Plus, Trash2 } from 'lucide-react'; import { useState } from 'react'; import { DataTable } from '@/components/data-table'; import { Button } from '@/components/ui/button'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from '@/components/ui/dialog'; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { apiClient } from '@/lib/api-client'; import { KeygenEntitlement } from '@/lib/types'; import { toast } from 'sonner'; export default function EntitlementsPage() { const [isCreateDialogOpen, setIsCreateDialogOpen] = useState(false); const [isDeleteDialogOpen, setIsDeleteDialogOpen] = useState(false); const [selectedEntitlement, setSelectedEntitlement] = useState(null); const [formData, setFormData] = useState({ name: '', code: '', }); const queryClient = useQueryClient(); const { data, isLoading } = useQuery({ queryKey: ['entitlements'], queryFn: () => apiClient.getEntitlements(1, 100), }); const createMutation = useMutation({ mutationFn: (entitlementData: { name: string; code: string }) => apiClient.createEntitlement(entitlementData), onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ['entitlements'] }); setIsCreateDialogOpen(false); setFormData({ name: '', code: '' }); toast.success('Entitlement created successfully'); }, onError: (error) => { toast.error(error.message); }, }); const deleteMutation = useMutation({ mutationFn: (id: string) => apiClient.deleteEntitlement(id), onSuccess: async () => { await queryClient.invalidateQueries({ queryKey: ['entitlements'] }); setIsDeleteDialogOpen(false); setSelectedEntitlement(null); toast.success('Entitlement deleted successfully'); }, onError: (error) => { toast.error(error.message); }, }); const handleCreate = () => { createMutation.mutate(formData); }; const handleDelete = () => { if (selectedEntitlement) { deleteMutation.mutate(selectedEntitlement.id); } }; const columns: ColumnDef[] = [ { accessorKey: 'attributes.name', header: 'Name', cell: ({ row }) => (
{row.original.attributes.name}
), }, { accessorKey: 'attributes.code', header: 'Code', cell: ({ row }) => ( {row.original.attributes.code} ), }, { accessorKey: 'attributes.created', header: 'Created', cell: ({ row }) => format(new Date(row.original.attributes.created), 'MMM d, yyyy'), }, { id: 'actions', cell: ({ row }) => { const entitlement = row.original; return ( Edit { setSelectedEntitlement(entitlement); setIsDeleteDialogOpen(true); }} className="text-red-600" > Delete ); }, }, ]; if (isLoading) { return ( <>

Loading entitlements...

); } return ( <>

Entitlements

Manage feature entitlements and access controls

{/* Create Entitlement Dialog */} Create Entitlement Define a new feature entitlement for your products
setFormData({ ...formData, name: e.target.value })} placeholder="Premium Features" />
setFormData({ ...formData, code: e.target.value })} placeholder="PREMIUM_FEATURES" />
{/* Delete Entitlement Dialog */} Delete Entitlement Are you sure you want to delete {selectedEntitlement?.attributes.name}? This action cannot be undone.
); }