From 39819430fa47172c1a3a36218339f6baf86f8983 Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Tue, 27 Sep 2022 21:42:51 +0530 Subject: added edit option for items and brands --- src/components/editors/item-editor.js | 90 +++++++++++++++++++---------------- 1 file changed, 50 insertions(+), 40 deletions(-) (limited to 'src/components/editors/item-editor.js') diff --git a/src/components/editors/item-editor.js b/src/components/editors/item-editor.js index 6c5e976..e2d9d94 100644 --- a/src/components/editors/item-editor.js +++ b/src/components/editors/item-editor.js @@ -15,22 +15,22 @@ * along with this program. If not, see . */ -import { Item, saveItem } from './../../classes/item' -import { Brand, getAllBrands } from './../../classes/brand' -import './scss/item-editor.scss' +import { Item, saveItem, editItem } from './../../classes/item'; +import { Brand, getAllBrands } from './../../classes/brand'; +import './scss/item-editor.scss'; import { useState, useEffect } from 'react'; const ItemEditor = (props) => { - const [name, setName] = useState(""); - const [desc, setDesc] = useState(""); - const [HSN, setHSN] = useState(""); - const [unit, setUnit] = useState(""); - const [unitPrice, setUnitPrice] = useState(0.00); - const [gstP, setGSTP] = useState(0.00); - const [minQty, setMinQty] = useState(0.00); - const [maxQty, setMaxQty] = useState(0.00); - const [brand, setBrand] = useState(new Brand()); + const [name, setName] = useState(props.item.Name); + const [desc, setDesc] = useState(props.item.Description); + const [HSN, setHSN] = useState(props.item.HSN); + const [unit, setUnit] = useState(props.item.UnitOfMeasure); + const [unitPrice, setUnitPrice] = useState(props.item.UnitPrice); + const [gstP, setGSTP] = useState(props.item.GSTPercentage); + const [minQty, setMinQty] = useState(props.item.MinQuantity); + const [maxQty, setMaxQty] = useState(props.item.MaxQuantity); + const [brand, setBrand] = useState(props.item.Brand); const [savedBrands, setSavedBrands] = useState([]); // get saved brands from API @@ -48,19 +48,22 @@ const ItemEditor = (props) => { item.Description = desc; item.HSN = HSN; item.UnitOfMeasure = unit; - item.UnitPrice = unitPrice; - item.GSTPercentage = gstP; - item.MinQuantity = minQty; - item.MaxQuantity = maxQty; + item.UnitPrice = parseFloat(unitPrice); + item.GSTPercentage = parseFloat(gstP); + item.MinQuantity = parseFloat(minQty); + item.MaxQuantity = parseFloat(maxQty); item.Brand = brand; // TODO: Save is for new items. implement modification too - saveItem(item, handleSuccess, handleFail); + props.editing + ? editItem(item, handleSuccess, handleFail) + : saveItem(item, handleSuccess, handleFail); } const handleSuccess = () => { clearAll(); props.callback(); + props.editing && props.hide(); } const handleFail = () => { @@ -85,73 +88,80 @@ const ItemEditor = (props) => { } const handleCancel = () => { - // TODO: hide this component or something clearAll(); - } - - const validateFloatInput = (e, callback) => { - const f = parseFloat(e.target.value); - f && callback(f) + props.editing && props.hide(); } return ( -
+

{props.heading}

{savedBrands && savedBrands.length > 0 && -- cgit v1.2.3