Просмотр исходного кода

Add validation for invetory page

Lee Morgan 6 лет назад
Родитель
Сommit
724741d52b
3 измененных файлов с 88 добавлено и 32 удалено
  1. 1 0
      views/inventory/inventory.ejs
  2. 40 32
      views/inventory/inventory.js
  3. 47 0
      views/shared/validation.js

+ 1 - 0
views/inventory/inventory.ejs

@@ -54,6 +54,7 @@
 
         <script>let merchant = <%- JSON.stringify(merchant) %>;</script>
         <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
+        <script src="../shared/validation.js"></script>
         <script src="/inventory/inventory.js"></script>
     </body>
 </html>

+ 40 - 32
views/inventory/inventory.js

@@ -70,6 +70,7 @@ let filter = ()=>{
 let editIngredient = (id, row)=>{
     let quantity = row.children[2];
     let button = row.children[4].children[0];
+    let originalQuantity = quantity.innerText;
 
     let quantityInput = document.createElement("input");
     quantityInput.type = "number";
@@ -80,31 +81,36 @@ let editIngredient = (id, row)=>{
     quantity.appendChild(quantityInput);
 
     button.innerText = "Save";
-    button.onclick = ()=>{updateOne(id, row)};
+    button.onclick = ()=>{updateOne(id, row, originalQuantity)};
 }
 
-let updateOne = (id, row)=>{
+let updateOne = (id, row, originalQuantity)=>{
     let quantityField = row.children[2];
     let quantity = quantityField.children[0].value;
     let button = row.children[4].children[0];
 
     quantityField.removeChild(quantityField.firstChild);
-    quantityField.innerText = quantity;
+
+    if(validator.ingredient.quantity(quantity)){
+        axios.post("/ingredients/update", {
+            id: id,
+            quantity: quantity
+        })
+            .then((ingredient)=>{
+                banner.createNotification("The ingredient has been successfully updated");
+            })
+            .catch((err)=>{
+                banner.createError("There was an error and the ingredient was not updated");
+                console.log(err);
+            });
+
+        quantityField.innerText = quantity;
+    }else{
+        quantityField.innerText = originalQuantity;
+    }
 
     button.innerText = "Edit";
     button.onclick = ()=>{editThis(item.id, row)};
-
-    axios.post("/ingredients/update", {
-        id: id,
-        quantity: quantity
-    })
-        .then((ingredient)=>{
-            banner.createNotification("The ingredient has been successfully updated");
-        })
-        .catch((err)=>{
-            banner.createError("There was an error and the ingredient was not updated");
-            console.log(err);
-        });
 }
 
 let removeIngredient = (id, row)=>{
@@ -146,24 +152,26 @@ let addIngredient = ()=>{
         quantity: content.children[2].children[0].value
     }
 
-    axios.post("/ingredients/createone", newIngredient)
-        .then((ingredient)=>{
-            items.push({
-                id: ingredient._id,
-                name: newIngredient.ingredient.name,
-                category: newIngredient.ingredient.category,
-                quantity: newIngredient.quantity,
-                unit: newIngredient.ingredient.unitType
+    if(validator.ingredient.all(newIngredient)){
+        axios.post("/ingredients/createone", newIngredient)
+            .then((ingredient)=>{
+                items.push({
+                    id: ingredient._id,
+                    name: newIngredient.ingredient.name,
+                    category: newIngredient.ingredient.category,
+                    quantity: newIngredient.quantity,
+                    unit: newIngredient.ingredient.unitType
+                })
+                
+                sortIngredients("name");
+                renderIngredients();
+                banner.createNotification("The new ingredient has been successfully added to your inventory");
             })
-            
-            sortIngredients("name");
-            renderIngredients();
-            banner.createNotification("The new ingredient has been successfully added to your inventory");
-        })
-        .catch((err)=>{
-            banner.createError("There was an error and the ingredient could not be added to your inventory");
-            console.log(err);
-        });
+            .catch((err)=>{
+                banner.createError("There was an error and the ingredient could not be added to your inventory");
+                console.log(err);
+            });
+    }
 
     let modal = document.querySelector(".add-ingredient");
     modal.style.visibility = "hidden";

+ 47 - 0
views/shared/validation.js

@@ -0,0 +1,47 @@
+let validator = {
+    ingredient: {
+        name: function(ingName){
+            if(ingName.length < 2){
+                banner.createError("Ingredient name must contain at least 2 characters");
+                return false;
+            }
+
+            return true;
+        },
+
+        category: function(ingCategory){
+            if(ingCategory.length < 3){
+                banner.createError("Category name must contain at least 3 characters");
+                return false;
+            }
+
+            return true;
+        },
+
+        quantity: function(num){
+            if(num < 0){
+                banner.createError("Quantity cannot be a negative number");
+                return false;
+            }
+
+            return true;
+        },
+
+        unit: (ingUnit)=>{
+            return true;
+        },
+
+        all: function(ingObject){
+            let nameCheck = this.name(ingObject.ingredient.name);
+            let categoryCheck = this.category(ingObject.ingredient.category);
+            let quantityCheck = this.quantity(ingObject.quantity);
+            let unitCheck = this.unit(ingObject.ingredient.unitType);
+
+            if(!nameCheck || !categoryCheck || !quantityCheck || !unitCheck){
+                return false;
+            }
+
+            return true;
+        }
+    }
+}