aboutsummaryrefslogtreecommitdiff
path: root/src/components/Form
diff options
context:
space:
mode:
authorMikunoNaka <bokuwakanojogahoshii@yahoo.com>2021-05-05 11:00:28 +0530
committerMikunoNaka <bokuwakanojogahoshii@yahoo.com>2021-05-05 11:00:28 +0530
commit5b630590d7e2f803007911e22f24444aefef541d (patch)
tree21d0a567bd7fcf9db46d2035dbcd540a22fe2d86 /src/components/Form
parent499c54e8eb53cf49d8ddae68528eb3a8243101d2 (diff)
made the code less error prone and more oriented towards the functional paradigm
Diffstat (limited to 'src/components/Form')
-rw-r--r--src/components/Form/Items/AddNewItemForm.js44
1 files changed, 17 insertions, 27 deletions
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) => {
}
}>
<option key={enterItemNamePrompt}>{enterItemNamePrompt}</option>
- {savedItemNames.map(
+ {props.savedItems.map(
(i) => {
- return <option key={i}>{i}</option>
+ return <option key={i.Model}>{i.Model}</option>
}
)}
<option key={registerItemPrompt}>{registerItemPrompt}</option>