@@ -55,35 +55,19 @@ export const collections = {
5555 } ,
5656
5757 // Check if next page is avaible and inject more products
58- async fetchAndRenderNextPage ( ) {
58+ async fetchAndRenderTableRows ( product_handle : string , total_pages : number , current_page : number , section_id : string , inject_before : string ) {
59+
5960 // Show loading
6061 this . pagination_loading = true ;
6162
62- // Get filter data
63- const filter = document . getElementById (
64- "js-desktopFilter" ,
65- ) as HTMLFormElement ;
66-
6763 // Get pagination count
68- const pageUrl = `&page=${ this . pagination_current_page + 1 } ` ;
69-
70- // Get search parameter
71- const searchUrl = new URL ( location . href ) . searchParams . get ( "q" )
72- ? `&q=${ new URL ( location . href ) . searchParams . get ( "q" ) } `
73- : "" ;
64+ const pageUrl = `&page=${ current_page } ` ;
7465
7566 // Build fetch url
76- let fetchUrl = `${ window . location . pathname } ?section_id=${ this . pagination_section } ${ pageUrl } ${ searchUrl } ` ;
77-
78- // If filter exists, add filter data to fetch url
79- if ( filter ) {
80- const filterData = new FormData ( filter ) ;
81- const filterUrl = this . buildUrlFilter ( filterData ) ;
82- fetchUrl += filterUrl ;
83- }
67+ let fetchUrl = `${ window . Shopify . routes . root } products/${ product_handle } ?section_id=${ section_id } ${ pageUrl } ` ;
8468
8569 // Check if new page is available
86- if ( this . pagination_current_page < this . pagination_total_pages ) {
70+ if ( current_page <= total_pages ) {
8771 // Get data from Shopify
8872 try {
8973 const response = await fetch ( fetchUrl ) ;
@@ -94,24 +78,27 @@ export const collections = {
9478 tempElement . innerHTML = data ;
9579
9680 // Find the results within the fetched data
97- const fetchedElement = tempElement . querySelector ( "# js-results" ) ;
81+ const fetchedElement = tempElement . querySelector ( ". js-results" ) ;
9882
9983 // If results are found, append its innerHTML to the existing element on the page
10084 if ( fetchedElement ) {
101- const resultsElement = document . getElementById ( "js-results" ) ;
102- if ( resultsElement ) {
103- resultsElement . insertAdjacentHTML (
85+ const injectSpot = document . querySelector ( inject_before ) ;
86+ if ( injectSpot ) {
87+ injectSpot . insertAdjacentHTML (
10488 "beforeend" ,
10589 fetchedElement . innerHTML ,
10690 ) ;
10791 }
10892 }
10993
110- // Update next page url
111- this . pagination_current_page += 1 ;
94+ if ( current_page >= total_pages ) {
95+ this . pagination_load_more_button = false ;
96+ }
11297
11398 // Reset loading
11499 this . loadImages ( ) ;
100+
101+ // Reset loading
115102 this . pagination_loading = false ;
116103 } catch ( error ) {
117104 console . error ( "Error:" , error ) ;
@@ -124,6 +111,100 @@ export const collections = {
124111 this . pagination_loading = false ;
125112 }
126113 } ,
114+
115+ // Check if next page is avaible and inject more products
116+ async fetchAndRenderPage ( direction : "next" | "previous" ) {
117+ // Prevent browser from scrolling down
118+ history . scrollRestoration = "manual" ;
119+
120+ // Show loading
121+ this . pagination_loading = true ;
122+
123+ // Update URL to show updated page number
124+ if ( direction === "next" ) {
125+ let url = new URL ( window . location . href ) ;
126+ url . searchParams . set ( "page" , this . pagination_current_page + 1 ) ;
127+ window . history . pushState ( { } , "" , url . toString ( ) ) ;
128+ }
129+
130+ // Get filter data
131+ const filter = document . getElementById ( "js-desktopFilter" ) as HTMLFormElement ;
132+
133+ // Get pagination count
134+ const pageUrl = `&page=${ direction === "next" ? this . pagination_current_page + 1 : this . pagination_current_page - 1 } ` ;
135+
136+ // Get search parameter
137+ const searchUrl = new URL ( location . href ) . searchParams . get ( "q" ) ? `&q=${ new URL ( location . href ) . searchParams . get ( "q" ) } ` : '' ;
138+
139+ // Build fetch url
140+ let fetchUrl = `${ window . location . pathname } ?section_id=${ this . pagination_section } ${ pageUrl } ${ searchUrl } ` ;
141+
142+ // If filter exists, add filter data to fetch url
143+ if ( filter ) {
144+ const filterData = new FormData ( filter ) ;
145+ const filterUrl = this . buildUrlFilter ( filterData ) ;
146+ fetchUrl += filterUrl ;
147+ }
148+
149+ // Check if new page is available
150+ if ( this . pagination_current_page < this . pagination_total_pages ||
151+ direction === "previous" ) {
152+
153+ // Get data from Shopify
154+ try {
155+ const response = await fetch ( fetchUrl ) ;
156+ const data = await response . text ( ) ;
157+
158+ // Create a new HTML element and set its innerHTML to the fetched data
159+ const tempElement = document . createElement ( "div" ) ;
160+ tempElement . innerHTML = data ;
161+
162+ // Find the results within the fetched data
163+ const fetchedElement = tempElement . querySelector ( "#js-results" ) ;
164+
165+ // If results are found, append its innerHTML to the existing element on the page
166+ if ( fetchedElement ) {
167+ const resultsElement = document . getElementById ( "js-results" ) ;
168+ if ( resultsElement ) {
169+ if ( direction === "next" ) {
170+ resultsElement . insertAdjacentHTML (
171+ "beforeend" ,
172+ fetchedElement . innerHTML ,
173+ ) ;
174+ } else {
175+ resultsElement . insertAdjacentHTML (
176+ "afterbegin" ,
177+ fetchedElement . innerHTML ,
178+ ) ;
179+ }
180+ }
181+ }
182+
183+ // Update next page url
184+ // Update next page url
185+ if ( direction === "next" ) {
186+ this . pagination_current_page += 1 ;
187+ this . pagination_pages_loaded += 1 ;
188+ } else {
189+ this . pagination_current_page -= 1 ;
190+ }
191+
192+ // Reset loading
193+ // this.loadImages();
194+ this . pagination_loading = false ;
195+ }
196+
197+ catch ( error ) {
198+ console . error ( "Error:" , error ) ;
199+ this . pagination_loading = false ;
200+ }
201+ }
202+
203+ // If last page, stop loading
204+ else {
205+ this . pagination_loading = false ;
206+ }
207+ } ,
127208
128209 // Load quick add with section render
129210 async fetchAndRenderQuickGallery ( product_handle : string ) {
0 commit comments