Răsfoiți Sursa

Fix the displaying of transactions on the transactions strand.

Lee Morgan 5 ani în urmă
părinte
comite
3199f5fcdd

+ 3 - 6
views/dashboardPage/js/classes/Merchant.js

@@ -311,9 +311,7 @@ class Merchant{
 
     removeRecipe(recipe){
         const index = this._recipes.indexOf(recipe);
-        if(index === undefined){
-            return false;
-        }
+        if(index === undefined) return false;
 
         this._recipes.splice(index, 1);
 
@@ -324,11 +322,9 @@ class Merchant{
         return this._transactions;
     }
 
-    getTransactions(from = 0, to = new Date()){
+    getTransactions(from, to){
         if(merchant._transactions.length <= 0) return [];
 
-        if(from === 0) from = this._transactions[this._transactions.length-1].date;
-
         const {start, end} = this.getTransactionIndices(from, to);
 
         return this._transactions.slice(start, end);
@@ -584,6 +580,7 @@ class Merchant{
         let end = 0;
 
         if(
+            this._transactions.length === 0 ||
             from > this._transactions[0].date ||
             to >= this._transactions[this._transactions.length-1].date
         ){

+ 7 - 3
views/dashboardPage/js/dashboard.js

@@ -34,7 +34,7 @@ window.merchant = new Merchant(
 );
 
 controller = {
-    openStrand: function(strand, data = undefined){
+    openStrand: function(strand, data){
         this.closeSidebar();
 
         let strands = document.querySelectorAll(".strand");
@@ -78,7 +78,7 @@ controller = {
             case "transactions":
                 activeButton = document.getElementById("transactionsBtn");
                 document.getElementById("transactionsStrand").style.display = "flex";
-                transactions.transactions = data;
+                if(data !== undefined) transactions.transactions = data;
                 transactions.display();
                 break;
             case "account":
@@ -419,6 +419,7 @@ window.state = {
         home.isPopulated = false;
         ingredients.populateByProperty();
         analytics.isPopulated = false;
+        transactions.display();
         home.mostUsedRecipes();
         home.mostUsedIngredients();
     },
@@ -439,6 +440,9 @@ window.state = {
         recipeBook.isPopulated = false;
     }
 }
+let from = new Date();
+from.setDate(from.getDate() - 7);
+from.setHours(0, 0, 0, 0);
 
 //Add click listeners for menu buttons
 document.getElementById("menuShifter").onclick = ()=>{controller.changeMenu()}
@@ -448,7 +452,7 @@ document.getElementById("ingredientsBtn").onclick = ()=>{controller.openStrand("
 document.getElementById("recipeBookBtn").onclick = ()=>{controller.openStrand("recipeBook")};
 document.getElementById("analyticsBtn").onclick = ()=>{controller.openStrand("analytics")};
 document.getElementById("ordersBtn").onclick = ()=>{controller.openStrand("orders")};
-document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions", merchant.getTransactions())};
+document.getElementById("transactionsBtn").onclick = ()=>{controller.openStrand("transactions")};
 document.getElementById("accountBtn").onclick = ()=>{controller.openStrand("account")};
 document.getElementById("feedbackButton").onclick = ()=>{controller.openModal("feedback")};
 

+ 12 - 2
views/dashboardPage/js/sidebars/newTransaction.js

@@ -75,11 +75,21 @@ let newTransaction = {
                     if(typeof(response) === "string"){
                         controller.createBanner(response, "error");
                     }else{
-                        if(new Date(response.date) > merchant.transactions[merchant.transactions.length-1].date){
+                        let thirtyAgo = new Date();
+                        thirtyAgo.setDate(thirtyAgo.getDate() - 30);
+                        thirtyAgo.setHours(0, 0, 0);
+
+                        if(
+                            merchant.transactions.length === 0 ||
+                            new Date(response.date) > thirtyAgo
+                        ){
                             merchant.addTransactions([response], true);
                             state.updateTransactions();
                         }
-                        controller.openStrand("transactions", merchant.getTransactions());
+                        let from = new Date();
+                        from.setDate(from.getDate() - 7);
+                        from.setHours(0, 0, 0, 0);
+                        controller.openStrand("transactions", merchant.getTransactions(from, new Date()));
                         controller.createBanner("TRANSACTION CREATED", "success");
                     }
                 })

+ 5 - 1
views/dashboardPage/js/sidebars/transactionDetails.js

@@ -66,7 +66,11 @@ let transactionDetails = {
                     merchant.removeTransaction(this.transaction);
                     state.updateTransactions();
 
-                    controller.openStrand("transactions", merchant.getTransactions());
+                    let from = new Date();
+                    from.setDate(from.getDate() - 7);
+                    from.setHours(0, 0, 0, 0);
+
+                    controller.openStrand("transactions", merchant.getTransactions(from, new Date()));
                     controller.closeModal();
                     controller.createBanner("TRANSACTION REMOVED", "success");
                 }

+ 15 - 8
views/dashboardPage/js/strands/transactions.js

@@ -4,13 +4,20 @@ let transactions = {
     display: function(){
         document.getElementById("filterTransactionsButton").onclick = ()=>{controller.openSidebar("transactionFilter")};
         document.getElementById("newTransactionButton").onclick = ()=>{controller.openSidebar("newTransaction")};
+        if(this.transactions.length === 0){
+            let from = new Date();
+            from.setDate(from.getDate() - 7);
+            from.setHours(0, 0, 0, 0);
 
-        this.populateTransactions(this.transactions);
+            this.transactions = merchant.getTransactions(from, new Date());
+        }
+
+        this.populateTransactions();
 
         this.isPopulated = true;
     },
 
-    populateTransactions: function(transactions){
+    populateTransactions: function(){
         let transactionsList = document.getElementById("transactionsList");
         let template = document.getElementById("transaction").content.children[0];
 
@@ -19,9 +26,9 @@ let transactions = {
         }
 
         let i = 0;
-        while(i < transactions.length && i < 100){
+        while(i < this.transactions.length && i < 100){
             let transactionDiv = template.cloneNode(true);
-            let transaction = transactions[i];
+            let transaction = this.transactions[i];
 
             transactionDiv.onclick = ()=>{
                 controller.openSidebar("transactionDetails", transaction);
@@ -32,12 +39,12 @@ let transactions = {
             let totalRecipes = 0;
             let totalPrice = 0;
 
-            for(let j = 0; j < transactions[i].recipes.length; j++){
-                totalRecipes += transactions[i].recipes[j].quantity;
-                totalPrice += transactions[i].recipes[j].recipe.price * transactions[i].recipes[j].quantity;
+            for(let j = 0; j < transaction.recipes.length; j++){
+                totalRecipes += transaction.recipes[j].quantity;
+                totalPrice += transaction.recipes[j].recipe.price * transaction.recipes[j].quantity;
             }
 
-            transactionDiv.children[0].innerText = transactions[i].date.toLocaleDateString();
+            transactionDiv.children[0].innerText = transaction.date.toLocaleDateString();
             transactionDiv.children[1].innerText = `${totalRecipes} recipes sold`;
             transactionDiv.children[2].innerText = `$${totalPrice.toFixed(2)}`;