mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-30 15:22:41 +00:00
Lint clean up
This commit is contained in:
@@ -16,182 +16,186 @@ const BrewItem = require('../basePages/listPage/brewItem/brewItem.jsx');
|
|||||||
const request = require('superagent');
|
const request = require('superagent');
|
||||||
|
|
||||||
const ArchivePage = createClass({
|
const ArchivePage = createClass({
|
||||||
displayName: 'ArchivePage',
|
displayName : 'ArchivePage',
|
||||||
getDefaultProps: function () {
|
getDefaultProps : function () {
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
getInitialState: function () {
|
getInitialState : function () {
|
||||||
return {
|
return {
|
||||||
title : this.props.query.title || '',
|
title : this.props.query.title || '',
|
||||||
brewCollection : null,
|
brewCollection : null,
|
||||||
page : 1,
|
page : 1,
|
||||||
totalPages : 1,
|
totalPages : 1,
|
||||||
searching : false,
|
searching : false,
|
||||||
error : null,
|
error : null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
componentDidMount : function() {
|
componentDidMount : function() {
|
||||||
|
|
||||||
},
|
},
|
||||||
handleChange(e) {
|
handleChange(e) {
|
||||||
this.setState({ title: e.target.value });
|
this.setState({ title: e.target.value });
|
||||||
},
|
},
|
||||||
|
|
||||||
updateStateWithBrews : (brews, page, totalPages) => {
|
updateStateWithBrews : (brews, page, totalPages)=>{
|
||||||
this.setState({
|
this.setState({
|
||||||
brewCollection: brews,
|
brewCollection : brews,
|
||||||
page: page,
|
page : page,
|
||||||
totalPages: totalPages,
|
totalPages : totalPages,
|
||||||
searching: false
|
searching : false
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
loadPage: async function(page) {
|
loadPage : async function(page) {
|
||||||
if(this.state.title == '') {} else {
|
if(this.state.title == '') {} else {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
//this.updateUrl();
|
//this.updateUrl();
|
||||||
this.setState({ searching: true, error: null });
|
this.setState({ searching: true, error: null });
|
||||||
const title = encodeURIComponent(this.state.title);
|
const title = encodeURIComponent(this.state.title);
|
||||||
const response = await fetch(`/archive?title=${title}&page=${page}`);
|
const response = await fetch(`/archive?title=${title}&page=${page}`);
|
||||||
|
|
||||||
|
|
||||||
if (response.ok) {
|
|
||||||
const res = await response.json();
|
|
||||||
this.updateStateWithBrews(res.brews, page, res.totalPages);
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (error) {
|
|
||||||
console.log("LoadPage error: " + error);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
updateUrl: function() {
|
|
||||||
const url = new URL(window.location.href);
|
|
||||||
const urlParams = new URLSearchParams(url.search);
|
|
||||||
|
|
||||||
// Set the title and page parameters
|
|
||||||
urlParams.set('title', this.state.title);
|
|
||||||
urlParams.set('page', this.state.page);
|
|
||||||
|
|
||||||
url.search = urlParams.toString(); // Convert URLSearchParams to string
|
|
||||||
window.history.replaceState(null, null, url);
|
|
||||||
},
|
|
||||||
|
|
||||||
renderFoundBrews() {
|
|
||||||
const { title, brewCollection, page, totalPages, error } = this.state;
|
|
||||||
|
|
||||||
if (title === '') {return (<div className="foundBrews noBrews"><h3>Whenever you want, just start typing...</h3></div>);}
|
|
||||||
|
|
||||||
if (error !== null) { return (
|
|
||||||
<div className="foundBrews noBrews">
|
|
||||||
<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>
|
|
||||||
</div></div>
|
|
||||||
);}
|
|
||||||
|
|
||||||
if (!brewCollection || brewCollection.length === 0) { return (
|
|
||||||
<div className="foundBrews noBrews">
|
|
||||||
<h3>We haven't found brews meeting your request.</h3>
|
|
||||||
</div>
|
|
||||||
);}
|
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="foundBrews">
|
|
||||||
<span className="brewCount">{`Brews Found: ${brewCollection.length}`}</span>
|
|
||||||
|
|
||||||
{brewCollection.map((brew, index) => (
|
|
||||||
<BrewItem brew={brew} key={index} reportError={this.props.reportError} />
|
|
||||||
))}
|
|
||||||
<div className="paginationControls">
|
|
||||||
{page > 1 && (
|
|
||||||
<button onClick={() => this.loadPage(page - 1)}>Previous Page</button>
|
|
||||||
)}
|
|
||||||
<span className="currentPage">Page {page}</span>
|
|
||||||
{page < totalPages && (
|
|
||||||
<button onClick={() => this.loadPage(page + 1)}>Next Page</button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
|
|
||||||
renderForm: function () {
|
if(response.ok) {
|
||||||
return (
|
const res = await response.json();
|
||||||
<div className='brewLookup'>
|
this.updateStateWithBrews(res.brews, page, res.totalPages);
|
||||||
<h2>Brew Lookup</h2>
|
}
|
||||||
<label>Title of the brew</label>
|
|
||||||
<input
|
} catch (error) {
|
||||||
type='text'
|
console.log(`LoadPage error: ${error}`);
|
||||||
value={this.state.title}
|
}
|
||||||
onChange={this.handleChange}
|
}
|
||||||
onKeyDown={(e) => {
|
},
|
||||||
if (e.key === 'Enter') {
|
updateUrl : function() {
|
||||||
this.handleChange(e);
|
const url = new URL(window.location.href);
|
||||||
this.loadPage(1);
|
const urlParams = new URLSearchParams(url.search);
|
||||||
}
|
|
||||||
}}
|
// Set the title and page parameters
|
||||||
placeholder='v3 Reference Document'
|
urlParams.set('title', this.state.title);
|
||||||
/>
|
urlParams.set('page', this.state.page);
|
||||||
{/* In the future, we should be able to filter the results by adding tags.
|
|
||||||
|
url.search = urlParams.toString(); // Convert URLSearchParams to string
|
||||||
|
window.history.replaceState(null, null, url);
|
||||||
|
},
|
||||||
|
|
||||||
|
renderFoundBrews() {
|
||||||
|
const { title, brewCollection, page, totalPages, error } = this.state;
|
||||||
|
|
||||||
|
if(title === '') {return (<div className='foundBrews noBrews'><h3>Whenever you want, just start typing...</h3></div>);}
|
||||||
|
|
||||||
|
if(error !== null) {
|
||||||
|
return (
|
||||||
|
<div className='foundBrews noBrews'>
|
||||||
|
<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>
|
||||||
|
</div></div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!brewCollection || brewCollection.length === 0) {
|
||||||
|
return (
|
||||||
|
<div className='foundBrews noBrews'>
|
||||||
|
<h3>We haven't found brews meeting your request.</h3>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='foundBrews'>
|
||||||
|
<span className='brewCount'>{`Brews Found: ${brewCollection.length}`}</span>
|
||||||
|
|
||||||
|
{brewCollection.map((brew, index)=>(
|
||||||
|
<BrewItem brew={brew} key={index} reportError={this.props.reportError} />
|
||||||
|
))}
|
||||||
|
<div className='paginationControls'>
|
||||||
|
{page > 1 && (
|
||||||
|
<button onClick={()=>this.loadPage(page - 1)}>Previous Page</button>
|
||||||
|
)}
|
||||||
|
<span className='currentPage'>Page {page}</span>
|
||||||
|
{page < totalPages && (
|
||||||
|
<button onClick={()=>this.loadPage(page + 1)}>Next Page</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
|
|
||||||
|
renderForm : function () {
|
||||||
|
return (
|
||||||
|
<div className='brewLookup'>
|
||||||
|
<h2>Brew Lookup</h2>
|
||||||
|
<label>Title of the brew</label>
|
||||||
|
<input
|
||||||
|
type='text'
|
||||||
|
value={this.state.title}
|
||||||
|
onChange={this.handleChange}
|
||||||
|
onKeyDown={(e)=>{
|
||||||
|
if(e.key === 'Enter') {
|
||||||
|
this.handleChange(e);
|
||||||
|
this.loadPage(1);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
placeholder='v3 Reference Document'
|
||||||
|
/>
|
||||||
|
{/* In the future, we should be able to filter the results by adding tags.
|
||||||
<label>Tags</label><input type='text' value={this.state.query} placeholder='add a tag to filter'/>
|
<label>Tags</label><input type='text' value={this.state.query} placeholder='add a tag to filter'/>
|
||||||
<input type="checkbox" id="v3" /><label>v3 only</label>
|
<input type="checkbox" id="v3" /><label>v3 only</label>
|
||||||
*/}
|
*/}
|
||||||
|
|
||||||
<button onClick={() => { this.handleChange({ target: { value: this.state.title } }); this.loadPage(1); }}>
|
|
||||||
<i
|
|
||||||
className={cx('fas', {
|
|
||||||
'fa-search': !this.state.searching,
|
|
||||||
'fa-spin fa-spinner': this.state.searching,
|
|
||||||
})}
|
|
||||||
/>
|
|
||||||
</button>
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
|
||||||
|
|
||||||
|
<button onClick={()=>{ this.handleChange({ target: { value: this.state.title } }); this.loadPage(1); }}>
|
||||||
|
<i
|
||||||
|
className={cx('fas', {
|
||||||
|
'fa-search' : !this.state.searching,
|
||||||
|
'fa-spin fa-spinner' : this.state.searching,
|
||||||
|
})}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
|
||||||
renderNavItems: function () {
|
|
||||||
return (
|
|
||||||
<Navbar>
|
renderNavItems : function () {
|
||||||
<Nav.section>
|
return (
|
||||||
|
<Navbar>
|
||||||
|
<Nav.section>
|
||||||
<Nav.item className='brewTitle'>Archive: Search for brews</Nav.item>
|
<Nav.item className='brewTitle'>Archive: Search for brews</Nav.item>
|
||||||
</Nav.section>
|
</Nav.section>
|
||||||
<Nav.section>
|
<Nav.section>
|
||||||
<NewBrew />
|
<NewBrew />
|
||||||
<HelpNavItem />
|
<HelpNavItem />
|
||||||
<RecentNavItem />
|
<RecentNavItem />
|
||||||
<Account />
|
<Account />
|
||||||
</Nav.section>
|
</Nav.section>
|
||||||
</Navbar>
|
</Navbar>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
||||||
render: function () {
|
render : function () {
|
||||||
return (
|
return (
|
||||||
<div className='archivePage'>
|
<div className='archivePage'>
|
||||||
<link href='/themes/V3/Blank/style.css' rel='stylesheet'/>
|
<link href='/themes/V3/Blank/style.css' rel='stylesheet'/>
|
||||||
<link href='/themes/V3/5ePHB/style.css' rel='stylesheet'/>
|
<link href='/themes/V3/5ePHB/style.css' rel='stylesheet'/>
|
||||||
{this.renderNavItems()}
|
{this.renderNavItems()}
|
||||||
|
|
||||||
<div className='content'>
|
<div className='content'>
|
||||||
<div className='welcome'>
|
<div className='welcome'>
|
||||||
<h1>Welcome to the Archive</h1>
|
<h1>Welcome to the Archive</h1>
|
||||||
</div>
|
</div>
|
||||||
<div className='flexGroup'>
|
<div className='flexGroup'>
|
||||||
<div className='form dataGroup'>{this.renderForm()}</div>
|
<div className='form dataGroup'>{this.renderForm()}</div>
|
||||||
<div className='resultsContainer dataGroup'>
|
<div className='resultsContainer dataGroup'>
|
||||||
<div className='title'>
|
<div className='title'>
|
||||||
<h2>Your results, my lordship</h2>
|
<h2>Your results, my lordship</h2>
|
||||||
</div>
|
</div>
|
||||||
{this.renderFoundBrews()}
|
{this.renderFoundBrews()}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
module.exports = ArchivePage;
|
module.exports = ArchivePage;
|
||||||
|
|||||||
@@ -3,49 +3,49 @@ const router = require('express').Router();
|
|||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
|
|
||||||
const archive = {
|
const archive = {
|
||||||
archiveApi: router,
|
archiveApi : router,
|
||||||
/* Searches for matching title, also attempts to partial match */
|
/* Searches for matching title, also attempts to partial match */
|
||||||
findBrews: async (req, res, next) => {
|
findBrews : async (req, res, next)=>{
|
||||||
try {
|
try {
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
const page = parseInt(req.query.page) || 1;
|
const page = parseInt(req.query.page) || 1;
|
||||||
console.log('try:',page);
|
console.log('try:', page);
|
||||||
const pageSize = 10; // Set a default page size
|
const pageSize = 10; // Set a default page size
|
||||||
const skip = (page - 1) * pageSize;
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
const titleQuery = {
|
const titleQuery = {
|
||||||
title: { $regex: decodeURIComponent(title), $options: 'i' },
|
title : { $regex: decodeURIComponent(title), $options: 'i' },
|
||||||
published: true
|
published : true
|
||||||
};
|
};
|
||||||
|
|
||||||
const projection = {
|
const projection = {
|
||||||
editId: 0,
|
editId : 0,
|
||||||
googleId: 0,
|
googleId : 0,
|
||||||
text: 0,
|
text : 0,
|
||||||
textBin: 0,
|
textBin : 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
const brews = await HomebrewModel.find(titleQuery, projection)
|
const brews = await HomebrewModel.find(titleQuery, projection)
|
||||||
.skip(skip)
|
.skip(skip)
|
||||||
.limit(pageSize)
|
.limit(pageSize)
|
||||||
.maxTimeMS(5000)
|
.maxTimeMS(5000)
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
if (!brews || brews.length === 0) {
|
if(!brews || brews.length === 0) {
|
||||||
// No published documents found with the given title
|
// No published documents found with the given title
|
||||||
return res.status(404).json({ error: 'Published documents not found' });
|
return res.status(404).json({ error: 'Published documents not found' });
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalDocuments = await HomebrewModel.countDocuments(title);
|
const totalDocuments = await HomebrewModel.countDocuments(title);
|
||||||
|
|
||||||
const totalPages = Math.ceil(totalDocuments / pageSize);
|
const totalPages = Math.ceil(totalDocuments / pageSize);
|
||||||
|
|
||||||
return res.json({ brews, page, totalPages});
|
return res.json({ brews, page, totalPages });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
return res.status(500).json({ error: 'Internal Server Error' });
|
return res.status(500).json({ error: 'Internal Server Error' });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
router.get('/archive', asyncHandler(archive.findBrews));
|
router.get('/archive', asyncHandler(archive.findBrews));
|
||||||
|
|||||||
Reference in New Issue
Block a user