import React, { useState } from 'react'; import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'; import { Plus, Pencil, Trash2, Search } from 'lucide-react'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter, DialogDescription } from '@/components/ui/dialog'; import { Label } from '@/components/ui/label'; import { Textarea } from '@/components/ui/textarea'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select'; import { api } from '@/lib/api'; import { toast } from 'sonner'; import { __ } from '@/lib/i18n'; interface Attribute { attribute_id: number; attribute_name: string; attribute_label: string; attribute_type: string; attribute_orderby: string; attribute_public: number; } export default function ProductAttributes() { const queryClient = useQueryClient(); const [search, setSearch] = useState(''); const [dialogOpen, setDialogOpen] = useState(false); const [editingAttribute, setEditingAttribute] = useState(null); const [formData, setFormData] = useState({ name: '', label: '', type: 'select', orderby: 'menu_order', public: 1 }); const { data: attributes = [], isLoading } = useQuery({ queryKey: ['product-attributes'], queryFn: async () => { const response = await fetch(`${api.root()}/products/attributes`, { headers: { 'X-WP-Nonce': api.nonce() }, }); return response.json(); }, }); const createMutation = useMutation({ mutationFn: (data: any) => api.post('/products/attributes', data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['product-attributes'] }); toast.success(__('Attribute created successfully')); handleCloseDialog(); }, onError: (error: any) => { toast.error(error?.message || __('Failed to create attribute')); }, }); const updateMutation = useMutation({ mutationFn: ({ id, data }: { id: number; data: any }) => api.put(`/products/attributes/${id}`, data), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['product-attributes'] }); toast.success(__('Attribute updated successfully')); handleCloseDialog(); }, onError: (error: any) => { toast.error(error?.message || __('Failed to update attribute')); }, }); const deleteMutation = useMutation({ mutationFn: (id: number) => api.del(`/products/attributes/${id}`), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['product-attributes'] }); toast.success(__('Attribute deleted successfully')); }, onError: (error: any) => { toast.error(error?.message || __('Failed to delete attribute')); }, }); const handleOpenDialog = (attribute?: Attribute) => { if (attribute) { setEditingAttribute(attribute); setFormData({ name: attribute.attribute_name, label: attribute.attribute_label, type: attribute.attribute_type, orderby: attribute.attribute_orderby, public: attribute.attribute_public, }); } else { setEditingAttribute(null); setFormData({ name: '', label: '', type: 'select', orderby: 'menu_order', public: 1 }); } setDialogOpen(true); }; const handleCloseDialog = () => { setDialogOpen(false); setEditingAttribute(null); setFormData({ name: '', label: '', type: 'select', orderby: 'menu_order', public: 1 }); }; const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); if (editingAttribute) { updateMutation.mutate({ id: editingAttribute.attribute_id, data: formData }); } else { createMutation.mutate(formData); } }; const handleDelete = (id: number) => { if (confirm(__('Are you sure you want to delete this attribute?'))) { deleteMutation.mutate(id); } }; const filteredAttributes = attributes.filter((attr) => attr.attribute_label.toLowerCase().includes(search.toLowerCase()) || attr.attribute_name.toLowerCase().includes(search.toLowerCase()) ); return (

{__('Product Attributes')}

setSearch(e.target.value)} className="!pl-9" />
{isLoading ? (

{__('Loading attributes...')}

) : filteredAttributes.length === 0 ? (

{__('No attributes found')}

) : (
{filteredAttributes.map((attribute, index) => ( ))}
{__('Name')} {__('Slug')} {__('Type')} {__('Order By')} {__('Actions')}
{attribute.attribute_label} {attribute.attribute_name} {attribute.attribute_type} {attribute.attribute_orderby}
)} {editingAttribute ? __('Edit Attribute') : __('Add Attribute')} {editingAttribute ? __('Update attribute information') : __('Create a new product attribute')}
setFormData({ ...formData, label: e.target.value })} placeholder={__('e.g., Color, Size')} required />
setFormData({ ...formData, name: e.target.value })} placeholder={__('Leave empty to auto-generate')} />
); }