/* OpenBills-web - Web based libre billing software * Copyright (C) 2022 Vidhu Kant Sharma * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import { Item, InvoiceItem, getAllItems } from '../../classes/item'; import './scss/item-picker.scss'; import { useState, useEffect } from 'react'; import { Link } from 'react-router-dom'; const ItemPicker = ({invoiceItems, addInvoiceItem}) => { const [items, setItems] = useState([new Item()]); const [item, setItem] = useState(new InvoiceItem()); useEffect(() => refreshItems, []); // when an item is selected, set its quantity to item.MinQuantity useEffect(() => { if (item.Id != null) { setItem(prevItem => ({...prevItem, Quantity: prevItem.MinQuantity})) } }, [item.Id]) const refreshItems = () => getAllItems(setItems, () => {}); const handleItemSelect = e => { const i = items.find(i => i.Id === e.target.value); setItem(prevItem => i ? ({...prevItem, ...i}) : new InvoiceItem()); } const handleInput = e => { const { name, value } = e.target; setItem(prevItem => ({ ...prevItem, [name]: `${value}` })); } const validate = () => { if (!isNumeric(item.Quantity) || !isNumeric(item.DiscountPercentage) || !isNumeric(item.GSTPercentage) || !isNumeric(item.UnitPrice)) { return false; } if (item.Id === null) { return false; } if (!item.UnitPrice > 0) { return false; } if (!item.Quantity > 0) { return false; } if (item.Quantity < item.MinQuantity) { return false; } if (item.MaxQuantity > 0 && item.Quantity > item.MaxQuantity) { return false; } if (item.DiscountPercentage > 100 || item.DiscountPercentage < 0 || item.GSTPercentage > 100 || item.GSTPercentage < 0) { return false; } return true; } // add item to the invoice items list const addItem = (e) => { e.preventDefault(); if (validate()) { addInvoiceItem(item); setItem(new InvoiceItem()); } } const isNumeric = (i) => !isNaN(i) && !isNaN(parseFloat(i)); // input elements are sorted on the basis of // how likely they are going to be edited return (

Add an Item

{items.length > 0 ? <>
: }

); } export default ItemPicker;