Selaa lähdekoodia

Fixed ability to add new ingredients

Lee Morgan 6 vuotta sitten
vanhempi
sitoutus
f07841d8f1

+ 1 - 3
app.js

@@ -30,6 +30,4 @@ app.use(express.json());
 
 require("./routes")(app);
 
-app.listen(process.env.PORT, ()=>{
-    console.log(`Running on port ${process.env.PORT}`);
-});
+app.listen(process.env.PORT, ()=>{});

+ 8 - 15
views/dashboardPage/Merchant.js

@@ -102,7 +102,7 @@ class Merchant{
     Inputs:
     Array of objects
         id: id of ingredient
-        quantityChange: change in quantity (if not removing)
+        quantity: change in quantity (if not removing)
         name: name of ingredient (only for new ingredient)
         category: category of ingredient (only for new ingredient)
         unit: unit of measurement (only for new ingredient)
@@ -111,13 +111,12 @@ class Merchant{
     addIngredients(ingredients, remove = false){
         for(let i = 0; i < ingredients.length; i++){
             let isNew = true;
-            for(let j = 0; j < merchant.inventory.length; j++){
-                if(merchant.inventory[j].ingredient._id === ingredients[i].id){
+            for(let j = 0; j < merchant.ingredients.length; j++){
+                if(merchant.ingredients[j].ingredient === ingredients[i].ingredient){
                     if(remove){
-                        merchant.inventory.splice(j, 1);
+                        merchant.ingredients.splice(j, 1);
                     }else{
-    
-                        merchant.inventory[j].quantity += ingredients[i].quantity;
+                        merchant.ingredients[j].quantity += ingredients[i].quantity;
                     }
     
                     isNew = false;
@@ -126,15 +125,9 @@ class Merchant{
             }
     
             if(isNew){
-                merchant.inventory.push({
-    
-                    ingredient: {
-                        _id: ingredients[i].id,
-                        category: ingredients[i].category,
-                        name: ingredients[i].name,
-                        unit: ingredients[i].unit
-                    },
-                    quantity: parseFloat(ingredients[i].quantityChange)
+                merchant.ingredients.push({
+                    ingredient: ingredients[i].ingredient,
+                    quantity: parseFloat(ingredients[i].quantity)
                 });
             }
         }

+ 14 - 21
views/dashboardPage/components/components.js

@@ -398,7 +398,6 @@ let addIngredientsComp = {
                     }
                 })
                 .catch((err)=>{
-                    console.log(err);
                     banner.createError("Unable to retrieve data");
                 });
 
@@ -413,9 +412,7 @@ let addIngredientsComp = {
         let categoryTemplate = document.getElementById("addIngredientsCategory");
         let ingredientTemplate = document.getElementById("addIngredientsIngredient");
 
-        console.log(this.fakeMerchant);
         let categories = this.fakeMerchant.categorizeIngredients();
-        console.log(categories);
 
         while(addIngredientsDiv.children.length > 0){
             addIngredientsDiv.removeChild(addIngredientsDiv.firstChild);
@@ -506,7 +503,8 @@ let addIngredientsComp = {
 
     submit: function(){
         let ingredients = document.getElementById("myIngredients").children;
-        let added = [];
+        let newIngredients = [];
+        let fetchable = [];
 
         for(let i = 0; i < ingredients.length; i++){
             if(ingredients[i].children[1].value === ""){
@@ -514,20 +512,15 @@ let addIngredientsComp = {
                 return;
             }
 
-            let ingredient = {
-                id: ingredients[i]._id,
-                quantity: ingredients[i].children[1].value,
-                quantityChange: ingredients[i].children[1].value,
-                name: ingredients[i]._name,
-                category: ingredients[i]._category,
-                unit: ingredients[i]._unit
-            }
+            newIngredients.push({
+                ingredient: ingredients[i].ingredient,
+                quantity: ingredients[i].children[1].value
+            });
 
-            if(validator.ingredientQuantity(ingredient.quantity)){
-                added.push(ingredient);
-            }else{
-                return;
-            }
+            fetchable.push({
+                id: ingredients[i].ingredient.id,
+                quantity: ingredients[i].children[1].value
+            });
         }
 
         fetch("/merchant/ingredients/add", {
@@ -535,17 +528,18 @@ let addIngredientsComp = {
             headers: {
                 "Content-Type": "application/json;charset=utf-8"
             },
-            body: JSON.stringify(added)
+            body: JSON.stringify(fetchable)
         })
             .then((response)=>{
                 if(typeof(response) === "string"){
                     banner.createError(response);
                 }else{
-                    updateInventory(added);
+                    merchant.addIngredients(newIngredients);
                     banner.createNotification("All ingredients added successfully");
                 }
             })
             .catch((err)=>{
+                console.log(err);
                 banner.createError("Something went wrong.  Try refreshing the page");
             });
     }
@@ -588,9 +582,7 @@ let ingredientDetailsComp = {
         document.querySelector("#dailyUse").innerText = `${(sum/quantities.length).toFixed(2)} ${ingredient.ingredient.unit}`;
 
         let ul = document.querySelector("#ingredientRecipeList");
-        console.log(ingredient);
         let recipes = merchant.getRecipesForIngredient(ingredient.ingredient);
-        console.log(recipes);
         while(ul.children.length > 0){
             ul.removeChild(ul.firstChild);
         }
@@ -662,6 +654,7 @@ let ingredientDetailsComp = {
                     }
                 })
                 .catch((err)=>{
+                    console.log(err);
                     banner.createError("Something went wrong, try refreshing the page");
                 });
         }

+ 0 - 117
views/dashboardPage/controller.js

@@ -23,55 +23,6 @@ let changeStrand = (name)=>{
     window[`${name}Obj`].display();
 }
 
-/*
-Updates all specified item in the merchant's inventory and updates the page
-If ingredient doesn't exist, add it
-Inputs:
- Array of objects
-     id: id of ingredient
-     quantityChange: change in quantity (if not removing)
-     name: name of ingredient (only for new ingredient)
-     category: category of ingredient (only for new ingredient)
-     unit: unit of measurement (only for new ingredient)
- remove: if true, remove ingredient from inventory
- */
-let updateInventory = (ingredients, remove = false)=>{
-    for(let i = 0; i < ingredients.length; i++){
-        let isNew = true;
-        for(let j = 0; j < merchant.inventory.length; j++){
-            if(merchant.inventory[j].ingredient._id === ingredients[i].id){
-                if(remove){
-                    merchant.inventory.splice(j, 1);
-                }else{
-
-                    merchant.inventory[j].quantity += ingredients[i].quantity;
-                }
-
-                isNew = false;
-                break;
-            }
-        }
-
-        if(isNew){
-            merchant.inventory.push({
-
-                ingredient: {
-                    _id: ingredients[i].id,
-                    category: ingredients[i].category,
-                    name: ingredients[i].name,
-                    unit: ingredients[i].unit
-                },
-                quantity: parseFloat(ingredients[i].quantityChange)
-            });
-        }
-    }
-
-    homeStrandObj.drawInventoryCheckCard();
-    ingredientsStrandObj.populateByProperty("category");
-    addIngredientsComp.isPopulated = false;
-    closeSidebar();
-}
-
 /*
 Updates a recipe in the merchants list of recipes
 Can create, edit or remove
@@ -183,41 +134,6 @@ let openSidebar = (sidebar)=>{
     sidebar.style.display = "flex";
 }
 
-/*
-Gets the quantity of a single ingredient sold between two dates (dateRange)
-Input:
-    dateRange: array containing two elements, start and end indices
-    id: id of the ingredient to calculate
-Return: (int) Quantity of recipes sold
-*/
-let ingredientSold = (dateRange,  id)=>{
-    let recipes = recipesSold(dateRange);
-    let total = 0;
-
-    let checkRecipes = [];
-    let quantities = [];
-    for(let i = 0; i < merchant.recipes.length; i++){
-        for(let j = 0; j < merchant.recipes[i].ingredients.length; j++){
-            if(merchant.recipes[i].ingredients[j].ingredient._id === id){
-                checkRecipes.push(merchant.recipes[i]._id);
-                quantities.push(merchant.recipes[i].ingredients[j].quantity);
-                break;
-            }
-        }
-    }
-
-    for(let i = 0; i < recipes.length; i++){
-        for(let i = 0; i < checkRecipes.length; i++){
-            if(checkRecipes[i] === recipes[i].id){
-                total += recipes[i].quantity * quantities[i];
-                break;
-            }
-        }
-    }
-
-    return total;
-}
-
 let unitizeIngredients = (ingredients)=>{
     let ingredientsByUnit = [];
 
@@ -253,37 +169,4 @@ let unitizeIngredients = (ingredients)=>{
     return ingredientsByUnit;
 }
 
-let categorizeIngredientsFromDB = (ingredients)=>{
-    let ingredientsByCategory = [];
-
-    for(let i = 0; i < ingredients.length; i++){
-        let categoryExists = false;
-        for(let j = 0; j < ingredientsByCategory.length; j++){
-            if(ingredients[i].category === ingredientsByCategory[j].name){
-                ingredientsByCategory[j].ingredients.push({
-                    id: ingredients[i]._id,
-                    name: ingredients[i].name,
-                    unit: ingredients[i].unit
-                });
-
-                categoryExists = true;
-                break;
-            }
-        }
-
-        if(!categoryExists){
-            ingredientsByCategory.push({
-                name: ingredients[i].category,
-                ingredients: [{
-                    id: ingredients[i]._id,
-                    name: ingredients[i].name,
-                    unit: ingredients[i].unit
-                }]
-            });
-        }
-    }
-
-    return ingredientsByCategory;
-}
-
 homeStrandObj.display();

+ 4 - 0
views/dashboardPage/ingredients.js

@@ -11,8 +11,10 @@ window.ingredientsStrandObj = {
     },
 
     populateByProperty: function(property){
+        console.log("populate by property");
         let categories;
         if(property === "category"){
+            console.log("categorizing");
             categories = merchant.categorizeIngredients();
         }else if(property === "unit"){
             categories = unitizeIngredients(merchant.inventory);
@@ -48,6 +50,8 @@ window.ingredientsStrandObj = {
                 this.ingredients.push(ingredientDiv);
             }
         }
+
+        console.log("done");
     },
 
     displayIngredientsOnly: function(ingredients){