Browse Source

Cannot click on ingredients until data loads. Fix small bug in date choosing.

Lee Morgan 6 years ago
parent
commit
491e6c1e24
2 changed files with 23 additions and 7 deletions
  1. 10 2
      views/dataPage/fetchData.js
  2. 13 5
      views/dataPage/home.js

+ 10 - 2
views/dataPage/fetchData.js

@@ -1,3 +1,5 @@
+window.dataLoaded = false;
+
 let date = new Date();
 let yearAgo = new Date(new Date().setFullYear(date.getFullYear() - 1));
 
@@ -5,14 +7,20 @@ let onDataLoad = function(){
     let dateSort = document.querySelector("#dateSort");
     dateSort.onclick = ()=>{window.homeObj.newDates()};
     dateSort.classList = "button";
+
+    let ingredientsBody = document.querySelector("#ingredientsData tbody");
+    for(let row of ingredientsBody.children){
+        row.classList = "clickableRow";
+        row.onclick = ()=>{window.ingredientObj.display("ingredient", row.ingredient)};
+    }
+
+    window.dataLoaded = true;
 }
 
 axios.post("/transactions", {from: yearAgo, to: date})
     .then((response)=>{
         if(typeof(response.data) === "string"){
             banner.createError(response.data);
-
-            
         }else{
             data.transactions = response.data;
             onDataLoad();

+ 13 - 5
views/dataPage/home.js

@@ -1,4 +1,5 @@
 window.homeObj = {
+    isPopulated: false,
     recipeTotal: 0,
     revenueTotal: 0,
     dateFrom: "",
@@ -14,7 +15,10 @@ window.homeObj = {
 
         document.querySelector("#to").valueAsDate = new Date();
 
-        this.populate(data.transactions);
+        if(!this.isPopulated){
+            this.populate(data.transactions);
+            this.isPopulated = true;
+        }
     },
 
     populate: function(transactions){
@@ -103,9 +107,13 @@ window.homeObj = {
         
         for(let ingredient of soldIngredients){
             let row = document.createElement("tr");
-            row.classList = "clickableRow";
-            row.onclick = ()=>{window.ingredientObj.display("ingredient", ingredient)};
             ingredientsBody.appendChild(row);
+            if(window.dataLoaded){
+                row.classList = "clickableRow";
+                row.onclick = ()=>{window.ingredientObj.display("ingredient", ingredient)};
+            }else{
+                row.ingredient = ingredient;
+            }
 
             let name = document.createElement("td");
             name.innerText = `${ingredient.name} (${ingredient.unit})`;
@@ -170,7 +178,7 @@ window.homeObj = {
     },
 
     newDates: function(){
-        let from = document.querySelector("#from").value;
+        let from = new Date(document.querySelector("#from").value);
         let to = new Date(document.querySelector("#to").value);
 
         if(from === "" || to === ""){
@@ -183,7 +191,7 @@ window.homeObj = {
 
         if(validator.transaction.date(from, to)){
             let startIndex = 0;
-            let endIndex = 0;
+            let endIndex = data.transactions.length;
 
             for(let i = 0; i < data.transactions.length; i++){
                 if(from < new Date(data.transactions[i].date)){