Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const user = {
  2.     appId: "kid_BJXTsSi-e",
  3.     appSecret: "447b8e7046f048039d95610c1b039390",
  4.     username: "guest",
  5.     password: "pass",
  6.  
  7. };
  8. const POST_URL = `https://baas.kinvey.com/rpc/kid_BJ_Ke8hZg/custom/calendar?query=`;
  9. const GET_URL = `https://baas.kinvey.com/appdata/kid_BJ_Ke8hZg/venues/`;
  10.  
  11. const AUTH = {
  12.     "Authorization": 'Basic ' + btoa(user.username + ':' + user.password),
  13.     "Content-type": "application/json",
  14. };
  15.  
  16. const DOMElements = {
  17.     listVenuesBtn: document.getElementById("getVenues"),
  18.     venuesDate: document.getElementById("venueDate"),
  19.     venueInfo: document.getElementById("venue-info"),
  20. };
  21.  
  22. DOMElements.listVenuesBtn.addEventListener("click", getVenuesId);
  23.  
  24. function getVenuesId() {
  25.     DOMElements.venueInfo.innerHTML = "";
  26.     const date = DOMElements.venuesDate.value;
  27.     fetch(POST_URL + date, {
  28.         method: "POST",
  29.         headers: AUTH
  30.     })
  31.         .then(response => response.json())
  32.         .then(data => data.forEach(id => getVenues(id)));
  33. }
  34.  
  35. function getVenues(id) {
  36.     fetch(GET_URL + id, {
  37.         headers: AUTH
  38.     })
  39.         .then(response => response.json())
  40.         .then(data => appendVenueToDOM(data))
  41. }
  42.  
  43. function appendVenueToDOM(venue) {
  44.     const divElement = document.createElement("div");
  45.     divElement.classList.add("venue");
  46.     divElement.setAttribute("id", `${venue._id}`);
  47.     divElement.innerHTML = ` <span class="venue-name"><input class="info" type="button" value="More info">${venue.name}</span>
  48.     <div class="venue-details" style="display: none;">
  49.         <table>
  50.             <tr>
  51.                 <th>Ticket Price</th>
  52.                 <th>Quantity</th>
  53.                 <th></th>
  54.             </tr>
  55.             <tr>
  56.                 <td class="venue-price">${venue.price} lv</td>
  57.                 <td><select class="quantity">
  58.                         <option value="1">1</option>
  59.                         <option value="2">2</option>
  60.                         <option value="3">3</option>
  61.                         <option value="4">4</option>
  62.                         <option value="5">5</option>
  63.                     </select></td>
  64.                 <td><input class="purchase" type="button" value="Purchase"></td>
  65.             </tr>
  66.         </table>
  67.         <span class="head">Venue description:</span>
  68.         <p class="description">${venue.description}</p>
  69.         <p class="description">Starting time: ${venue.startingHour}</p>
  70.     </div>`;
  71.     const moreInfoBtn = divElement.getElementsByClassName("venue-name")[0];
  72.     const purchaseBtn = divElement.getElementsByClassName("purchase")[0];
  73.     purchaseBtn.addEventListener("click", purchaseTickets);
  74.     moreInfoBtn.addEventListener("click", loadMoreInfo);
  75.     DOMElements.venueInfo.appendChild(divElement);
  76. }
  77.  
  78. function loadMoreInfo(ev) {
  79.     const moreInfoMenu = ev.target.parentNode.parentNode.children[1];
  80.     moreInfoMenu.style.display = "block";
  81. }
  82.  
  83. function purchaseTickets(ev) {
  84.     const ticketsInfoTable = ev.target.parentNode.parentNode;
  85.     const name = ticketsInfoTable.parentNode.parentNode.parentNode.parentNode.children[0].textContent;
  86.     const id = ticketsInfoTable.parentNode.parentNode.parentNode.parentNode.id;
  87.  
  88.     const quantity = ticketsInfoTable.getElementsByClassName("quantity")[0].value;
  89.     let ticketPrice = ticketsInfoTable.getElementsByClassName("venue-price")[0].textContent;
  90.     ticketPrice = parseInt(ticketPrice.substring(0, ticketPrice.length - 3));
  91.  
  92.     DOMElements.venueInfo.innerHTML = `<span class="head">Confirm purchase</span>
  93.                                       <div class="purchase-info">
  94.                                         <span>${name}</span>
  95.                                         <span>${quantity} x ${ticketPrice}</span>
  96.                                         <span>Total: ${quantity * ticketPrice} lv</span>
  97.                                         <input type="button" value="Confirm">
  98.                                       </div>`;
  99.     const confirmBtn = DOMElements.venueInfo.getElementsByTagName("input")[0];
  100.     confirmBtn.addEventListener("click", function () {
  101.         confirmPurchase(id, quantity)
  102.     });
  103. }
  104.  
  105. function confirmPurchase(id, qty) {
  106.     fetch(`https://baas.kinvey.com/rpc/kid_BJ_Ke8hZg/custom/purchase?venue=${id}&qty=${qty}`, {
  107.         method: "POST",
  108.         headers: AUTH
  109.     })
  110.         .then(response => response.json())
  111.         .then(data => showTicket(data))
  112. }
  113.  
  114. function showTicket(ticketHtml) {
  115.  
  116.     DOMElements.venueInfo.innerHTML = "You may print this page as your ticket." + ticketHtml.html;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement