Sfoglia il codice sorgente

Add validation to recipe creation

Lee Morgan 6 anni fa
parent
commit
93e876262c

+ 2 - 1
models/merchant.js

@@ -20,7 +20,8 @@ const MerchantSchema = new mongoose.Schema({
         },
         quantity: {
             type: Number,
-            min: [0, "Quantity cannot be less than 0"]
+            min: [0, "Quantity cannot be less than 0"],
+            required: true
         }
     }],
     recipes: [{

+ 1 - 0
models/recipe.js

@@ -17,6 +17,7 @@ const RecipeSchema = new mongoose.Schema({
         },
         quantity: {
             type: Number,
+            min: [0, "Quantity cannot be a negative number"],
             required: true
         }
     }]

+ 64 - 46
views/merchantSetupPage/recipeSetup.js

@@ -54,7 +54,7 @@ let recipeSetup = {
             ingQuant.type = "number";
             ingQuant.step = "0.01";
             ingQuant.value = ing.quantity;
-            ingQuant.onblur = ()=>{checkValid("quantity", ingQuant.value)};
+            ingQuant.onblur = ()=>{checkValid("quantity", ingQuant)};
             quantTd.appendChild(ingQuant);
         }
     
@@ -75,72 +75,90 @@ let recipeSetup = {
         }
     },
 
+    //Adds ingredient data to recipeData
     //Empties all data in the table
     //Changes recipeDataIndex
     //Hands off to showRecipe function
     changeRecipe: function(num){
         let body = document.querySelector("#recipes tbody");
+        this.recipeData[this.recipeDataIndex].ingredients = [];
+        let isValid = true;
     
-        let recipeIngredients = [];
-        while(body.children.length > 0){
-            let row = body.firstChild;
-            recipeIngredients.push({
+        for(let row of body.children){
+            newIngredient ={
                 id: row.children[0].children[0].value,
                 quantity: row.children[1].children[0].value
-            });
-            this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
-    
-            body.removeChild(row);
+            };
+
+            this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
+            if(!validator.ingredient.quantity(newIngredient.quantity)){
+                isValid = false;
+                break;
+            }
+        }
+
+        if(isValid){
+            while(body.children.length > 0){
+                body.removeChild(body.firstChild);
+            }
+
+            this.recipeDataIndex += num;
+            this.showRecipe();
         }
-        this.recipeDataIndex += num;
-        this.showRecipe();
     },
 
     //Add all recipes to data variable
     //Creates a form and submits data
     submitAll: function(){
+        this.recipeData[this.recipeDataIndex].ingredients = [];
         let body = document.querySelector("#recipes tbody");
         data.recipes = [];
-    
-        let recipeIngredients = [];
-        while(body.children.length > 0){
-            let row = body.firstChild;
-            recipeIngredients.push({
+        let isValid = true;
+
+        for(let row of body.children){
+            newIngredient ={
                 id: row.children[0].children[0].value,
                 quantity: row.children[1].children[0].value
-            });
-            this.recipeData[this.recipeDataIndex].ingredients = recipeIngredients;
-    
-            body.removeChild(row);
-        }
-
-        for(let recipe of this.recipeData){
-            let newRecipe = {
-                cloverId: recipe.id,
-                name: recipe.name,
-                ingredients: []
             };
-            for(let ingredient of recipe.ingredients){
-                newRecipe.ingredients.push({
-                    id: ingredient.id,
-                    quantity: ingredient.quantity
-                });
+            
+            this.recipeData[this.recipeDataIndex].ingredients.push(newIngredient);
+            if(!validator.ingredient.quantity(newIngredient.quantity)){
+                
+                isValid = false;
+                break;
             }
-            data.recipes.push(newRecipe);
         }
+
+        if(isValid){
+            for(let recipe of this.recipeData){
+                let newRecipe = {
+                    cloverId: recipe.id,
+                    name: recipe.name,
+                    ingredients: []
+                };
+
+                for(let ingredient of recipe.ingredients){
+                    newRecipe.ingredients.push({
+                        id: ingredient.id,
+                        quantity: ingredient.quantity
+                    });
+                }
+                data.recipes.push(newRecipe);
+            }
+            
+            let form = document.createElement("form");
+            form.method = "post";
+            form.action = "/merchant/create"
+            
+            let dataInput = document.createElement("input");
+            dataInput.type = "hidden";
+            dataInput.name = "data";
+            dataInput.value = JSON.stringify(data);
         
-        let form = document.createElement("form");
-        form.method = "post";
-        form.action = "/merchant/create"
-        
-        let dataInput = document.createElement("input");
-        dataInput.type = "hidden";
-        dataInput.name = "data";
-        dataInput.value = JSON.stringify(data);
-    
-        form.appendChild(dataInput);
-        document.body.appendChild(form);
-        form.submit();
+            form.appendChild(dataInput);
+            document.body.appendChild(form);
+            form.submit();
+        }
     },
 
     //Creates a new, empty row in table to input data
@@ -167,7 +185,7 @@ let recipeSetup = {
         ingQuant.type = "number";
         ingQuant.step = "0.01";
         ingQuant.min = "0";
-        ingQuant.onblur = ()=>{checkValid("quantity", ingQuant.value)};
+        ingQuant.onblur = ()=>{checkValid("quantity", ingQuant)};
         quantTd.appendChild(ingQuant);
     
         let removeTd = document.createElement("td");

+ 1 - 1
views/shared/validation.js

@@ -22,7 +22,7 @@ let validator = {
             return true;
         },
 
-        quantity: function(num, createBanner){
+        quantity: function(num, createBanner = true){
             if(num < 0){
                 if(createBanner){
                     banner.createError("Quantity cannot be a negative number");