浏览代码

Finish frontend for orders

Lee Morgan 6 年之前
父节点
当前提交
261f5fcd34

+ 0 - 1
controllers/orderData.js

@@ -13,7 +13,6 @@ module.exports = {
             {$sort: {date: -1}},
             {$limit: 25},
             {$project: {
-                _id: 0,
                 orderId: 1,
                 date: 1,
                 ingredients: 1

+ 27 - 24
views/dashboardPage/components/components.js

@@ -202,8 +202,8 @@ let newOrderComp = {
         let categoriesList = document.querySelector("#newOrderCategories");
 
         let newOrder = {
-            orderId: "none",
-            date: new Date(),
+            orderId: document.querySelector("#orderName").value,
+            date: new Date(document.querySelector("#orderDate").value),
             ingredients: []
         }
 
@@ -211,38 +211,41 @@ let newOrderComp = {
             for(let j = 0; j < categoriesList.children[i].children[1].children.length; j++){
                 let ingredientDiv = categoriesList.children[i].children[1].children[j];
                 let quantity = ingredientDiv.children[1].value;
+                let price = ingredientDiv.children[2].value;
 
-                if(quantity !== ""){
+                if(quantity !== ""  || price !== ""){
                     let newIngredient = {
                         ingredient: ingredientDiv._id,
                         quantity: quantity,
-                        price: ingredientDiv.children[2].value
+                        price: parseInt(price * 100)
                     }
 
                     newOrder.ingredients.push(newIngredient);
                 }
             }
         }
-
-        fetch("/order", {
-            method: "POST",
-            headers: {
-                "Content-Type": "application/json;charset=utf-8"
-            },
-            body: JSON.stringify(newOrder)
-        })
-            .then(response => response.json())
-            .then((response)=>{
-                if(typeof(response) === "string"){
-                    banner.createError(response);
-                }else{
-                    banner.createNotification("New order created");
-                    //update orders list
-                }
+        
+        if(validator.order(newOrder)){
+            fetch("/order", {
+                method: "POST",
+                headers: {
+                    "Content-Type": "application/json;charset=utf-8"
+                },
+                body: JSON.stringify(newOrder)
             })
-            .catch((err)=>{
-                console.log(err);
-                banner.createError("Something went wrong.  Try refreshing the page");
-            });
+                .then(response => response.json())
+                .then((response)=>{
+                    if(typeof(response) === "string"){
+                        banner.createError(response);
+                    }else{
+                        banner.createNotification("New order created");
+                        updateOrders(newOrder);
+                    }
+                })
+                .catch((err)=>{
+                    console.log(err);
+                    banner.createError("Something went wrong.  Try refreshing the page");
+                });
+        }
     }
 }

+ 6 - 0
views/dashboardPage/components/newOrder.ejs

@@ -10,6 +10,12 @@
 
     <h1>New Order</h1>
 
+    <label>ID(optional):
+        <input id="orderName" type="text"> 
+    </label>
+
+    <input id="orderDate" type="date">
+
     <div id=newOrderCategories></div>
 
     <button class="button" onclick="newOrderComp.submit()">Create</button>

+ 36 - 0
views/dashboardPage/controller.js

@@ -111,6 +111,42 @@ let updateRecipes = (recipe, remove = false)=>{
     closeSidebar();
 }
 
+/*
+Updates an order in the front end
+Can create, edit or remove
+Inputs:
+    recipe: object
+        _id: id of recipe
+        name: name of recipe
+        price: price of recipe
+        ingredients: list of ingredients
+            ingredient: id of ingredient
+            quantity: quantity of ingredient
+    remove: if true, remove ingredient from inventory
+*/
+let updateOrders = (order, remove = false)=>{
+    let isNew = true;
+
+    for(let i = 0; i < orders.length; i++){
+        if(orders[i]._id === order._id){
+            if(remove){
+                orders.splice(i, 1);
+            }else{
+                orders[i] = order;
+            }
+
+            isNew = false;
+        }
+    }
+
+    if(isNew){
+        orders.push(order);
+    }
+
+    ordersStrandObj.isPopulated = false;
+    ordersStrandObj.display();
+}
+
 //Close any open sidebar
 let closeSidebar = ()=>{
     let sidebar = document.querySelector("#sidebarDiv");

+ 8 - 3
views/dashboardPage/orders.js

@@ -1,8 +1,10 @@
 window.ordersStrandObj = {
     isPopulated: false,
 
-    display: function(){
+    display: async function(){
         if(!this.isPopulated){
+            window.orders = [];
+
             fetch("/orders", {
                 method: "GET",
                 headers: {
@@ -21,15 +23,18 @@ window.ordersStrandObj = {
                             let row = template.cloneNode(true);
                             let totalCost = 0;
                             
-                            for(let j = 0; j < response[i].ingredients; j++){
+                            for(let j = 0; j < response[i].ingredients.length; j++){
                                 totalCost += response[i].ingredients[j].quantity * response[i].ingredients[j].price;
                             }
 
                             row.children[0].innerText = response[i].orderId;
                             row.children[1].innerText = `${response[i].ingredients.length} items`;
                             row.children[2].innerText = new Date(response[i].date).toLocaleDateString("en-US");
-                            row.children[3].innerText = totalCost;
+                            row.children[3].innerText = (totalCost / 100).toFixed(2);
+                            row._date = row.children[2].innerText;
+                            row._id = response[i]._id;
 
+                            window.orders.push(row);
                             listDiv.appendChild(row);
                         }
                     }

+ 45 - 2
views/shared/validation.js

@@ -183,12 +183,55 @@ let validator = {
         return true;
     },
 
-    isSanitary: function(str){
+    order: function(order, createBanner = true){
+        let errors = [];
+
+        if(!validator.isSanitary(order.orderId, false)){
+            errors.push("Your string contains illegal characters");
+        }
+
+        let now = new Date()
+        if(order.date > now){
+            errors.push("Cannot have a date/time in the future");
+        }
+
+        for(let i = 0; i < order.ingredients.length; i++){
+            if(order.ingredients[i].quantity < 0){
+                errors.push("Quantity cannot be negative");
+                break;
+            }
+
+            if(order.ingredients[i].price < 0){
+                errors.push("Price cannot be negative");
+                break;
+            }
+
+            if(order.ingredients[i].price === "" || order.ingredients[i].quantity === ""){
+                errors.push("Incomplete information");
+            }
+        }
+
+        if(errors.length > 0){
+            if(createBanner){
+                for(let error of errors){
+                    banner.createError(error);
+                }
+            }
+
+            return false;
+        }
+
+        return true;
+    },
+
+    isSanitary: function(str, createBanner = true){
         let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
 
         for(let char of disallowed){
             if(str.includes(char)){
-                banner.createError("Your string contains illegal characters");
+                if(createBanner){
+                    banner.createError("Your string contains illegal characters");
+                }
                 return false;
             }
         }