0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2026-01-06 05:42:40 +00:00

pagination controls don't reset search

This commit is contained in:
Víctor Losada Hernández
2024-02-15 13:45:31 +01:00
parent ffdbe46a23
commit 2658831e83
2 changed files with 79 additions and 36 deletions

View File

@@ -26,26 +26,22 @@ const ArchivePage = createClass({
//request //request
title : this.props.query.title || '', title : this.props.query.title || '',
//tags : {}, //tags : {},
legacy : true, legacy : `${this.props.query.legacy === 'false' ? false : true }`,
v3 : true, v3 : `${this.props.query.v3 === 'false' ? false : true }`,
pageSize : 10, pageSize : this.props.query.size || 10,
page : 1, page : this.props.query.page || 1,
//response //response
brewCollection : null, brewCollection : null,
totalPages : null, totalPages : null,
totalBrews : null, totalBrews : null,
searching : false, searching : false,
error : null, error : null,
}; };
}, },
componentDidMount : function() { componentDidMount : function() {
},
handleChange(inputName, e) {
this.setState({ [inputName]: e.target.value });
}, },
updateStateWithBrews : function (brews, page, totalPages, totalBrews) { updateStateWithBrews : function (brews, page, totalPages, totalBrews) {
@@ -58,8 +54,22 @@ const ArchivePage = createClass({
}); });
}, },
loadPage : async function(page) { updateStateWithForm : function() {
this.updateUrl(); this.setState({
title: document.getElementById( 'title' ).value || "",
pageSize: document.getElementById( 'size' ).value || 10,
v3: document.getElementById('v3').checked,
legacy: document.getElementById('legacy').checked,
});
},
loadPage : async function(page, update) {
console.log(update);
if(update === true) {
this.updateStateWithForm();
this.updateUrl();
};
try { try {
this.setState({ searching: true, error: null }); this.setState({ searching: true, error: null });
@@ -75,7 +85,7 @@ const ArchivePage = createClass({
} catch (error) { } catch (error) {
console.log(`LoadPage error: ${error}`); console.log(`LoadPage error: ${error}`);
this.setState({ error: `${error}` }); this.setState({ error: `${error}` });
this.updateStateWithBrews([], 1, 1); this.updateStateWithBrews([], 1, 1, 0);
} }
}, },
@@ -95,9 +105,9 @@ const ArchivePage = createClass({
}, },
renderPaginationControls() { renderPaginationControls() {
const { page, totalPages,} = this.state; const { page, totalPages} = this.state;
const pages = new Array(totalPages).fill().map((_, index) => ( const pages = new Array(totalPages).fill().map((_, index) => (
<li key={index} className={`pageNumber ${page === index + 1 ? 'currentPage' : ''}`} onClick={() => this.loadPage(index+1)}>{index + 1}</li> <li key={index} className={`pageNumber ${page === index + 1 ? 'currentPage' : ''}`} onClick={() => this.loadPage(index + 1, false)}>{index + 1}</li>
)); ));
if(totalPages === null) {return;} if(totalPages === null) {return;}
@@ -107,14 +117,14 @@ const ArchivePage = createClass({
{page > 1 && ( {page > 1 && (
<button <button
className="previousPage" className="previousPage"
onClick={() => this.loadPage(page - 1)} onClick={() => this.loadPage(page - 1, false)}
> >
&lt;&lt; &lt;&lt;
</button> </button>
)} )}
<ol className='pages'>{pages}</ol> <ol className='pages'>{pages}</ol>
{page < totalPages && ( {page < totalPages && (
<button className="nextPage" onClick={() => this.loadPage(page + 1)}> <button className="nextPage" onClick={() => this.loadPage(page + 1, false)}>
&gt;&gt; &gt;&gt;
</button> </button>
)} )}
@@ -123,26 +133,34 @@ const ArchivePage = createClass({
}, },
renderFoundBrews() { renderFoundBrews() {
const { title, brewCollection, page, totalPages, error } = this.state; const { title, brewCollection, page, totalPages, error, searching } = this.state;
console.log(searching === false && !brewCollection);
if(searching === false && title === '' && error === null) {return (<div className='foundBrews noBrews'><h3>Whenever you want, just start typing...</h3></div>);}
if(title === '' && error === null) {return (<div className='foundBrews noBrews'><h3>Whenever you want, just start typing...</h3></div>);} if(searching === false && error === 'Error: Service Unavailable') {
if(error === 'Error: Service Unavailable') {
return ( return (
<div className='foundBrews noBrews'> <div className='foundBrews noBrews'>
<div><h3>I'm sorry, your request didn't work</h3> <div><h3>I'm sorry, your request didn't work</h3>
<br /><p>Your search is not specific enough. Too many brews meet this criteria for us to display them.</p> <br /><p>Your search is not specific enough. Too many brews meet this criteria for us to display them.</p>
</div></div> </div></div>
); );
} };
if(!brewCollection || brewCollection.length === 0 || error === 'Error: Not found') { if(searching === false && (!brewCollection || error === 'Error: Not found')) {
return ( return (
<div className='foundBrews noBrews'> <div className='foundBrews noBrews'>
<h3>We haven't found brews meeting your request.</h3> <h3>We haven't found brews meeting your request.</h3>
</div> </div>
); );
} };
if(searching === true) {
return (
<div className='foundBrews searching'>
<span><h3>Searching</h3><h3 className='searchAnim'>...</h3></span>
</div>
);
};
return ( return (
<div className='foundBrews'> <div className='foundBrews'>
@@ -157,6 +175,9 @@ const ArchivePage = createClass({
renderForm : function () { renderForm : function () {
const v3 = (this.state.v3 === 'true');
const legacy = (this.state.legacy === 'true');
return ( return (
<div className='brewLookup'> <div className='brewLookup'>
<h2 className='formTitle'>Brew Lookup</h2> <h2 className='formTitle'>Brew Lookup</h2>
@@ -164,14 +185,13 @@ const ArchivePage = createClass({
<label> <label>
Title of the brew Title of the brew
<input <input
id='title'
type='text' type='text'
name='title' name='title'
value={this.state.title} defaultValue={this.state.title}
onChange={(e) => this.handleChange('title', e)}
onKeyDown={(e) => { onKeyDown={(e) => {
if (e.key === 'Enter') { if (e.key === 'Enter') {
this.handleChange('title', e); this.loadPage(1, true);
this.loadPage(1);
} }
}} }}
placeholder='v3 Reference Document' placeholder='v3 Reference Document'
@@ -181,27 +201,27 @@ const ArchivePage = createClass({
<label> <label>
Results per page Results per page
<input <input
id='size'
type="number" type="number"
min="6"step="2"max="30" min="6"step="2"max="30"
name="pageSize" name="pageSize"
onChange={(e) => this.handleChange('pageSize', e)}
/> />
</label> </label>
<label> <label>
<input <input
id='v3'
type="checkbox" type="checkbox"
onChange={()=>{this.setState({v3: !this.state.v3})}} defaultChecked={v3}
checked={this.state.v3}
/> />
Search for v3 brews Search for v3 brews
</label> </label>
<label> <label>
<input <input
id='legacy'
type="checkbox" type="checkbox"
onChange={()=>{this.setState({legacy: !this.state.legacy})}} defaultChecked={legacy}
checked={this.state.legacy}
/> />
Search for legacy brews Search for legacy brews
</label> </label>
@@ -218,9 +238,8 @@ const ArchivePage = createClass({
<button <button
className='search' className='search'
onClick={()=>{ onClick={()=>{
this.handleChange('title', { target: { value: this.state.title } }); this.loadPage(1, true);
this.loadPage(1);
}}> }}>
Search Search
<i <i

View File

@@ -117,6 +117,16 @@ body {
color: white; color: white;
} }
&.searching {
display: grid;
place-items: center;
color: white;
h3.searchAnim {
animation: trailingDots 2s ease infinite;
}
}
.totalBrews { .totalBrews {
position: fixed; position: fixed;
bottom: 0; bottom: 0;
@@ -215,3 +225,17 @@ body {
} }
} }
} }
@keyframes trailingDots {
0%,32% {
clip-path:inset(0 66% 0 0);
}
33%, 65% {
clip-path:inset(0 33% 0 0);
}
66%,100% {
clip-path:inset(0 0% 0 0);
}
}