Lee Morgan 6 лет назад
Родитель
Сommit
a8a5456130

+ 0 - 2
controllers/recipeData.js

@@ -67,8 +67,6 @@ module.exports = {
             return res.redirect("/");
         }
 
-        console.log(req.body);
-
         Recipe.findOne({_id: req.body.id})
             .then((recipe)=>{
                 recipe.name = req.body.name;

+ 24 - 7
views/dashboardPage/Merchant.js

@@ -57,6 +57,29 @@ class Transaction{
     }
 }
 
+class Order{
+    constructor(name, date, ingredients, parent){        
+        this.name = name;
+        this.date = date;
+        this.ingredients = [];
+        this.parent = parent;
+
+        for(let i = 0; i < ingredients.length; i++){
+            for(let j = 0; j < parent.ingredients.length; j++){
+                if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
+                    this.ingredients.push({
+                        ingredient: parent.ingredients[j].ingredient,
+                        quantity: ingredients[i].quantity,
+                        price: ingredients[i].price
+                    });
+                }
+            }
+        }
+
+        this.parent.orders.push(this);
+    }
+}
+
 class Merchant{
     constructor(oldMerchant, transactions){
         this.name = oldMerchant.name;
@@ -64,6 +87,7 @@ class Merchant{
         this.ingredients = [];
         this.recipes = [];
         this.transactions = [];
+        this.orders = [];
         
         for(let i = 0; i < oldMerchant.inventory.length; i++){
             this.ingredients.push({
@@ -386,13 +410,6 @@ class Merchant{
     }
 }
 
-class Order{
-    constructor(date, ingredients){
-        this.date = date;
-        this.ingredients = ingredients;
-    }
-}
-
 let isSanitary =  (str, createBanner = true)=>{
     let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
 

+ 1 - 1
views/dashboardPage/components/components.js

@@ -172,7 +172,7 @@ let newOrderComp = {
         openSidebar(document.querySelector("#newOrder"));
 
         if(!this.isPopulated){
-            let categories = categorizeIngredients(merchant.inventory);
+            let categories = merchant.categorizeIngredients();
             let categoriesList = document.querySelector("#newOrderCategories");
             let template = document.querySelector("#addIngredientsCategory").content.children[0];
             let ingredientTemplate = document.querySelector("#addIngredientsIngredient").content.children[0];

+ 0 - 49
views/dashboardPage/controller.js

@@ -23,55 +23,6 @@ let changeStrand = (name)=>{
     window[`${name}Obj`].display();
 }
 
-/*
-Updates a recipe in the merchants list of recipes
-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 updateRecipes = (recipe, remove = false)=>{
-    let isNew = true;
-    let index = 0;
-
-    for(let i = 0; i < recipe.ingredients.length; i++){
-        for(let j = 0; j < merchant.inventory.length; j++){
-            if(merchant.inventory[j].ingredient._id === recipe.ingredients[i].ingredient){
-                recipe.ingredients[i].ingredient = merchant.inventory[j].ingredient;
-                break;
-            }
-        }
-    }
-
-    for(let i = 0; i < merchant.recipes.length; i++){
-        if(recipe._id === merchant.recipes[i]._id){
-            if(remove){
-                merchant.recipes.splice(i, 1);
-            }else{
-                merchant.recipes[i] = recipe;
-                index = i;
-            }
-
-            isNew = false;
-            break;
-        }
-    }
-
-    if(isNew){
-        merchant.recipes.push(recipe);
-        index = merchant.recipes.length - 1;
-    }
-
-    recipeBookStrandObj.populateRecipes();
-    closeSidebar();
-}
-
 /*
 Updates an order in the front end
 Can create, edit or remove

+ 18 - 9
views/dashboardPage/orders.js

@@ -16,6 +16,16 @@ window.ordersStrandObj = {
                     if(typeof(response) === "string"){
                         banner.createError(response);
                     }else{
+                        let newOrders = [];
+                        for(let i = 0; i < response.length; i++){
+                            newOrders.push(new Order(
+                                response[i].name,
+                                new Date(response.date),
+                                response.ingredients,
+                                merchant
+                            ));
+                        }
+
                         let listDiv = document.querySelector("#orderList");
                         let template = document.querySelector("#order").content.children[0];
 
@@ -23,22 +33,21 @@ window.ordersStrandObj = {
                             listDiv.removeChild(listDiv.firstChild);
                         }
 
-                        for(let i = 0; i < response.length; i++){
+                        for(let i = 0; i < merchant.orders.length; i++){
                             let row = template.cloneNode(true);
                             let totalCost = 0;
                             
-                            for(let j = 0; j < response[i].ingredients.length; j++){
+                            for(let j = 0; j < merchant.orders[i].ingredients.length; j++){
                                 
-                                totalCost += response[i].ingredients[j].quantity * response[i].ingredients[j].price;
+                                totalCost += merchant.orders[i].ingredients[j].quantity * merchant.orders[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[0].innerText = merchant.orders[i].name;
+                            row.children[1].innerText = `${merchant.orders[i].ingredients.length} items`;
+                            row.children[2].innerText = new Date(merchant.orders[i].date).toLocaleDateString("en-US");
                             row.children[3].innerText = (totalCost / 100).toFixed(2);
-                            row._date = row.children[2].innerText;
-                            row._id = response[i]._id;
-                            row.onclick = ()=>{orderDetailsComp.display(response[i])};
+                            row.order = merchant.orders[i];
+                            row.onclick = ()=>{orderDetailsComp.display(merchant.orders[i])};
 
                             window.orders.push(row);
                             listDiv.appendChild(row);