From 5b630590d7e2f803007911e22f24444aefef541d Mon Sep 17 00:00:00 2001 From: MikunoNaka Date: Wed, 5 May 2021 11:00:28 +0530 Subject: made the code less error prone and more oriented towards the functional paradigm --- src/components/Form/Items/AddNewItemForm.js | 44 +++++++++++------------------ 1 file changed, 17 insertions(+), 27 deletions(-) (limited to 'src/components/Form') diff --git a/src/components/Form/Items/AddNewItemForm.js b/src/components/Form/Items/AddNewItemForm.js index b2099e7..4b2397c 100644 --- a/src/components/Form/Items/AddNewItemForm.js +++ b/src/components/Form/Items/AddNewItemForm.js @@ -22,34 +22,24 @@ const AddNewItemForm = (props) => { const registerItemPrompt = "add new"; const emptyItemNames = [enterItemNamePrompt, registerItemPrompt, ""]; - // Extract the model names from savedItems - let savedItemNames= []; - if (props.savedItems !== null) { - for (let i = 0; i < props.savedItems.length; i++) { - savedItemNames.push(props.savedItems[i].Model); - } + // set description and price if match found in DB + const applyItemInfo = (i) => { + setItemDescValue(i.Description); + setItemPriceValue(i.Price); + setItemHSNValue(i.HSN); + setItemGSTValue(i.GST); } - - // set description and price - // when item is entered + + // check the item name value and do stuff accordingly const setItemInfo = (itemName) => { - for (let i = 0; i < props.savedItems.length; i++) { - const mod = props.savedItems[i].Model.toLowerCase(); - const desc = props.savedItems[i].Description; - const price = props.savedItems[i].Price; - const hsn = props.savedItems[i].HSN; - const gst = props.savedItems[i].GST; - - if (mod === itemName) { - setItemDescValue(desc); - setItemPriceValue(price); - setItemHSNValue(hsn); - setItemGSTValue(gst); - break; - } else if (itemName === registerItemPrompt) { - props.registerItemFormVisibility(true); + props.savedItems.some( + (i) => { + itemName === i.Model.toLowerCase() + ? applyItemInfo(i) + : itemName === registerItemPrompt && props.registerItemFormVisibility(true) + return null; } - } + ) } const resetAllValues = () => { @@ -95,9 +85,9 @@ const AddNewItemForm = (props) => { } }> - {savedItemNames.map( + {props.savedItems.map( (i) => { - return + return } )} -- cgit v1.2.3