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.

226 lines
6.0 KiB

3 years ago
3 years ago
  1. <template>
  2. <div class="white-bkg">
  3. <header>
  4. <div class="header__col">
  5. <button class="btn btn--primary" @click="goBack">
  6. <svg
  7. xmlns="http://www.w3.org/2000/svg"
  8. class="icon icon-tabler icon-tabler-chevrons-left"
  9. width="24"
  10. height="24"
  11. viewBox="0 0 24 24"
  12. stroke-width="3"
  13. stroke="#ffffff"
  14. fill="none"
  15. stroke-linecap="round"
  16. stroke-linejoin="round"
  17. >
  18. <path stroke="none" d="M0 0h24v24H0z" fill="none" />
  19. <polyline points="11 7 6 12 11 17" />
  20. <polyline points="17 7 12 12 17 17" />
  21. </svg> back
  22. </button>
  23. </div>
  24. <div class="header__col">
  25. <Logo />
  26. </div>
  27. <div class="header__col" />
  28. </header>
  29. <main class="main-with-header">
  30. <ChosenFlights
  31. :selected-orig="selectedOrig"
  32. :selected-dest="selectedDest"
  33. style="margin: 0 1rem;"
  34. />
  35. <!-- <pre>
  36. {{ schedules }}
  37. </pre> -->
  38. <DatePicker
  39. :starting-date="startingDate"
  40. :selected-dows="selectedDows"
  41. />
  42. </main>
  43. </div>
  44. </template>
  45. <script>
  46. export default {
  47. data () {
  48. return {
  49. schedules: [],
  50. startingDate: new Date(),
  51. selectedSchedule: {},
  52. selectedDays: [],
  53. selectedOrig: {},
  54. selectedDest: {}
  55. }
  56. },
  57. async fetch () {
  58. // console.log(this.$route.params.o)
  59. const today = new Date().toLocaleDateString('en-CA')
  60. // function addDays (date, days) {
  61. // const result = new Date(date)
  62. // result.setDate(result.getDate() + days)
  63. // return result
  64. // }
  65. // const tomorrow = addDays(new Date(), 1).toLocaleDateString('en-CA')
  66. const scheduleFetchFilterFormula = `AND(Is_Current="Yes",Origin_IATA="${this.$route.params.o}",Destination_IATA="${this.$route.params.d}",Effective_End>"${today}")`
  67. const scheduleFetchSort = '&sort[0][field]=Departure_TimeL&sort[0][direction]=asc'
  68. const response = await fetch(`https://api.airtable.com/v0/appiQwfVZixRgRICe/Schedule?filterByFormula=${scheduleFetchFilterFormula}${scheduleFetchSort}`, {
  69. method: 'GET',
  70. headers: {
  71. 'Content-Type': 'application/x-www-form-urlencoded',
  72. Authorization: 'Bearer keyJ2ht64ZSN57AG1'
  73. }
  74. })
  75. const data = await response.json()
  76. // console.log(data)
  77. this.schedules = [...data.records]
  78. },
  79. computed: {
  80. selectedDows () {
  81. const mappedDows = this.schedules.map((schedule) => {
  82. // console.log(schedule.fields)
  83. return {
  84. Flight_Number: schedule.fields.Flight_Number,
  85. DowsList: this.daysList(schedule.fields.Frequency_Convert),
  86. Effective_Start: schedule.fields.Effective_Start,
  87. Effective_End: schedule.fields.Effective_End,
  88. Departure_TimeL: schedule.fields.Departure_TimeL
  89. }
  90. })
  91. return mappedDows
  92. }
  93. },
  94. created () {
  95. // console.log(this.$route.path)
  96. if (this.$route && this.$route.params && this.$route.params.o) {
  97. this.fetchSingleAirport(this.$route.params.o, true)
  98. }
  99. if (this.$route && this.$route.params && this.$route.params.d) {
  100. this.fetchSingleAirport(this.$route.params.d, false)
  101. }
  102. },
  103. methods: {
  104. daysList (days) {
  105. const list = days.split(', ').map((day) => {
  106. const name = day.toLowerCase().slice(0, -1)
  107. let num
  108. switch (name) {
  109. case 'su':
  110. num = 0
  111. break
  112. case 'mo':
  113. num = 1
  114. break
  115. case 'tu':
  116. num = 2
  117. break
  118. case 'we':
  119. num = 3
  120. break
  121. case 'th':
  122. num = 4
  123. break
  124. case 'fr':
  125. num = 5
  126. break
  127. case 'sa':
  128. num = 6
  129. break
  130. default:
  131. break
  132. }
  133. return {
  134. name,
  135. num
  136. }
  137. })
  138. return list.sort(function (a, b) {
  139. return a.num - b.num
  140. })
  141. },
  142. goBack () {
  143. this.$router.go(-1)
  144. },
  145. book (schedule) {
  146. if (schedule.fields.IsExpedia[0] === 'true') {
  147. this.startingDate = new Date()
  148. this.selectedSchedule = schedule.fields
  149. this.selectedDays = this.daysList(schedule.fields.Frequency_Convert)
  150. } else {
  151. window.open(schedule.fields['BookingLink (from Carriers_Alaska)'][0])
  152. }
  153. },
  154. clearSelectedSchedule () {
  155. console.log('clear')
  156. this.startingDate = ''
  157. this.selectedSchedule = {}
  158. },
  159. async fetchSingleAirport (iata, isOrig) {
  160. const airportFetchFilterFormula =
  161. isOrig
  162. ? `AND(Is_Origin=1,{IsCurrent-AsOrigin}="Yes",Airport_IATA="${iata}")`
  163. : `AND(Is_Destination=1,{IsCurrent-AsDest}="Yes",Airport_IATA="${iata}")`
  164. const response = await fetch(`https://api.airtable.com/v0/appiQwfVZixRgRICe/Airports_IATA?filterByFormula=${airportFetchFilterFormula}`, {
  165. method: 'GET',
  166. headers: {
  167. 'Content-Type': 'application/x-www-form-urlencoded',
  168. Authorization: 'Bearer keyJ2ht64ZSN57AG1'
  169. }
  170. })
  171. const mapData = await response.json()
  172. const thisAirport = {
  173. iata: mapData.records[0].fields.Airport_IATA,
  174. lat: mapData.records[0].fields.Latitude_Deg,
  175. long: mapData.records[0].fields.Longitude_Deg,
  176. icon: mapData.records[0].fields.Icon_Url,
  177. name: mapData.records[0].fields.Airport_Name,
  178. municipality: mapData.records[0].fields.Municipality,
  179. type: mapData.records[0].fields.Type,
  180. search: mapData.records[0].fields.Search_Field
  181. }
  182. switch (isOrig) {
  183. case true:
  184. this.selectedOrig = { ...thisAirport }
  185. break
  186. case false:
  187. this.selectedDest = { ...thisAirport }
  188. break
  189. default:
  190. break
  191. }
  192. }
  193. }
  194. }
  195. </script>
  196. <style lang="scss" scoped>
  197. .main-with-header {
  198. margin-top: clamp(20px + 1.75rem, 4rem + 1.75rem, 50px + 1.75rem);
  199. }
  200. </style>