Skip to content

Commit 11650b9

Browse files
committed
Add the load more
1 parent 312c186 commit 11650b9

File tree

9 files changed

+33255
-14436
lines changed

9 files changed

+33255
-14436
lines changed

assets/build/assets.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('js/main.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'a3b4a89a5cfd5eff3880117abb72bf0c'), 'js/single.js' => array('dependencies' => array('wp-polyfill'), 'version' => '7fead00701b6b38d5570a4ed8e130ec7'), 'js/editor.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'ee640f54cf55292f6221a70587326775'), 'js/blocks.js' => array('dependencies' => array('lodash', 'react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-polyfill'), 'version' => '3d35cad1be3150206c0c17e25b2b99e6'), 'js/author.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'd0de12045532844ce7f3c7e552d71c7a'), 'js/search.js' => array('dependencies' => array('wp-polyfill'), 'version' => '9ecb57bca86ee00854b1e86fabec0e8b'));
1+
<?php return array('js/main.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'a3b4a89a5cfd5eff3880117abb72bf0c'), 'js/single.js' => array('dependencies' => array('wp-polyfill'), 'version' => '7fead00701b6b38d5570a4ed8e130ec7'), 'js/editor.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'ee640f54cf55292f6221a70587326775'), 'js/blocks.js' => array('dependencies' => array('lodash', 'react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-i18n', 'wp-polyfill'), 'version' => '3d35cad1be3150206c0c17e25b2b99e6'), 'js/author.js' => array('dependencies' => array('wp-polyfill'), 'version' => 'd0de12045532844ce7f3c7e552d71c7a'), 'js/search.js' => array('dependencies' => array('wp-polyfill'), 'version' => '1eeb880184b340e6d211201b060689ef'));

assets/build/css/search.css

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/build/js/search.js

Lines changed: 244 additions & 67 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/package-lock.json

Lines changed: 32787 additions & 14337 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

assets/src/js/search/data.js

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* External dependencies.
33
*/
4-
import { getFiltersFromUrl, getResultMarkup, getUrlWithFilters } from './helpers';
4+
import { getFiltersFromUrl, getLoadMoreMarkup, getResultMarkup, getUrlWithFilters } from './helpers';
55

66
const { persist, create, stores } = window.zustand;
77

@@ -21,6 +21,8 @@ export const DEFAULT_STATE = {
2121
filters: {},
2222
filterIds: [],
2323
pageNo: 1,
24+
resultCount: null,
25+
noOfPages: 0,
2426
resultMarkup: '',
2527
loading: false,
2628
};
@@ -48,11 +50,11 @@ const getStateFromUrl = ( rootUrl = '' ) => {
4850
const { filterKeys } = getState();
4951
const url = new URL( window.location.href );
5052
let data = {};
51-
53+
5254
// Build data from URL.
5355
// Add filters and page no to data.
5456
data = getFiltersFromUrl( url, filterKeys );
55-
57+
5658
// Get url with filter selection.
5759
data.url = getUrlWithFilters( data?.filters ?? {}, rootUrl );
5860

@@ -75,13 +77,13 @@ const setStateFromUrl = ( settings = {}, stateFromUrl = {} ) => {
7577
loading: true,
7678
...stateFromUrl,
7779
} );
78-
79-
// Action: Get results with data from state.
80+
81+
// Action: Get result with data from state.
8082
getResult();
8183
};
8284

8385
/**
84-
* Get Results.
86+
* Get Result.
8587
*/
8688
const getResult = () => {
8789
const { restApiUrl, filters, pageNo } = getState();
@@ -92,19 +94,20 @@ const getResult = () => {
9294
// Add query-params to rest api url.
9395
const params = {
9496
...filters,
95-
page_no: pageNo
97+
page_no: pageNo,
9698
};
9799
const fetchUrl = restApiUrl + '?' + ( new URLSearchParams( params ) ).toString();
98100

99101
fetch( fetchUrl )
100102
.then( ( response ) => response.json() )
101103
.then( ( responseData ) => {
102104
const resultMarkup = getResultMarkup( responseData?.posts ?? [], responseData?.total_posts ?? 0 );
105+
const loadMoreMarkup = getLoadMoreMarkup( responseData?.no_of_pages ?? 0, pageNo );
103106
setState( {
104107
loading: false,
105108
resultCount: responseData?.total_posts ?? 0,
106109
resultPosts: responseData?.posts ?? [],
107-
resultMarkup: resultMarkup || '',
110+
resultMarkup: resultMarkup + loadMoreMarkup || '',
108111
noOfPages: responseData?.no_of_pages ?? 0,
109112
} );
110113
} );
@@ -116,9 +119,8 @@ const getResult = () => {
116119
* @param {Object} currentSelection currentSelection
117120
*/
118121
const addFilter = ( currentSelection = {} ) => {
119-
const { filters, filterKeys, rootUrl } = getState();
122+
const { filters, rootUrl } = getState();
120123
const { key, value } = currentSelection || {};
121-
console.log( 'currentSelection', currentSelection, filters[key] );
122124

123125
// Get new filter values.
124126
let newFilters = { ...filters };
@@ -140,10 +142,11 @@ const addFilter = ( currentSelection = {} ) => {
140142
url,
141143
currentSelection,
142144
filters: newFilters,
145+
pageNo: 1,
143146
loading: true,
144147
} );
145148

146-
// Get Results.
149+
// Get Result.
147150
getResult();
148151
};
149152

@@ -187,6 +190,7 @@ const deleteFilter = ( currentSelection = {} ) => {
187190
url,
188191
currentSelection,
189192
filters: newFilters,
193+
pageNo: 1,
190194
loading: true,
191195
} );
192196

@@ -212,6 +216,46 @@ const updateUrl = ( url ) => {
212216
}
213217
};
214218

