import React, { useEffect, useState } from 'react' import { router } from '@inertiajs/react' import { usePrevious } from 'react-use' import { Head } from '@inertiajs/react' import { Button, Dropdown } from 'flowbite-react' import { HiPencil, HiTrash } from 'react-icons/hi' import { useModalState } from '@/hooks' import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' import Pagination from '@/Components/Pagination' import ModalConfirm from '@/Components/ModalConfirm' import FormModal from './FormModal' import SearchInput from '@/Components/SearchInput' import { formatIDR, hasPermission } from '@/utils' export default function Product(props) { const { query: { links, data }, auth, } = props const [search, setSearch] = useState('') const preValue = usePrevious(search) const confirmModal = useModalState() const formModal = useModalState() const toggleFormModal = (product = null) => { formModal.setData(product) formModal.toggle() } const handleDeleteClick = (product) => { confirmModal.setData(product) confirmModal.toggle() } const onDelete = () => { if (confirmModal.data !== null) { router.delete(route('product.destroy', confirmModal.data.id)) } } const params = { q: search } useEffect(() => { if (preValue) { router.get( route(route().current()), { q: search }, { replace: true, preserveState: true, } ) } }, [search]) const canCreate = hasPermission(auth, 'create-product') const canUpdate = hasPermission(auth, 'update-product') const canDelete = hasPermission(auth, 'delete-product') return (
{canCreate && ( )}
setSearch(e.target.value)} value={search} />
{data.map((product) => ( ))}
Kode Nama Status Kategori Harga Jual Harga Beli Stok
{product.code} {product.name}
{product.category.name} {formatIDR(product.price)} {formatIDR(product.cost)} {formatIDR(product.stock)} {canUpdate && ( toggleFormModal( product ) } >
Ubah
)} {canDelete && ( handleDeleteClick( product ) } >
Hapus
)}
) }