aboutsummaryrefslogtreecommitdiff
path: root/extension/pages/confirm-contract.tsx
blob: 811e2e001193bd22a690146adcf7f63c8c66f4ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/// <reference path="../decl/handlebars/handlebars.d.ts" />
"use strict";

let url = URI(document.location.href);
let query: any = URI.parseQuery(url.query());

let $_ = (x) => document.getElementById(x);

function renderContract(contract) {
  let showAmount = document.getElementById("show-amount");
  $_('merchant-name').innerText = contract.merchant.name;
}

function clone(obj) {
  // This is faster than it looks ...
  return JSON.parse(JSON.stringify(obj));
}


Handlebars.registerHelper('prettyAmount', function(amount) {
  let v = amount.value + amount.fraction / 10e6;
  return v.toFixed(2) + " " + amount.currency;
});


document.addEventListener("DOMContentLoaded", (e) => {
  let offer = JSON.parse(query.offer);
  console.dir(offer);

  let source = $_("contract-template").innerHTML;
  let template = Handlebars.compile(source);
  let html = template(offer.contract);

  $_("render-contract").innerHTML = html;

  document.getElementById("confirm-pay").addEventListener("click", (e) => {
    let d = clone(query);
    chrome.runtime.sendMessage({type:'confirm-pay', detail: d}, (resp) => {
      if (resp.success === true) {
        document.location.href = resp.backlink;
      } else {
        document.body.innerHTML =
          `Oops, something went wrong.
           Here is some more info:
           <pre>${resp.text}</pre>`;
      }
    });
  });
});