diff options
author | MikunoNaka <bokuwakanojogahoshii@yahoo.com> | 2021-05-05 11:00:28 +0530 |
---|---|---|
committer | MikunoNaka <bokuwakanojogahoshii@yahoo.com> | 2021-05-05 11:00:28 +0530 |
commit | 5b630590d7e2f803007911e22f24444aefef541d (patch) | |
tree | 21d0a567bd7fcf9db46d2035dbcd540a22fe2d86 /src/components | |
parent | 499c54e8eb53cf49d8ddae68528eb3a8243101d2 (diff) |
made the code less error prone and more oriented towards the functional paradigm
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/BillingPage.js | 37 | ||||
-rw-r--r-- | src/components/Display/SummaryDisplay.js | 23 | ||||
-rw-r--r-- | src/components/Form/Items/AddNewItemForm.js | 44 |
3 files changed, 40 insertions, 64 deletions
diff --git a/src/components/BillingPage.js b/src/components/BillingPage.js index 71d7c81..cb9e74c 100644 --- a/src/components/BillingPage.js +++ b/src/components/BillingPage.js @@ -25,47 +25,28 @@ const BillingPage = () => { const [savedPeople, getSavedPeople] = useState([]); const [registerItemFormVisibility, setRegisterItemFormVisibility] = useState(false); const [registerPersonFormVisibility, setRegisterPersonFormVisibility] = useState(false); + const [items, setItems] = useState([]); - const getRegisteredItems = () => { + const getRegisteredItems = () => axios.get(`/api/items/get-all`) - .then((res) => { - getSavedItems(res.data); - }) - .catch((res) => { - alert("The promise returned an error idk what to do"); - console.log(res); - }) - } - - const getRegisteredPeople = () => { + .then((res) => getSavedItems(res.data)) + .catch((res) => console.log(res)); + + const getRegisteredPeople = () => axios.get(`/api/people/get-all`) - .then((res) => { - getSavedPeople(res.data); - }) - .catch((res) => { - alert("The promise returned an error idk what to do"); - console.log(res); - }) - } + .then((res) => getSavedPeople(res.data)) + .catch((res) => console.log(res)); // get data from server on startup useEffect(() => { - async function fetchdata() { getRegisteredItems(); getRegisteredPeople(); - } - fetchdata() }, []); // TODO: to be handled by backend const defGSTValue = 18; // update the items from AddNewItemForm - const [items, setItems] = useState([]); - const getItems = (item) => { - setItems( - [...items, item] - ); - }; + const getItems = (item) => setItems([...items, item]); return ( <> diff --git a/src/components/Display/SummaryDisplay.js b/src/components/Display/SummaryDisplay.js index 2f65e79..9b1ca6b 100644 --- a/src/components/Display/SummaryDisplay.js +++ b/src/components/Display/SummaryDisplay.js @@ -13,10 +13,12 @@ const getBasicSummary = (items) => { let totalRawPrice = 0; let totalQuantity = 0; - for (let i = 0; i < items.length; i++) { - totalRawPrice += items[i].TotalPrice; - totalQuantity += items[i].Quantity - } + items.some((i) => { + totalRawPrice += i.TotalPrice; + totalQuantity += i.Quantity; + return null; + } + ) return ( { @@ -31,14 +33,17 @@ const getFullSummary = (items) => { let totalDiscount = 0; // to be subtracted from totalRawPrice let totalTax = 0; - for (let i = 0; i < items.length; i++) { - const itemTotalPrice = items[i].TotalPrice; - const itemDiscount = (items[i].Discount / 100) * itemTotalPrice; + items.some((i) => { + const itemTotalPrice = i.TotalPrice; + const itemDiscount = (i.Discount / 100) * itemTotalPrice; totalRawPrice += itemTotalPrice; totalDiscount += itemDiscount; - totalTax += (items[i].GST / 100) * (itemTotalPrice - itemDiscount); - } + totalTax += (i.GST / 100) * (itemTotalPrice - itemDiscount); + return null; + }) + + const totalPriceAfterTax = (totalRawPrice - totalDiscount) + totalTax; const totalRoundedOff = Math.abs(totalPriceAfterTax - Math.round(totalPriceAfterTax)); 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> |