219+
const loadMorePosts = ( nextPageNo = 1 ) => {
220+
const { restApiUrl, resultMarkup, filters } = getState();
221+
// Update page no in the fetch url.
222+
const fetchUrl = getUrlWithFilters( { ...filters, page_no: nextPageNo }, restApiUrl );
223+
224+
// Set State.
225+
setState({
226+
loadingMorePosts: true,
227+
pageNo: nextPageNo,
228+
});
229+
230+
// Fetch load more results.
231+
fetch( fetchUrl )
232+
.then( ( response ) => response.json() )
233+
.then( ( responseData ) => {
234+
const moreResultMarkup = getResultMarkup( responseData?.posts ?? [] );
235+
const loadMoreMarkup = getLoadMoreMarkup( responseData?.no_of_pages ?? 0, nextPageNo );
236+
setState( {
237+
loadingMorePosts: false,
238+
resultPosts: responseData?.posts ?? [],
239+
resultMarkup: resultMarkup + moreResultMarkup + loadMoreMarkup,
240+
} );
241+
} );
242+
};
243+
244+
const clearAllFilters = () => {
245+
const { rootUrl } = getState();
246+
setState({
247+
loading: true,
248+
filters: {},
249+
filterIds: [],
250+
currentSelection: {},
251+
pageNo: 1,
252+
});
253+
254+
updateUrl( rootUrl );
255+
256+
getResult();
257+
};
258+
215259
/**
216260
* Create store.
217261
*/
@@ -222,6 +266,8 @@ export const store = create(
222266
initialize,
223267
addFilter,
224268
deleteFilter,
269+
loadMorePosts,
270+
clearAllFilters,
225271
} ),
226272
{
227273
name: STORE_NAME,

assets/src/js/search/helpers.js

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable prettier/prettier */
12
/**
23
* Get Filters From Url.
34
*
@@ -58,14 +59,6 @@ export const getUrlWithFilters = ( filters = {} = {}, rootUrl = '' ) => {
5859
// Build URL.
5960
let url = new URL( rootUrl );
6061

61-
// Remove the page parameter whenever any filter value is changed. (Pages should start over from Page 1)
62-
url.searchParams.delete( 'pageNo' );
63-
64-
// 1.Add page number if it does not already exists and at least one filter is available.
65-
if ( ! url.searchParams.has( 'pageNo' ) && Object.keys( filters ).length ) {
66-
url.searchParams.append( 'pageNo', '1' );
67-
}
68-
6962
// 2.Add the given keys value pairs in search params.
7063
Object.keys( filters ).forEach( ( key ) => {
7164
url.searchParams.set( key, filters[ key ] );
@@ -81,26 +74,24 @@ export const getUrlWithFilters = ( filters = {} = {}, rootUrl = '' ) => {
8174
* Get Results markup.
8275
*
8376
* @param posts
84-
* @param totalPosts
8577
* @return {string}
8678
*/
87-
export const getResultMarkup = ( posts = [], totalPosts = 0 ) => {
79+
export const getResultMarkup = ( posts = [] ) => {
8880
if ( ! Array.isArray( posts ) || ! posts.length ) {
8981
return '';
9082
}
9183

92-
let markup = `
93-
<div>
94-
<h5>Results: ${totalPosts}</h5>
95-
<div class="row">`;
84+
let img = '';
85+
let markup = ``;
9686

9787
posts.forEach( post => {
88+
img = post.thumbnail ? post.thumbnail : '<img src="https://via.placeholder.com/526x300" width="526" height="300"/>';
9889
markup += `
9990
<section id="post-${ post?.id ?? 0 }" class="col-lg-4 col-md-6 col-sm-12 pb-4">
10091
<header>
10192
<a href="${ post?.permalink ?? '' }" class="block">
10293
<figure class="img-container">
103-
${ post?.thumbnail ?? '' }
94+
${ img }
10495
</figure>
10596
</header>
10697
<div class="post-excerpt my-4">
@@ -118,9 +109,18 @@ export const getResultMarkup = ( posts = [], totalPosts = 0 ) => {
118109
`;
119110
} );
120111

121-
markup += `
122-
</div>
123-
</div>`;
124-
125112
return markup;
113+
};
114+
115+
export const getLoadMoreMarkup = ( noOfPages = 0, currentPageNo = 1 ) => {
116+
if ( parseInt( currentPageNo ) >= parseInt( noOfPages ) ) {
117+
return '';
118+
}
119+
120+
return `<aquila-load-more
121+
class="load-more-wrap"
122+
next-page-no="${ parseInt( currentPageNo ) + 1 }"
123+
>
124+
<button class="btn btn-primary">Load More</button>
125+
</aquila-load-more>`;
126126
}

assets/src/js/search/index.js

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@ class AquilaSearch extends HTMLElement {
2727
}
2828
}
2929

30+
/**
31+
* Clear All Filters.
32+
*/
33+
class AquilaClearAllFilters extends HTMLElement {
34+
/**
35+
* Constructor.
36+
*/
37+
constructor() {
38+
super();
39+
40+
const { clearAllFilters } = getState();
41+
this.clearAllFiltersButton = this.querySelector( 'button' );
42+
43+
this.clearAllFiltersButton.addEventListener( 'click', () => {
44+
clearAllFilters();
45+
} );
46+
}
47+
}
48+
3049
/**
3150
* AquilaFilters Class.
3251
*/
@@ -138,6 +157,9 @@ class AquilaCheckboxAccordionChild extends HTMLElement {
138157
if ( this.parentContentEl.style ) {
139158
this.parentContentEl.style.height = 'auto';
140159
}
160+
} else {
161+
this.inputEl.checked = false;
162+
this.parentEl.removeAttribute( 'active' );
141163
}
142164
}
143165

