import { Link, router } from '@inertiajs/react'; import { CardTable } from '@/components/card-table'; import { DeleteConfirmDialog } from '@/components/delete-confirm-dialog'; import { FilterPopover } from '@/components/filter-popover'; import { useCardTableExpand } from '@/components/hooks/use-card-table-expand'; import { PageHeader } from '@/components/page-header'; import { Button } from '@/components/ui/button'; import { Combobox, ComboboxContent, ComboboxEmpty, ComboboxInput, ComboboxItem, ComboboxList, } from '@/components/ui/combobox'; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from '@/components/ui/select'; import { useCan } from '@/hooks/use-can'; import { useServerTable } from '@/hooks/use-server-table'; import { destroy, create as productCreate, edit as productEdit, index as productIndex, toggleStatus, } from '@/routes/admin/master/products'; import { destroy as variantDestroy, edit as variantEdit, } from '@/routes/admin/master/products/variants'; import { Head } from '@inertiajs/react'; import { Plus } from 'lucide-react'; import { useMemo, useState } from 'react'; import type { Product, ProductVariant } from './columns'; import { ProductCardRow } from './product-card'; import { VariantSubRow } from './variant/sub-row'; type Props = { products: { data: Product[]; current_page: number; last_page: number; per_page: number; total: number; }; categories: { id: number; name: string; }[]; productNames: string[]; filters: { status?: string; name?: string; stock?: string; category?: string; }; }; export default function ProductIndex({ products, categories, productNames, filters }: Props) { const { can } = useCan(); const [deleting, setDeleting] = useState(null); const [deletingVariant, setDeletingVariant] = useState<{ product: Product; variant: ProductVariant; } | null>(null); const expand = useCardTableExpand(true); const pagination = { current_page: products.current_page, last_page: products.last_page, per_page: products.per_page, total: products.total, }; const { search, filterOpen, setFilterOpen, handlePageChange, handlePerPageChange, handleSearchChange, applyFilter, clearFilters, } = useServerTable({ route: () => productIndex.url(), pagination, filters, filterWithParams: false, }); const sortedProductNames = useMemo( () => [...productNames].sort(), [productNames], ); const selectedCategory = useMemo( () => categories.find((c) => String(c.id) === filters.category) ?? null, [categories, filters.category], ); function handleDelete() { if (!deleting) { return; } router.delete(destroy.url(deleting.id), { onSuccess: () => setDeleting(null), }); } function handleDeleteVariant() { if (!deletingVariant) { return; } router.delete( variantDestroy.url({ product: deletingVariant.product.id, variant: deletingVariant.variant.id, }), { onSuccess: () => setDeletingVariant(null), }, ); } const filterToolbar = (
applyFilter('name', (value as string) ?? '') } > Tidak ada produk ditemukan. {(name) => ( {name} )}
Berdasarkan stok bagus
cat.name} value={selectedCategory} onValueChange={(value) => applyFilter('category', value ? String(value.id) : '') } > Tidak ada kategori ditemukan. {(cat) => ( {cat.name} )}
); return ( <>
Tambah ) : undefined } /> p.id} expandedKeys={expand.expandedKeys} onToggleExpand={expand.toggleExpand} searchValue={search} onSearchChange={handleSearchChange} pagination={pagination} onPageChange={handlePageChange} onPerPageChange={handlePerPageChange} toolbar={filterToolbar} renderCard={({ item, index, isExpanded, onToggleExpand, }) => ( { router.visit(productEdit.url(p.id)); }} onDelete={(p) => setDeleting(p)} toggleStatusUrl={(id) => toggleStatus.url(id)} /> )} renderSubContent={(product) => ( { router.visit( variantEdit.url({ product: p.id, variant: v.id, }), ); }} onDeleteVariantClick={(p, v) => setDeletingVariant({ product: p, variant: v }) } /> )} /> { if (!open) { setDeleting(null); } }} title="Hapus Produk" description={(product) => `Apakah Anda yakin ingin menghapus produk "${product.name}"? Tindakan ini tidak dapat dibatalkan.` } onConfirm={handleDelete} /> { if (!open) { setDeletingVariant(null); } }} title="Hapus Varian" description={(target) => `Apakah Anda yakin ingin menghapus varian "${target.variant.name}" dari produk "${target.product.name}"? Tindakan ini tidak dapat dibatalkan.` } onConfirm={handleDeleteVariant} />
); }