import { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Link, useNavigate } from 'react-router-dom'; import toast from 'react-hot-toast'; import { Plus, Trash2 } from 'lucide-react'; import { api } from '../../lib/api-client'; import { AppShell, PageHeader, PageBody } from '../../layout/AppShell'; import { Button, Badge, Card, Input, Label, Select } from '../../components/ui/primitives'; import { Table, THead, TBody, TR, TH, TD } from '../../components/ui/table'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from '../../components/ui/dialog'; interface Collection { id: string; name: string; slug: string; type: 'smart' | 'manual'; enabled: boolean; product_count: number; updated_at: string; } export function CollectionsList() { const qc = useQueryClient(); const navigate = useNavigate(); const [newOpen, setNewOpen] = useState(false); const [form, setForm] = useState({ name: '', slug: '', type: 'smart' as 'smart' | 'manual' }); const { data } = useQuery({ queryKey: ['collections'], queryFn: () => api.get<{ collections: Collection[] }>('/collections'), }); const createMut = useMutation({ mutationFn: () => api.post<{ collection: Collection }>('/collections', form), onSuccess: (res) => { setNewOpen(false); setForm({ name: '', slug: '', type: 'smart' }); navigate(`/cms/collections/${res.collection.id}`); }, onError: (e: Error) => toast.error(e.message), }); const deleteMut = useMutation({ mutationFn: (id: string) => api.delete(`/collections/${id}`), onSuccess: () => { toast.success('Collectie verwijderd'); qc.invalidateQueries({ queryKey: ['collections'] }); }, }); const collections = data?.collections ?? []; return ( setNewOpen(true)}> Nieuwe collectie } /> {collections.map((c) => ( ))}
Naam Slug Type Producten Status
{c.name} {c.slug} {c.type} {c.product_count} {c.enabled ? actief : uit}
Nieuwe collectie
{ const name = e.target.value; setForm({ ...form, name, slug: form.slug || name.toLowerCase().replace(/[^a-z0-9]+/g, '-'), }); }} />
setForm({ ...form, slug: e.target.value })} />
); }