Advertisement
braveheart1989

Venuemaster

Nov 30th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function attachEvents() {
  2.     let authorization = "Basic " + btoa("guest" + ":" + "pass");
  3.     $('#getVenues').click(getVenues);
  4.     let baseUrl = 'https://baas.kinvey.com/';
  5.  
  6.     const requestHeaders = {
  7.         "Authorization": authorization,
  8.         "Content-Type": "application/json"
  9.     };
  10.  
  11.     function getVenues() {
  12.         let venueDate = $('#venueDate');
  13.         let request = {
  14.             method: "POST",
  15.             url: baseUrl + "rpc/kid_BJ_Ke8hZg/custom/calendar?query=" + venueDate.val(),
  16.             headers: requestHeaders
  17.         };
  18.         $.ajax(request)
  19.             .then(getInformation)
  20.             .catch(showError);
  21.  
  22.         function getInformation(dataID) {
  23.             let infoArray = [];
  24.             for (let id of dataID) {
  25.                 let request = {
  26.                     method: "GET",
  27.                     url: baseUrl + "appdata/kid_BJ_Ke8hZg/venues/" + id,
  28.                     headers: requestHeaders
  29.                     // success: function (response) {
  30.                     //     infoArray.push(response)
  31.                     // }
  32.                 };
  33.                 infoArray.push($.ajax(request));
  34.             }
  35.  
  36.             Promise.all(infoArray)
  37.                 .then(renderHTML)
  38.                 .then(renderMoreInfo);
  39.             function renderHTML(venue) {
  40.                 let venueInfo = $('#venue-info').empty();
  41.                 for (let ven of venue) {
  42.                     let html = `<div class="venue" id="${ven._id}">
  43.                   <span class="venue-name"><input class="info" type="button" value="More info">${ven.name}</span>
  44.                   <div class="venue-details" style="display: none;">
  45.                     <table>
  46.                       <tr><th>Ticket Price</th><th>Quantity</th><th></th></tr>
  47.                       <tr>
  48.                         <td class="venue-price">${ven.price} lv</td>
  49.                         <td><select class="quantity">
  50.                           <option value="1">1</option>
  51.                           <option value="2">2</option>
  52.                           <option value="3">3</option>
  53.                           <option value="4">4</option>
  54.                           <option value="5">5</option>
  55.                         </select></td>
  56.                         <td><input class="purchase" type="button" value="Purchase"></td>
  57.                       </tr>
  58.                     </table>
  59.                     <span class="head">Venue description:</span>
  60.                     <p class="description">${ven.description}</p>
  61.                     <p class="description">Starting time: ${ven.startingHour}</p>
  62.                   </div>
  63.                 </div>`;
  64.                     venueInfo.append(html);
  65.                 }
  66.                 return venue;
  67.             }
  68.  
  69.             function renderMoreInfo() {
  70.                 let arrInfo = [];
  71.                 $('.info').click(function () {
  72.                     let info = $(this).parent().parent().find('.venue-details');
  73.                     if (info.is(':hidden')) {
  74.                         info.css('display', 'block');
  75.                     }
  76.                     Promise.all(arrInfo).then(renderPurchase)
  77.                 });
  78.             }
  79.  
  80.             function renderPurchase() {
  81.                 $('.purchase').click(function () {
  82.                     let id = $(this).parent().parent().parent().parent().parent().parent().attr('id');
  83.                     let name=$(this).parent().parent().parent().parent().parent().parent().find('.venue-name').text();
  84.                     let price = $(this).parent().parent().parent().find('.venue-price').text().replace(' lv', '');
  85.                     let quantity = $(this).parent().parent().parent().find('.quantity').find('option:selected').text();
  86.                     let sum = Number(price) * Number(quantity).toFixed(2);
  87.                     let html = `<span class="head">Confirm purchase</span>
  88.                     <div class="purchase-info">
  89.                       <span>${name}</span>
  90.                       <span>${quantity} x ${price}.00</span>
  91.                       <span>Total: ${sum}.00 lv</span>
  92.                       <input type="button" value="Confirm">
  93.                     </div>`;
  94.  
  95.                     $('#venue-info').empty();
  96.                     $('#venue-info').append(html);
  97.  
  98.                     $('.purchase-info').find('input').click(function () {
  99.                         let request = {
  100.                             method: "POST",
  101.                             url: baseUrl + `rpc/kid_BJ_Ke8hZg/custom/purchase?venue=${id}&qty=${quantity}`,
  102.                             headers: requestHeaders,
  103.                             success: function (data) {
  104.                                 $('#venue-info').append(data)
  105.                             },
  106.                             error: function (error) {
  107.                                 alert(error)
  108.                             }
  109.                         };
  110.                         $.ajax(request).then(function (data) {
  111.                             $('.purchase-info').empty();
  112.                             $('.head').empty();
  113.                             $('#venue-info').text("You may print this page as your ticket").append(data.html)
  114.                         })
  115.                     })
  116.                 })
  117.             }
  118.         }
  119.     }
  120.     function showError(error) {
  121.         alert(JSON.stringify(error))
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement