Przeglądaj źródła

Fix transaction fetching not updating the page correctly

Lee Morgan 5 lat temu
rodzic
commit
f7b3e5ce01

+ 27 - 18
views/dashboardPage/bundle.js

@@ -539,14 +539,15 @@ class Transaction{
 
 module.exports = Transaction;
 },{}],6:[function(require,module,exports){
-const Transaction = require("./Transaction");
-
 let analytics = {
+    dateChange: false,
     transactions: [],
     ingredient: {},
     recipe: {},
 
-    display: function(){
+    display: function(Transaction){
+        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates(Transaction)};
+
         if(this.transactions.length === 0){
             let startDate = new Date();
             startDate.setMonth(startDate.getMonth() - 1);
@@ -556,7 +557,7 @@ let analytics = {
         }
 
         let slider = document.getElementById("analSlider");
-        slider.onchange = ()=>{this.display()};
+        slider.onchange = ()=>{this.display(Transaction)};
 
         let ingredientContent = document.getElementById("analIngredientContent");
         let recipeContent = document.getElementById("analRecipeContent");
@@ -575,13 +576,6 @@ let analytics = {
     displayIngredients: function(){
         const itemsList = document.getElementById("itemsList");
 
-        let now = new Date();
-        let lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), now.getHours(), now.getMinutes());
-
-        document.getElementById("analStartDate").valueAsDate = lastMonth;
-        document.getElementById("analEndDate").valueAsDate = now;
-        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates()};
-
         while(itemsList.children.length > 0){
             itemsList.removeChild(itemsList.firstChild);
         }
@@ -604,6 +598,11 @@ let analytics = {
             };
             itemsList.appendChild(li);
         }
+
+        if(this.dateChange && Object.keys(this.ingredient).length !== 0){
+            this.ingredientDisplay();
+        }
+        this.dateChange = false;
     },
 
     displayRecipes: function(){
@@ -630,6 +629,11 @@ let analytics = {
 
             recipeList.appendChild(li);
         }
+
+        if(this.dateChange  && Object.keys(this.recipe).length !== 0){
+            this.recipeDisplay();
+        }
+        this.dateChange = false;
     },
 
     ingredientDisplay: function(){
@@ -783,12 +787,10 @@ let analytics = {
         }
 
         document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
-        console.log(sum / quantities.length);
-        console.log(this.recipe.price);
         document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
     },
 
-    changeDates: function(){
+    changeDates: function(Transaction){
         let dates = {
             from: document.getElementById("analStartDate").valueAsDate,
             to: document.getElementById("analEndDate").valueAsDate
@@ -814,7 +816,7 @@ let analytics = {
                 if(typeof(response) === "string"){
                     banner.createError(response.data);
                 }else{
-                    this.transactions = [];                    
+                    this.transactions = [];
 
                     for(let i = 0; i < response.length; i++){
                         this.transactions.push(new Transaction(
@@ -824,8 +826,15 @@ let analytics = {
                             merchant
                         ));
                     }
+
+                    let isRecipe = document.getElementById("analSlider").checked;
+                    if(isRecipe && Object.keys(this.recipe).length !== 0){
+                        this.recipeDisplay();
+                    }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
+                        this.ingredientDisplay();
+                    }
                     
-                    this.ingredientDisplay();
+                    this.dateChange = true;
                 }
             })
             .catch((err)=>{
@@ -838,7 +847,7 @@ let analytics = {
 }
 
 module.exports = analytics;
-},{"./Transaction":5}],7:[function(require,module,exports){
+},{}],7:[function(require,module,exports){
 const home = require("./home.js");
 const ingredients = require("./ingredients.js");
 const recipeBook = require("./recipeBook.js");
@@ -898,7 +907,7 @@ controller = {
             case "analytics":
                 activeButton = document.getElementById("analyticsBtn");
                 document.getElementById("analyticsStrand").style.display = "flex";
-                analytics.display();
+                analytics.display(Transaction);
                 break;
             case "orders":
                 activeButton = document.getElementById("ordersBtn");

+ 25 - 16
views/dashboardPage/js/analytics.js

@@ -1,11 +1,12 @@
-const Transaction = require("./Transaction");
-
 let analytics = {
+    dateChange: false,
     transactions: [],
     ingredient: {},
     recipe: {},
 
-    display: function(){
+    display: function(Transaction){
+        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates(Transaction)};
+
         if(this.transactions.length === 0){
             let startDate = new Date();
             startDate.setMonth(startDate.getMonth() - 1);
@@ -15,7 +16,7 @@ let analytics = {
         }
 
         let slider = document.getElementById("analSlider");
-        slider.onchange = ()=>{this.display()};
+        slider.onchange = ()=>{this.display(Transaction)};
 
         let ingredientContent = document.getElementById("analIngredientContent");
         let recipeContent = document.getElementById("analRecipeContent");
@@ -34,13 +35,6 @@ let analytics = {
     displayIngredients: function(){
         const itemsList = document.getElementById("itemsList");
 
-        let now = new Date();
-        let lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), now.getHours(), now.getMinutes());
-
-        document.getElementById("analStartDate").valueAsDate = lastMonth;
-        document.getElementById("analEndDate").valueAsDate = now;
-        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates()};
-
         while(itemsList.children.length > 0){
             itemsList.removeChild(itemsList.firstChild);
         }
@@ -63,6 +57,11 @@ let analytics = {
             };
             itemsList.appendChild(li);
         }
+
+        if(this.dateChange && Object.keys(this.ingredient).length !== 0){
+            this.ingredientDisplay();
+        }
+        this.dateChange = false;
     },
 
     displayRecipes: function(){
@@ -89,6 +88,11 @@ let analytics = {
 
             recipeList.appendChild(li);
         }
+
+        if(this.dateChange  && Object.keys(this.recipe).length !== 0){
+            this.recipeDisplay();
+        }
+        this.dateChange = false;
     },
 
     ingredientDisplay: function(){
@@ -242,12 +246,10 @@ let analytics = {
         }
 
         document.getElementById("recipeAvgUse").innerText = (sum / quantities.length).toFixed(2);
-        console.log(sum / quantities.length);
-        console.log(this.recipe.price);
         document.getElementById("recipeAvgRevenue").innerText = `$${(((sum / quantities.length) * this.recipe.price) / 100).toFixed(2)}`;
     },
 
-    changeDates: function(){
+    changeDates: function(Transaction){
         let dates = {
             from: document.getElementById("analStartDate").valueAsDate,
             to: document.getElementById("analEndDate").valueAsDate
@@ -273,7 +275,7 @@ let analytics = {
                 if(typeof(response) === "string"){
                     banner.createError(response.data);
                 }else{
-                    this.transactions = [];                    
+                    this.transactions = [];
 
                     for(let i = 0; i < response.length; i++){
                         this.transactions.push(new Transaction(
@@ -283,8 +285,15 @@ let analytics = {
                             merchant
                         ));
                     }
+
+                    let isRecipe = document.getElementById("analSlider").checked;
+                    if(isRecipe && Object.keys(this.recipe).length !== 0){
+                        this.recipeDisplay();
+                    }else if(!isRecipe && Object.keys(this.ingredient).length !== 0){
+                        this.ingredientDisplay();
+                    }
                     
-                    this.ingredientDisplay();
+                    this.dateChange = true;
                 }
             })
             .catch((err)=>{

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

@@ -57,7 +57,7 @@ controller = {
             case "analytics":
                 activeButton = document.getElementById("analyticsBtn");
                 document.getElementById("analyticsStrand").style.display = "flex";
-                analytics.display();
+                analytics.display(Transaction);
                 break;
             case "orders":
                 activeButton = document.getElementById("ordersBtn");