@@ -3,6 +3,8 @@ const props = defineProps<{
33 handle: string
44}>()
55
6+ const route = useRoute ()
7+
68const key = ` collection-${props .handle } `
79
810const { data : collection } = await useStorefrontData (key , ` #graphql
@@ -27,16 +29,28 @@ const { data: collection } = await useStorefrontData(key, `#graphql
2729 ${COLLECTION_FRAGMENT }
2830 ${PRODUCT_CONNECTION_FRAGMENT }
2931` , {
30- variables: {
32+ variables: computed (() => ( {
3133 handle: props .handle ,
32- first: 4 ,
3334 language: ' EN' ,
3435 country: ' US' ,
35- },
36+ first: route .query .before ? undefined : 4 ,
37+ last: route .query .before ? 4 : undefined ,
38+ after: route .query .after ,
39+ before: route .query .before ,
40+ })),
3641 transform : data => data .collection ,
42+ watch: [
43+ () => route .query .after ,
44+ () => route .query .before ,
45+ ],
3746})
3847
3948const products = computed (() => flattenConnection (collection .value ?.products ))
49+
50+ const startCursor = computed (() => collection .value ?.products .pageInfo .startCursor )
51+ const endCursor = computed (() => collection .value ?.products .pageInfo .endCursor )
52+ const hasPreviousPage = computed (() => collection .value ?.products .pageInfo .hasPreviousPage )
53+ const hasNextPage = computed (() => collection .value ?.products .pageInfo .hasNextPage )
4054 </script >
4155
4256<template >
@@ -56,5 +70,23 @@ const products = computed(() => flattenConnection(collection.value?.products))
5670 :product =" product"
5771 />
5872 </div >
73+
74+ <div class =" flex justify-between mt-8" >
75+ <UButton
76+ v-if =" hasPreviousPage"
77+ :to =" `?before=${startCursor}`"
78+ icon =" i-lucide-arrow-left"
79+ >
80+ Previous
81+ </UButton >
82+
83+ <UButton
84+ v-if =" hasNextPage"
85+ :to =" `?after=${endCursor}`"
86+ trailing-icon =" i-lucide-arrow-right"
87+ >
88+ Next
89+ </UButton >
90+ </div >
5991 </div >
6092</template >
0 commit comments