Selaa lähdekoodia

Frontend now updates ingredients when after the creation of a new ingredient

Lee Morgan 5 vuotta sitten
vanhempi
sitoutus
c3857acef6

+ 0 - 1
controllers/transactionData.js

@@ -88,7 +88,6 @@ module.exports = {
                 return res.json(response);
             })
             .catch((err)=>{
-                console.log(err);
                 return res.json("ERROR: UNABLE TO CREATE NEW TRANSACTION");
             });
     },

Tiedoston diff-näkymää rajattu, sillä se on liian suuri
+ 62 - 0
views/dashboardPage/bundle.js


+ 25 - 1
views/dashboardPage/js/Merchant.js

@@ -1,6 +1,7 @@
 const Ingredient = require("./Ingredient.js");
 const Recipe = require("./Recipe.js");
 const Transaction = require("./Transaction.js");
+const merchant = require("../../../models/merchant.js");
 
 class Merchant{
     constructor(oldMerchant, transactions){
@@ -156,7 +157,14 @@ class Merchant{
         controller.closeSidebar();
     }
 
-    editTransactions(transaction, remove = false){
+    /*
+    transaction = Transaction Object to add
+    ingredients = The ingredients that need to be updated
+        keys = ingredient ids
+        values = quantity to change in grams
+    remove = If true, removes transaction
+    */
+    editTransactions(transaction, ingredients, remove = false, ){
         let isNew = true;
         for(let i = 0; i < this.transactions.length; i++){
             if(this.transactions[i] === transaction){
@@ -174,6 +182,22 @@ class Merchant{
             this.transactions.sort((a, b) => a.date > b.date ? 1 : -1);
         }
 
+        let keys = Object.keys(ingredients);
+        for(let i = 0; i < keys.length; i++){
+            for(let j = 0; j < this.ingredients.length; j++){
+                if(this.ingredients[j].ingredient.id === keys[i]){
+                    if(remove === false){
+                        this.ingredients[j].quantity -= ingredients[keys[i]];
+                    }else{
+                        this.ingredients[j].quantity += ingredients[keys[i]];
+                    }
+
+                    break;
+                }
+            }
+        }
+
+        controller.updateData("ingredient");
         controller.updateData("transaction");
         controller.closeSidebar();
     }

+ 2 - 3
views/dashboardPage/js/newTransaction.js

@@ -79,12 +79,11 @@ let newTransaction = {
                             response.recipes,
                             merchant
                         );
-                        merchant.editTransactions(transaction);
-                        banner.createNotification("NEW TRANSACTION CREATED");
+                        merchant.editTransactions(transaction, data.ingredientUpdates);
+                        banner.createNotification("NEW TRANSACTION CREATED, INGREDIENTS UPDATED ACCORDINGLY");
                     }
                 })
                 .catch((err)=>{
-                    console.log(err);
                     banner.createError("SOMETHING WENT WRONG. PLEASE REFRESH THE PAGE");
                 })
                 .finally(()=>{

Kaikkia tiedostoja ei voida näyttää, sillä liian monta tiedostoa muuttui tässä diffissä