the bola v2 website
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

main.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. import AOS from "aos";
  2. import jQuery from "jquery";
  3. import queryString from "query-string";
  4. //import API from './api/api';
  5. const server_url = "https://thebola.club";
  6. export default class API {
  7. constructor() {
  8. this.prefix = server_url;
  9. this.url = "/api";
  10. this.test = 200;
  11. }
  12. listLocation({ _search, _page = 1, _pagesize = 20, _callback = () => {} }) {
  13. let url = `${this.prefix}${
  14. this.url
  15. }/public/event/location?${queryString.stringify({
  16. _search,
  17. _page,
  18. _pagesize,
  19. })}`;
  20. fetch(url)
  21. .then(response => response.json())
  22. .then(data => _callback(data) )
  23. .catch(err => console.log(err))
  24. }
  25. }
  26. const $ = jQuery;
  27. const api = new API();
  28. var bookingList = [];
  29. var bookingRules = [];
  30. setTimeout(() => {
  31. console.log("AOS init");
  32. AOS.init();
  33. }, 500);
  34. $(document).ready(function () {
  35. // this section is for booking page
  36. //check if booking section exist in any page
  37. let bookingContainer = $('#booking-section');
  38. if(bookingContainer.length > 0){
  39. api.listLocation({
  40. _search: "",
  41. _page: 1,
  42. _pagesize: 20,
  43. _callback: (data) => {
  44. console.log("fetch")
  45. localStorage.setItem('thebola-booking', JSON.stringify(data))
  46. bookingList = data;
  47. // set date max and min
  48. let { maxDate, minDate } = createDateInputWithRange(14)
  49. $('#input-booking-date').attr("min", minDate)
  50. $('#input-booking-date').attr("max", maxDate)
  51. },
  52. });
  53. }
  54. $(".booking-item").on("click", function () {
  55. // remove all child active class, then add active on clicked
  56. let bookingContainer = $('.booking-container');
  57. $(bookingContainer).find(".booking-item").removeClass("active")
  58. $(this).addClass("active");
  59. let fieldID = $(this).data('fieldid');
  60. //ensure everything exist
  61. if(bookingList === undefined || bookingList.length == 0){
  62. bookingList = localStorage.getItem('thebola-booking')
  63. }
  64. if(fieldID && bookingList){
  65. let { results } = bookingList;
  66. let slots = results.filter( data => data.id == fieldID )
  67. let { rules } = slots[0].slot_rules
  68. bookingRules = rules
  69. }else{
  70. console.log("no booking data")
  71. }
  72. });
  73. $('#input-booking-date').change(function() {
  74. if(bookingRules){
  75. // render rule of slot base on current input value date
  76. var dayName = getDayNameFromInput(document.getElementById("input-booking-date"));
  77. $("#input-booking-slot").children(".select-value").remove();
  78. setTimeout(() => {
  79. // append slot list
  80. bookingRules[dayName].forEach(({end_time, start_time, price, id}) => {
  81. let newOption = document.createElement('option');
  82. $(newOption).attr("id", id)
  83. $(newOption).addClass("select-value")
  84. newOption.text = `${start_time} - ${end_time} (RM ${price})`
  85. $("#input-booking-slot").append(newOption)
  86. });
  87. }, 1000);
  88. }
  89. });
  90. function createDateInputWithRange(days) {
  91. var today = new Date();
  92. var futureDate = new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
  93. var maxDate = futureDate.toISOString().split('T')[0];
  94. let minDate = today.toISOString().split('T')[0];
  95. return { maxDate, minDate };
  96. }
  97. function getDayNameFromInput(inputElement) {
  98. // Get the value of the input element
  99. var selectedDate = inputElement.value;
  100. // Create a new Date object from the input date string
  101. var date = new Date(selectedDate);
  102. // Array of day names
  103. var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  104. // Get the day of the week (0 for Sunday, 1 for Monday, ..., 6 for Saturday)
  105. var dayIndex = date.getDay();
  106. // Return the name of the day corresponding to the day index
  107. return dayNames[dayIndex];
  108. }
  109. });