|
3 | 3 | <div class="mx-auto box-content w-[calc(100%+40px)] max-w-[935px] grow p-1"> |
4 | 4 | <div role="tablist" class="my-4 flex max-w-full flex-col items-stretch"> |
5 | 5 | <div class="flex flex-row flex-nowrap items-center gap-4"> |
6 | | - <HeaderLogo /> |
| 6 | + <div class="flex items-center justify-center"> |
| 7 | + <NuxtLink to="/"> |
| 8 | + <img class="mx-auto my-0 h-10 rounded-xl" src="/logo.png" alt="logo" /> |
| 9 | + </NuxtLink> |
| 10 | + </div> |
7 | 11 | <div class="flex flex-shrink flex-grow flex-col text-sm font-semibold text-neutral-600 dark:text-neutral-200"> |
8 | 12 | <form |
9 | 13 | class="group flex h-10 flex-grow rounded-xl border border-transparent bg-neutral-800/5 dark:bg-white/10 pl-4 pr-3 transition-all focus-within:border-neutral-400 focus-within:dark:border-neutral-600 focus-within:bg-white/30 focus-within:dark:bg-neutral-800/30 hover:border-neutral-300 hover:focus-within:border-neutral-400 hover:dark:border-neutral-600"> |
|
145 | 149 | </div> |
146 | 150 | </template> |
147 | 151 |
|
148 | | -<script setup> |
149 | | -import getProducts from '~/gql/queries/getProducts.gql'; |
150 | | -import getCategories from '~/gql/queries/getCategories.gql'; |
151 | | -const isDropdownSortBy = ref(false); |
152 | | -const isDropdownCategory = ref(false); |
153 | | -const router = useRouter(); |
154 | | -const route = useRoute(); |
155 | | -const searchTerm = ref(route.query.search || ''); |
156 | | -const selectedCategory = ref(route.query.category || ''); |
157 | | -const sortByOrder = ref(route.query.orderby && route.query.orderby !== '' ? route.query.orderby : 'DESC'); |
158 | | -const sortByField = ref(route.query.fieldby && route.query.fieldby !== '' ? route.query.fieldby : 'DATE'); |
159 | | -const variables = ref({ |
160 | | - search: searchTerm, |
161 | | - category: selectedCategory, |
162 | | - order: sortByOrder, |
163 | | - field: sortByField, |
164 | | -}); |
165 | | -
|
166 | | -const { result: categoriesResult } = useQuery(getCategories); |
167 | | -const { result: productsResult, loading, fetchMore } = useQuery(getProducts, variables.value); |
168 | | -const products = computed(() => productsResult.value?.products.nodes); |
169 | | -const empty = computed(() => productsResult.value?.products.nodes.length); |
170 | | -const pageInfo = computed(() => productsResult.value?.products.pageInfo); |
171 | | -const categories = computed(() => categoriesResult.value?.productCategories.nodes.filter((categories) => categories.products.nodes.length && categories.children.nodes.length)); |
172 | | -
|
173 | | -const options = reactive([{ value: 'Newest' }, { value: 'Price: High to Low' }, { value: 'Price: Low to High' }]); |
174 | | -
|
175 | | -const selectedOption = ref(sortByOrder.value === 'DESC' && sortByField.value === 'DATE' ? 'Newest' : sortByOrder.value === 'DESC' ? 'Price: High to Low' : 'Price: Low to High'); |
176 | | -
|
177 | | -const loadMore = () => { |
178 | | - fetchMore({ |
179 | | - variables: { |
180 | | - after: pageInfo.value?.endCursor, |
181 | | - }, |
182 | | - updateQuery(prev, { fetchMoreResult }) { |
183 | | - const mergedData = { |
184 | | - ...prev, |
185 | | - }; |
186 | | - mergedData.products = { |
187 | | - ...prev.products, |
188 | | - nodes: [...prev.products.nodes, ...fetchMoreResult.products.nodes], |
189 | | - }; |
190 | | - mergedData.products.pageInfo = fetchMoreResult.products.pageInfo; |
191 | | - return mergedData; |
192 | | - }, |
193 | | - }); |
194 | | -}; |
195 | | -
|
196 | | -const handleScroll = () => { |
197 | | - const scrollPosition = window.scrollY + window.innerHeight; |
198 | | - const loadMorePosition = document.documentElement.scrollHeight - 1600; |
199 | | -
|
200 | | - if (scrollPosition >= loadMorePosition && pageInfo.value?.hasNextPage && !loading.value) { |
201 | | - loadMore(); |
202 | | - } |
203 | | -}; |
204 | | -
|
205 | | -const handleClickOutside = (event) => { |
206 | | - if (!event.target.closest('.dropdown')) { |
207 | | - isDropdownSortBy.value = false; |
208 | | - isDropdownCategory.value = false; |
209 | | - } |
210 | | -}; |
211 | | -
|
212 | | -onMounted(() => { |
213 | | - window.addEventListener('scroll', handleScroll); |
214 | | - document.addEventListener('click', handleClickOutside); |
215 | | -}); |
216 | | -
|
217 | | -onUnmounted(() => { |
218 | | - window.removeEventListener('scroll', handleScroll); |
219 | | - document.removeEventListener('click', handleClickOutside); |
220 | | -}); |
221 | | -
|
222 | | -watch([selectedOption, searchTerm, selectedCategory], ([newSelectedOption, newSearchTerm, newCategory]) => { |
223 | | - let updatedQuery = { |
224 | | - ...route.query, |
225 | | - search: newSearchTerm || undefined, |
226 | | - category: newCategory || undefined, |
227 | | - }; |
228 | | -
|
229 | | - switch (newSelectedOption) { |
230 | | - case 'Newest': |
231 | | - sortByOrder.value = 'DESC'; |
232 | | - sortByField.value = 'DATE'; |
233 | | - updatedQuery = { |
234 | | - ...updatedQuery, |
235 | | - orderby: undefined, |
236 | | - fieldby: undefined, |
237 | | - }; |
238 | | - break; |
239 | | - case 'Price: High to Low': |
240 | | - sortByOrder.value = 'DESC'; |
241 | | - sortByField.value = 'PRICE'; |
242 | | - updatedQuery = { |
243 | | - ...updatedQuery, |
244 | | - orderby: sortByOrder.value || undefined, |
245 | | - fieldby: sortByField.value || undefined, |
246 | | - }; |
247 | | - break; |
248 | | - case 'Price: Low to High': |
249 | | - sortByOrder.value = 'ASC'; |
250 | | - sortByField.value = 'PRICE'; |
251 | | - updatedQuery = { |
252 | | - ...updatedQuery, |
253 | | - orderby: sortByOrder.value || undefined, |
254 | | - fieldby: sortByField.value || undefined, |
255 | | - }; |
256 | | - break; |
257 | | - } |
258 | | - router.push({ |
259 | | - query: updatedQuery, |
260 | | - }); |
261 | | -}); |
| 152 | +<script setup lang="ts"> |
| 153 | +const { products, empty, loading, categories, options, searchTerm, selectedCategory, selectedOption, isDropdownCategory, isDropdownSortBy } = useSearch(); |
262 | 154 | </script> |
263 | 155 |
|
264 | 156 | <style lang="postcss"> |
|
0 commit comments