@@ -165,6 +187,28 @@ class AquilaCheckboxAccordionChild extends HTMLElement {
165187
}
166188
}
167189

190+
/**
191+
* AquilaResults Class.
192+
*/
193+
class AquilaResultsCount extends HTMLElement {
194+
/**
195+
* Constructor.
196+
*/
197+
constructor() {
198+
super();
199+
200+
// Subscribe to updates.
201+
subscribe( this.update.bind( this ) );
202+
}
203+
204+
update( currentState = {} ) {
205+
const { resultCount } = currentState;
206+
if ( null !== resultCount ) {
207+
this.innerHTML = `Results: ${resultCount} Posts`;
208+
}
209+
}
210+
}
211+
168212
/**
169213
* AquilaResults Class.
170214
*/
@@ -195,11 +239,64 @@ class AquilaResults extends HTMLElement {
195239
}
196240
}
197241

242+
/**
243+
* AquilaLoadMore Class.
244+
*/
245+
class AquilaLoadMore extends HTMLElement {
246+
/**
247+
* Constructor.
248+
*/
249+
constructor() {
250+
super();
251+
252+
// Subscribe to updates.
253+
subscribe( this.update.bind( this ) );
254+
255+
this.querySelector( 'button' ).addEventListener( 'click', () => this.handleLoadMoreButtonClick() );
256+
this.nextPageNo = this.getAttribute( 'next-page-no' );
257+
}
258+
259+
update( currentState = {} ) {
260+
const { pageNo } = currentState;
261+
if ( parseInt( this.nextPageNo ) <= parseInt( pageNo ) ) {
262+
this.remove();
263+
return null;
264+
}
265+
266+
}
267+
268+
handleLoadMoreButtonClick() {
269+
const { loadMorePosts } = getState();
270+
loadMorePosts( this.nextPageNo );
271+
}
272+
}
273+
274+
class AquilaLoadingMore extends HTMLElement {
275+
constructor() {
276+
super();
277+
// Subscribe to updates.
278+
subscribe( this.update.bind( this ) );
279+
}
280+
281+
update( currentState = {} ) {
282+
const { loadingMorePosts } = currentState;
283+
if ( loadingMorePosts ) {
284+
this.innerHTML = 'Loading more posts...';
285+
} else {
286+
this.innerHTML = '';
287+
}
288+
}
289+
}
290+
198291
/**
199292
* Initialize.
200293
*/
201294
customElements.define( 'aquila-checkbox-accordion', AquilaCheckboxAccordion );
202295
customElements.define( 'aquila-checkbox-accordion-child', AquilaCheckboxAccordionChild );
296+
customElements.define( 'aquila-clear-all-filters', AquilaClearAllFilters );
203297
customElements.define( 'aquila-filters', AquilaFilters );
298+
customElements.define( 'aquila-results-count', AquilaResultsCount );
204299
customElements.define( 'aquila-results', AquilaResults );
300+
customElements.define( 'aquila-load-more', AquilaLoadMore );
301+
customElements.define( 'aquila-loading-more', AquilaLoadingMore );
205302
customElements.define( 'aquila-search', AquilaSearch );

0 commit comments

Comments
 (0)