StepOne.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <h3 class="ptc-title">Choose a store</h3>
  3. <div class="ptc-block">
  4. <div class="ptc-inner-md pr">
  5. <form class="search-wrap fls0" action="" @submit.prevent>
  6. <input
  7. v-model="code"
  8. type="search"
  9. class="search-input ptc-input"
  10. placeholder="Search by zip code"
  11. @input="autocomplete"
  12. @keydown="$event.keyCode === 13 && handleSearch()"
  13. />
  14. <div
  15. role="button"
  16. class="search-btn ptc-button"
  17. @click="handleSearch"
  18. ></div>
  19. </form>
  20. <div v-if="suggestions.length" class="search-suggestions">
  21. <div
  22. v-for="(item, index) of suggestions"
  23. :key="index"
  24. class="suggestion"
  25. @click="selectShop(item)"
  26. >
  27. {{ item.name }}
  28. </div>
  29. </div>
  30. <div class="shop-wrap">
  31. <div v-show="!postCode" class="tip">Nearby shops</div>
  32. <InfiniteList
  33. class="shop-list-wrap"
  34. :loading="loading"
  35. :has-more="hasMore"
  36. scroll-target=".shop-list-wrap"
  37. @loadmore="fetchData"
  38. >
  39. <ul v-if="list && list.length" class="shop-list">
  40. <li
  41. v-for="(item, index) of list"
  42. :key="index"
  43. class="shop-item border-bottom"
  44. @click="selectShop(item)"
  45. >
  46. <div class="shop-name">
  47. <span>{{ item.name }}</span>
  48. <!-- <span class="distance">3.5KM</span> -->
  49. </div>
  50. <div class="shop-address">
  51. {{ item.centre_name }},{{ item.address }}
  52. </div>
  53. <div class="shop-mark" :class="{ danger: !item.can_appointment }">
  54. <i class="icon"></i
  55. >{{
  56. item.can_appointment
  57. ? item.can_appointment_day
  58. : 'No appointments available for the next 7 days'
  59. }}
  60. </div>
  61. </li>
  62. </ul>
  63. <div v-else-if="!loading" class="ptc-empty" style="height: auto">
  64. <i class="ptc-empty-img"></i>
  65. <p class="ptc-empty-txt">No results</p>
  66. </div>
  67. </InfiniteList>
  68. </div>
  69. </div>
  70. </div>
  71. </template>
  72. <script setup lang="ts">
  73. import { ref, reactive } from 'vue'
  74. import { state } from '../store'
  75. import { getShopList } from '@/service/repair'
  76. import getLocation from '@/utils/getLocation'
  77. import InfiniteList from '@/components/infinite-list/index.vue'
  78. import { debounce } from 'lodash-es'
  79. const loading = ref(false)
  80. const hasMore = ref(true)
  81. const list = ref<any[]>()
  82. const suggestions = ref<any[]>([])
  83. const code = ref('')
  84. const postCode = ref('')
  85. let pageNo = 1
  86. let coords: GeolocationCoordinates | undefined
  87. async function fetchData() {
  88. loading.value = true
  89. try {
  90. if (!list.value) {
  91. list.value = []
  92. const res = await getLocation({ timeout: 2000 })
  93. coords = res?.coords
  94. }
  95. let { results } = await getShopList({
  96. page: pageNo,
  97. size: 10,
  98. lat: postCode.value ? undefined : coords?.latitude,
  99. lng: postCode.value ? undefined : coords?.longitude,
  100. post_code: postCode.value,
  101. })
  102. results = results.results || results
  103. list.value.push(...results.items)
  104. pageNo++
  105. hasMore.value =
  106. list.value.length < results.pageBean.totalCount || !list.value.length
  107. } catch {}
  108. loading.value = false
  109. }
  110. const autocomplete = debounce(async () => {
  111. const value = code.value.trim()
  112. if (!value) {
  113. suggestions.value = []
  114. return
  115. }
  116. const { results } = await getShopList({
  117. page: 1,
  118. size: 10,
  119. post_code: value,
  120. })
  121. suggestions.value = results.results?.items || results.items
  122. }, 300)
  123. function handleSearch() {
  124. const value = code.value.trim()
  125. if (value === postCode.value) return
  126. postCode.value = value
  127. pageNo = 1
  128. list.value = []
  129. suggestions.value = []
  130. fetchData()
  131. }
  132. function selectShop(shop: any) {
  133. state.shop = { ...shop }
  134. state.step++
  135. }
  136. </script>