the bola v2 website
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.

main.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. import AOS from "aos";
  2. import jQuery from "jquery";
  3. import API from './api/api';
  4. const $ = jQuery;
  5. const api = new API();
  6. var bookingRules = [];
  7. setTimeout(() => {
  8. console.log("AOS init");
  9. AOS.init();
  10. }, 500);
  11. $(document).ready(function () {
  12. $('.article-carousel-item').on('click', function(){
  13. let redirectlink = $(this).data('href')
  14. location.href = redirectlink;
  15. })
  16. $('#contact-form').on('submit', function(e){
  17. e.preventDefault();
  18. let name = $(this).find("#name").val();
  19. let email = $(this).find("#email").val();
  20. let message = $(this).find("#message").val();
  21. let submitButton = $(this).find('#submitButton');
  22. let loadingSpan = document.createElement('span');
  23. $(submitButton).prop("disabled",true);
  24. $(loadingSpan).attr("role", "status");
  25. $(loadingSpan).attr("aria-hidden", "true");
  26. $(loadingSpan).addClass("spinner-border spinner-border-sm");
  27. submitButton.text("Sending ");
  28. $(submitButton).append(loadingSpan)
  29. const _callback = (resp) => {
  30. let {status} = resp
  31. if(status === "Success"){
  32. submitButton.text("Success");
  33. $("#name").val("");
  34. $("#email").val("");
  35. $("#message").val("");
  36. } else {
  37. submitButton.text("Fail");
  38. }
  39. setTimeout(()=>{
  40. $(submitButton).prop("disabled",false);
  41. submitButton.text("Submit");
  42. },1000)
  43. }
  44. api.enquiry({name, email, message}, _callback)
  45. })
  46. // this section is for booking page
  47. //check if booking section exist in any page
  48. let bookingContainer = $('#booking-section');
  49. if(bookingContainer.length > 0){
  50. api.listState({
  51. _callback: ({state_city}) => {
  52. const selectLocationInput = $("#select-location");
  53. $(selectLocationInput).children(".select-value").remove()
  54. if(Array.isArray(state_city)){
  55. state_city.forEach(({state, city}, i) => {
  56. let newOption = document.createElement('option');
  57. $(newOption).attr("id", `location_${i}`)
  58. $(newOption).attr("value",JSON.stringify({state,city}))
  59. $(newOption).addClass("select-value")
  60. newOption.text = `${city}, ${state}`
  61. $(selectLocationInput).append(newOption)
  62. });
  63. }
  64. $(selectLocationInput).on('change', ()=>{
  65. resetField()
  66. })
  67. }
  68. })
  69. // set date max and min
  70. let { maxDate, minDate } = createDateInputWithRange(14)
  71. let inputDate = $('#input-booking-date')
  72. $(inputDate).attr("min", minDate)
  73. $(inputDate).attr("max", maxDate)
  74. resetDate()
  75. }
  76. $("#select-location").on('change', function(){
  77. resetDate()
  78. resetRules()
  79. resetField()
  80. if(this.value !== "default" ){
  81. let {state, city} = JSON.parse(this.value);
  82. api.listLocation({
  83. state,
  84. city,
  85. _callback: ({field_location}) => {
  86. let template = $('#field-location-template')
  87. let container = $(template).parent()
  88. //reset eveything
  89. $(container).children(".select-field").remove()
  90. try{
  91. field_location.forEach(({field_name, city, state, photo_path}) => {
  92. let newField = $(template).clone()
  93. $(newField).css('display','block')
  94. $(newField).removeAttr("id")
  95. $(newField).addClass("select-field")
  96. $(newField).find('.field-location-text').text("")
  97. $(newField).on('click', ()=>{ handleClickFieldLocation(city, state, field_name, newField) })
  98. $(newField).find('.field-location-img').attr("style", `background-image:url(https://thebola.club/files/${photo_path})`)
  99. $(container).append(newField)
  100. });
  101. }catch(err){
  102. console.log(err)
  103. }
  104. },
  105. });
  106. }
  107. })
  108. const handleClickFieldLocation = function (city, state, field_name, refElem) {
  109. $("#field-location-container").find(".select-field").removeClass("active")
  110. $(refElem).addClass("active")
  111. resetDate()
  112. resetRules()
  113. resetField()
  114. api.listField({state, city, field_name, _callback:({results})=>{
  115. let container = $('#field-container')
  116. let template = $('#field-template')
  117. try{
  118. results.forEach(({location_name, photo_path, slot_rules}) => {
  119. let newField = $(template).clone()
  120. $(newField).css('display','block')
  121. $(newField).removeAttr("id")
  122. $(newField).addClass("select-field")
  123. $(newField).find('.field-text').text(location_name)
  124. $(newField).on('click', ()=>{ handleClickField(slot_rules, newField)})
  125. $(newField).find('.field-img').attr("style", `background-image:url(https://thebola.club/files/${photo_path})`)
  126. $(container).append(newField)
  127. });
  128. }catch(err){
  129. console.log(err)
  130. }
  131. }})
  132. }
  133. const handleClickField = function (slot_rules,refElem){
  134. // remove all child active class, then add active on clicked
  135. let fieldContainer = $('#field-container');
  136. $(fieldContainer).find(".select-field").removeClass("active")
  137. $(refElem).addClass("active");
  138. resetRules()
  139. resetDate()
  140. bookingRules = slot_rules.rules
  141. }
  142. $('#input-booking-date').change(function() {
  143. if(bookingRules){
  144. // render rule of slot base on current input value date
  145. var dayName = getDayNameFromInput(document.getElementById("input-booking-date"));
  146. resetRules()
  147. setTimeout(() => {
  148. // append slot list
  149. bookingRules[dayName].forEach(({end_time, start_time, price, id}) => {
  150. let newOption = document.createElement('option');
  151. $(newOption).attr("id", id)
  152. $(newOption).addClass("select-value")
  153. newOption.text = `${start_time} - ${end_time} (RM ${price})`
  154. $("#input-booking-slot").append(newOption)
  155. });
  156. }, 1000);
  157. }
  158. });
  159. function createDateInputWithRange(days) {
  160. var today = new Date();
  161. var futureDate = new Date(today.getTime() + days * 24 * 60 * 60 * 1000);
  162. var maxDate = futureDate.toISOString().split('T')[0];
  163. let minDate = today.toISOString().split('T')[0];
  164. return { maxDate, minDate };
  165. }
  166. function getDayNameFromInput(inputElement) {
  167. var selectedDate = inputElement.value;
  168. var date = new Date(selectedDate);
  169. var dayNames = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
  170. var dayIndex = date.getDay();
  171. return dayNames[dayIndex];
  172. }
  173. function resetField(){
  174. $('#field-container').children(".select-field").remove()
  175. }
  176. function resetDate(){
  177. $('#input-booking-date').val("")
  178. }
  179. function resetRules(){
  180. $("#input-booking-slot").children(".select-value").remove();
  181. }
  182. });