StepOne.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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" action="">
  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 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. </InfiniteList>
  64. </div>
  65. </div>
  66. </div>
  67. </template>
  68. <script setup lang="ts">
  69. import { ref, reactive } from 'vue'
  70. import { state } from '../store'
  71. import { getShopList } from '@/service/repair'
  72. import getLocation from '@/utils/getLocation'
  73. import InfiniteList from '@/components/infinite-list/index.vue'
  74. import { debounce } from 'lodash-es'
  75. const loading = ref(false)
  76. const hasMore = ref(true)
  77. const list = ref<any[]>()
  78. const suggestions = ref<any[]>([])
  79. const code = ref('')
  80. const postCode = ref('')
  81. let pageNo = 1
  82. let coords: GeolocationCoordinates | undefined
  83. async function fetchData() {
  84. loading.value = true
  85. try {
  86. if (!list.value) {
  87. list.value = []
  88. const res = await getLocation({ timeout: 2000 })
  89. coords = res?.coords
  90. }
  91. let { results } = await getShopList({
  92. page: pageNo,
  93. size: 10,
  94. lat: coords?.latitude,
  95. lng: coords?.longitude,
  96. post_code: postCode.value,
  97. })
  98. results = results.results || results
  99. list.value.push(...results.items)
  100. pageNo++
  101. hasMore.value = list.value.length < results.pageBean.totalCount
  102. } catch {}
  103. loading.value = false
  104. }
  105. const autocomplete = debounce(async () => {
  106. const value = code.value.trim()
  107. if (!value) {
  108. suggestions.value = []
  109. return
  110. }
  111. const { results } = await getShopList({
  112. page: 1,
  113. size: 10,
  114. post_code: value,
  115. })
  116. suggestions.value = results.results?.items || results.items
  117. }, 300)
  118. function handleSearch() {
  119. const value = code.value.trim()
  120. if (value === postCode.value) return
  121. postCode.value = value
  122. pageNo = 1
  123. list.value = []
  124. suggestions.value = []
  125. fetchData()
  126. }
  127. function selectShop(shop: any) {
  128. state.shop = { ...shop }
  129. state.step++
  130. }
  131. </script>