merchant-frontend-examples

ZZZ: Inactive/Deprecated
Log | Files | Refs

history.js (2205B)


      1 var FRACTION = 100000000;
      2 
      3 // Stringify amounts. Take a {value: x, fraction: y, currency: "Z"}
      4 // and return a "a.b Z" form.
      5 function parse_amount(amount){
      6   var v = amount.value + (amount.fraction/FRACTION);
      7   return v + " " + amount.currency;
      8 }
      9 
     10 // Parse Taler date ("/Date(TIMESTAMP)/") string and
     11 // return a JavaScript Date object.
     12 function get_date(date){
     13   var split = date.match(/Date\((.*)\)/);
     14   var seconds;
     15   if(isNaN(seconds = Number(split[1]))){
     16     console.error("Malformed date gotten from backend");
     17     return;
     18   }
     19   console.log("Extracting timestamp", split[1]);
     20   var d = new Date(seconds * 1000);
     21   return d;
     22 }
     23 
     24 
     25 // Perform the call to /history.php?instance=tutorial.
     26 // It also takes care of cleaning/filling the table showing
     27 // the results.
     28 function submit_history(){
     29 
     30   // Clean the table showing old results
     31   var table = document.getElementById("history");
     32   /* We don't want to kill the first child */
     33   for (var i = 2; i < table.childNodes.length; i++)
     34     table.removeChild(table.childNodes[i]);
     35   var req = new XMLHttpRequest();
     36   var get_column = function(value){
     37     var column = document.createElement("td");
     38     column.textContent = value;
     39     return column;
     40   };
     41   var on_error = function(){
     42     table.innerHTML = "<p>Could not get transactions list from server</p>"
     43   };
     44   req.open("GET", "/history.php?instance=tutorial", true);
     45   req.onload = function(){
     46     if(req.readyState == 4 && req.status == 200){
     47       console.log("Got history:", req.responseText);
     48       var history = JSON.parse(req.responseText); 
     49       if(!history)
     50         console.log("Got invalid JSON");
     51       if(0 == history.length){
     52         table.innerHTML = "<p>No transaction was that young!</p>"; 
     53       }
     54       // Fill the table with fresh results
     55       for (var i=0; i<history.length; i++){
     56 	var entry = history[i];
     57         var tr = document.createElement("tr");
     58         tr.appendChild(get_column(entry.order_id));
     59 	var date = get_date(entry.timestamp);
     60 	tr.appendChild(get_column(date.toLocaleDateString()));
     61 	tr.appendChild(get_column(parse_amount(entry.amount)))
     62 	table.appendChild(tr);
     63       }
     64       table.style.visibility = "";
     65     }
     66     else{
     67       on_error();
     68     }
     69   };
     70   req.send();
     71 }