You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.6 KiB

5 years ago
  1. var 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. ];
  18. var list = {};
  19. function 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. }
  27. function 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. function purchase() {
  57. var total = 0;
  58. for (var property in list) {
  59. if (list.hasOwnProperty(property)) {
  60. total = (+(total) + +(getProduct(property).price * list[property])).toFixed(4);
  61. }
  62. }
  63. var answ = confirm("total to pay: " + total + " eth");
  64. if (!answ) {
  65. return;
  66. }
  67. axios.post('/api/purchase', {
  68. list: list
  69. })
  70. .then(function (response) {
  71. console.log(response);
  72. })
  73. .catch(function (error) {
  74. console.log(error);
  75. });
  76. }