import React, { useEffect, useState } from 'react' import { Head, Link, router, useForm } from '@inertiajs/react' import { usePrevious } from 'react-use' import { HiXCircle } from 'react-icons/hi' import { dateToString, formatIDR } from '@/utils' import AuthenticatedLayout from '@/Layouts/AuthenticatedLayout' import SearchInput from '@/Components/SearchInput' import Button from '@/Components/Button' import FormInputDate from '@/Components/FormInputDate' import Pagination from '@/Components/Pagination' import FormInput from '@/Components/FormInput' import CustomerSelectionInput from '../Customer/SelectionInput' import { Spinner } from 'flowbite-react' import TextArea from '@/Components/TextArea' export default function Sale(props) { const { _products: { data: products, links }, _page, } = props const [loading, setLoading] = useState(false) const [search, setSearch] = useState('') const preValue = usePrevious(search) const { data, setData, post, processing, errors } = useForm({ date: dateToString(new Date()), customer_id: null, items: [], note: '', }) const addItem = (product) => { const isExist = data.items.find((item) => item.id === product.id) if (isExist) { return } setData( 'items', data.items.concat({ ...product, qty: 1, }) ) } const removeItem = (id) => { setData( 'items', data.items.filter((item) => item.id !== id) ) } const setQuantityItem = (id, qty) => { setData( 'items', data.items.map((item) => { if (item.id === id) { return { ...item, qty: qty, } } return item }) ) } const handleSubmit = () => { post(route('sale.store')) } const params = { q: search } useEffect(() => { if (preValue) { setLoading(true) router.get( route(route().current()), { q: search }, { replace: true, preserveState: true, onSuccess: () => { setLoading(false) }, } ) } }, [search]) const total = data.items.reduce( (amt, item) => amt + +item.qty * +item.price, 0 ) return (
setSearch(e.target.value)} value={search} />
{loading ? (
) : (
{products.map((item) => (
addItem(item)} >
{item.name}
Rp. {formatIDR(item.price)}
))}
)}