tor-eth-online-shop/www/index.js
www/index.jsRaw
1var products = [
2 {
3 id: 'id1',
4 title: 'beer1',
5 price: '0.1'
6 },
7 {
8 id: 'id2',
9 title: 'beer2',
10 price: '0.1'
11 },
12 {
13 id: 'id3',
14 title: 'beer3',
15 price: '0.1'
16 }
17];
18var list = {};
19function getProduct(id) {
20 for(var i=0; i<products.length; i++) {
21 if(products[i].id == id) {
22 return(products[i]);
23 }
24 }
25 return("not found");
26}
27function addProduct(id) {
28 if (!list[id]) {
29 list[id] = 1;
30 } else {
31 list[id]++;
32 }
33 // update html
34 var total = 0;
35 var html = "";
36 html += "<ul class='list-group'>";
37 for (var property in list) {
38 if (list.hasOwnProperty(property)) {
39 html += "<li class='list-group-item'>";
40 html += property + " x" + list[property];
41 html += "<div class='float-right'>";
42 html += (getProduct(property).price * list[property]).toFixed(4) + " eth";
43 html += "</div>";
44 html += "</li>";
45 total = (+(total) + +(getProduct(property).price * list[property])).toFixed(4);
46 }
47 }
48 html += "</ul>";
49 html += "<br>";
50 html += "<div class='float-right'>";
51 html += "<b>Total: " + total + "</b>";
52 html += "</div>";
53 html += "<br>";
54 document.getElementById("list").innerHTML = html;
55}
56
57function purchase() {
58 var total = 0;
59 for (var property in list) {
60 if (list.hasOwnProperty(property)) {
61 total = (+(total) + +(getProduct(property).price * list[property])).toFixed(4);
62 }
63 }
64 var answ = confirm("total to pay: " + total + " eth");
65 if (!answ) {
66 return;
67 }
68 axios.post('/api/purchase', {
69 list: list
70 })
71 .then(function (response) {
72 console.log(response);
73 })
74 .catch(function (error) {
75 console.log(error);
76 });
77}