瀏覽代碼

Create project and app.js

Lee Morgan 6 年之前
父節點
當前提交
3e13280206

+ 7 - 21
package-lock.json

@@ -88,14 +88,6 @@
       "resolved": "https://registry.npmjs.org/assign-symbols/-/assign-symbols-1.0.0.tgz",
       "integrity": "sha1-WWZ/QfrdTyDMvCu5a41Pf3jsA2c="
     },
-    "async": {
-      "version": "2.6.2",
-      "resolved": "https://registry.npmjs.org/async/-/async-2.6.2.tgz",
-      "integrity": "sha512-H1qVYh1MYhEEFLsP97cVKqCGo7KfCyTt6uEWqsTBr9SO84oK9Uwbyd/yCW+6rKJLHksBNUVWZDAjfS+Ccx0Bbg==",
-      "requires": {
-        "lodash": "^4.17.11"
-      }
-    },
     "async-each": {
       "version": "1.0.3",
       "resolved": "https://registry.npmjs.org/async-each/-/async-each-1.0.3.tgz",
@@ -1637,11 +1629,6 @@
         "package-json": "^4.0.0"
       }
     },
-    "lodash": {
-      "version": "4.17.15",
-      "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.15.tgz",
-      "integrity": "sha512-8xOcRHvCjnocdS5cpwXQXVzmmh5e5+saE2QGoeQmbKmRS6J3VQppPOIt0MnmE+4xlZoumy0GPG0D0MVIQbNA1A=="
-    },
     "lowercase-keys": {
       "version": "1.0.1",
       "resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-1.0.1.tgz",
@@ -1773,17 +1760,16 @@
       }
     },
     "mongoose": {
-      "version": "5.7.1",
-      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.7.1.tgz",
-      "integrity": "sha512-TJeZHb5h8UGH++ctngh2shgZuZmR9o0D8elxfkWzIpKB6QAFsJPmALtDuD6RYZTS33+CifhDdAMGqs3OpJ3cyQ==",
+      "version": "5.7.4",
+      "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.7.4.tgz",
+      "integrity": "sha512-IgqQS5HIaZ8tG2cib6QllfIw2Wc/A0QVOsdKLsSqRolqJFWOjI0se3vsKXLNkbEcuJ1xziW3e/jPhBs65678Hg==",
       "requires": {
-        "async": "2.6.2",
         "bson": "~1.1.1",
         "kareem": "2.3.1",
         "mongodb": "3.3.2",
         "mongoose-legacy-pluralize": "1.0.2",
         "mpath": "0.6.0",
-        "mquery": "3.2.1",
+        "mquery": "3.2.2",
         "ms": "2.1.2",
         "regexp-clone": "1.0.0",
         "safe-buffer": "5.1.2",
@@ -1809,9 +1795,9 @@
       "integrity": "sha512-i75qh79MJ5Xo/sbhxrDrPSEG0H/mr1kcZXJ8dH6URU5jD/knFxCVqVC/gVSW7GIXL/9hHWlT9haLbCXWOll3qw=="
     },
     "mquery": {
-      "version": "3.2.1",
-      "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.1.tgz",
-      "integrity": "sha512-kY/K8QToZWTTocm0U+r8rqcJCp5PRl6e8tPmoDs5OeSO3DInZE2rAL6AYH+V406JTo8305LdASOQcxRDqHojyw==",
+      "version": "3.2.2",
+      "resolved": "https://registry.npmjs.org/mquery/-/mquery-3.2.2.tgz",
+      "integrity": "sha512-XB52992COp0KP230I3qloVUbkLUxJIu328HBP2t2EsxSFtf4W1HPSOBWOXf1bqxK4Xbb66lfMJ+Bpfd9/yZE1Q==",
       "requires": {
         "bluebird": "3.5.1",
         "debug": "3.1.0",

+ 1 - 1
package.json

@@ -20,7 +20,7 @@
     "axios": "^0.19.0",
     "ejs": "^2.7.1",
     "express": "^4.17.1",
-    "mongoose": "^5.7.1",
+    "mongoose": "^5.7.4",
     "nodemon": "^1.19.3"
   }
 }

+ 1 - 1
views/inventory/inventory.js

@@ -152,7 +152,7 @@ let addIngredient = ()=>{
         quantity: content.children[2].children[0].value
     }
 
-    if(validator.ingredient.all(newIngredient)){
+    if(validator.ingredient.all(newIngredient.ingredient, newIngredient.quantity)){
         axios.post("/ingredients/createone", newIngredient)
             .then((ingredient)=>{
                 items.push({

+ 5 - 1
views/merchantSetupPage/merchantSetup.css

@@ -55,4 +55,8 @@ table{
         .input-new{
             display: flex;
             flex-wrap: nowrap;
-        }
+        }
+
+.input-error{
+    border-color: red;
+}

+ 1 - 0
views/merchantSetupPage/merchantSetup.ejs

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

+ 50 - 25
views/merchantSetupPage/merchantSetup.js

@@ -30,9 +30,9 @@ let updateState = (num)=>{
         newIngredients.style.display = "flex";
         createRecipes.style.display = "none";
     }else if(state === 2){
-        addIngredients.style.display = "none";
-        newIngredients.style.display = "none";
-        createRecipes.style.display = "flex";
+        // addIngredients.style.display = "none";
+        // newIngredients.style.display = "none";
+        // createRecipes.style.display = "flex";
         createIngredientsList();
     }
 }
@@ -89,24 +89,29 @@ let newIngredientField = ()=>{
     let name = document.createElement("td");
     let nameInput = document.createElement("input");
     nameInput.type = "text";
+    nameInput.onblur = ()=>{checkValid("name", nameInput)};
     name.appendChild(nameInput);
     row.appendChild(name);
 
     let category = document.createElement("td");
     let categoryInput = document.createElement("input");
     categoryInput.type = "text"
+    categoryInput.onblur = ()=>{checkValid("category", categoryInput)};
     category.appendChild(categoryInput);
     row.appendChild(category);
+
     let quantity = document.createElement("td");
     let quantityInput = document.createElement("input");
     quantityInput.type = "number";
     quantityInput.step = "0.01";
+    quantityInput.onblur = ()=>{checkValid("quantity", quantityInput)};
     quantity.appendChild(quantityInput);
     row.appendChild(quantity);
 
     let unit = document.createElement("td");
     let unitInput = document.createElement("input");
     unitInput.type = "text";
+    unitInput.onblur = ()=>{checkValid("unit", unitInput)};
     unit.appendChild(unitInput);
     row.appendChild(unit);
 
@@ -154,33 +159,44 @@ let createIngredientsList = ()=>{
         newIngredientQuantity.push({
             name: ingredient.children[0].children[0].value,
             quantity: ingredient.children[2].children[0].value
-        })
+        });
     }
 
-    axios.post("/ingredients/create", newIngredient)
-        .then((result)=>{
-            for(let ingredient of result.data){
-                let newIngredient = {
-                    id: ingredient._id,
-                    name: ingredient.name,
-                    unitType: ingredient.unitType
-                }
+    let isValid = true;
+    for(let i = 0; i < newIngredient.length; i++){
+        if(!validator.ingredient.all(newIngredient[i], newIngredientQuantity[i].quantity)){
+            isValid = false;
+            data.ingredients = [];
+            break;
+        }
+    }
 
-                for(let item of newIngredientQuantity){
-                    if(ingredient.name === item.name){
-                        newIngredient.quantity = item.quantity;
+    if(isValid){
+        axios.post("/ingredients/create", newIngredient)
+            .then((result)=>{
+                for(let ingredient of result.data){
+                    let newIngredient = {
+                        id: ingredient._id,
+                        name: ingredient.name,
+                        unitType: ingredient.unitType
                     }
-                }
 
-                data.ingredients.push(newIngredient);
-            }
-            banner.createNotification("All ingredients have been created and added to your inventory");
-            showRecipe();
-        })
-        .catch((err)=>{
-            banner.createError("There has been an error and your ingredients have not been saved");
-            console.log(err);
-        });
+                    for(let item of newIngredientQuantity){
+                        if(ingredient.name === item.name){
+                            newIngredient.quantity = item.quantity;
+                        }
+                    }
+
+                    data.ingredients.push(newIngredient);
+                }
+                banner.createNotification("All ingredients have been created and added to your inventory");
+                showRecipe();
+            })
+            .catch((err)=>{
+                banner.createError("There has been an error and your ingredients have not been saved");
+                console.log(err);
+            });
+    }
 }
 
 //Recipe functions
@@ -330,5 +346,14 @@ let submitAll = ()=>{
     form.submit();
 }
 
+let checkValid = (valueToCheck, inputField)=>{
+    console.log(inputField.value);
+    if(!validator.ingredient[valueToCheck](inputField.value, createBanner = false)){
+        inputField.classList += " input-error"
+    }else{
+        inputField.classList.remove("input-error");
+    }
+}
+
 populateIngredients();
 updateState(0);

+ 2 - 1
views/shared/banner.ejs

@@ -37,7 +37,8 @@
             }
 
             for(let notification of this.notificationList){
-                let li = document.createElement("li");
+                let li = document.createElement("li");                                                                       
+                                                                                                            
                 li.classList = "notification";
                 li.innerText = notification;
                 ul.appendChild(li);

+ 19 - 11
views/shared/validation.js

@@ -1,26 +1,32 @@
 let validator = {
     ingredient: {
-        name: function(ingName){
+        name: function(ingName, createBanner = true){
             if(ingName.length < 2){
-                banner.createError("Ingredient name must contain at least 2 characters");
+                if(createBanner){
+                    banner.createError("Ingredient name must contain at least 2 characters");
+                }
                 return false;
             }
 
             return true;
         },
 
-        category: function(ingCategory){
+        category: function(ingCategory, createBanner = true){
             if(ingCategory.length < 3){
-                banner.createError("Category name must contain at least 3 characters");
+                if(createBanner){
+                    banner.createError("Category name must contain at least 3 characters");
+                }
                 return false;
             }
 
             return true;
         },
 
-        quantity: function(num){
+        quantity: function(num, createBanner){
             if(num < 0){
-                banner.createError("Quantity cannot be a negative number");
+                if(createBanner){
+                    banner.createError("Quantity cannot be a negative number");
+                }
                 return false;
             }
 
@@ -31,11 +37,13 @@ let validator = {
             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);
+        //Check all parts of ingredient, return true if all pass
+        //Quantity passed seperately and optional
+        all: function(ingObject, quantity = 0, createBanner = true){
+            let nameCheck = this.name(ingObject.name, createBanner);
+            let categoryCheck = this.category(ingObject.category, createBanner);
+            let unitCheck = this.unit(ingObject.unitType, createBanner);
+            let quantityCheck = this.quantity(Number(quantity), createBanner);
 
             if(!nameCheck || !categoryCheck || !quantityCheck || !unitCheck){
                 return false;