diff --git a/client/homebrew/pages/archivePage/archivePage.jsx b/client/homebrew/pages/archivePage/archivePage.jsx index b2279a8fa..aae1b6631 100644 --- a/client/homebrew/pages/archivePage/archivePage.jsx +++ b/client/homebrew/pages/archivePage/archivePage.jsx @@ -16,182 +16,186 @@ const BrewItem = require('../basePages/listPage/brewItem/brewItem.jsx'); const request = require('superagent'); const ArchivePage = createClass({ - displayName: 'ArchivePage', - getDefaultProps: function () { - return {}; - }, - getInitialState: function () { - return { - title : this.props.query.title || '', - brewCollection : null, - page : 1, - totalPages : 1, - searching : false, - error : null, - }; - }, - componentDidMount : function() { + displayName : 'ArchivePage', + getDefaultProps : function () { + return {}; + }, + getInitialState : function () { + return { + title : this.props.query.title || '', + brewCollection : null, + page : 1, + totalPages : 1, + searching : false, + error : null, + }; + }, + componentDidMount : function() { - }, - handleChange(e) { - this.setState({ title: e.target.value }); - }, + }, + handleChange(e) { + this.setState({ title: e.target.value }); + }, - updateStateWithBrews : (brews, page, totalPages) => { - this.setState({ - brewCollection: brews, - page: page, - totalPages: totalPages, - searching: false - }); - }, - loadPage: async function(page) { - if(this.state.title == '') {} else { + updateStateWithBrews : (brews, page, totalPages)=>{ + this.setState({ + brewCollection : brews, + page : page, + totalPages : totalPages, + searching : false + }); + }, + loadPage : async function(page) { + if(this.state.title == '') {} else { - try { - //this.updateUrl(); - this.setState({ searching: true, error: null }); - const title = encodeURIComponent(this.state.title); - 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 (

Whenever you want, just start typing...

);} - - if (error !== null) { return ( -
-

I'm sorry, your request didn't work

-

Your search is not specific enough. Too many brews meet this criteria for us to display them.

-
- );} - - if (!brewCollection || brewCollection.length === 0) { return ( -
-

We haven't found brews meeting your request.

-
- );} - - return ( -
- {`Brews Found: ${brewCollection.length}`} - - {brewCollection.map((brew, index) => ( - - ))} -
- {page > 1 && ( - - )} - Page {page} - {page < totalPages && ( - - )} -
-
- ); - }, + try { + //this.updateUrl(); + this.setState({ searching: true, error: null }); + const title = encodeURIComponent(this.state.title); + const response = await fetch(`/archive?title=${title}&page=${page}`); - renderForm: function () { - return ( -
-

Brew Lookup

- - { - 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. + 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 (

Whenever you want, just start typing...

);} + + if(error !== null) { + return ( +
+

I'm sorry, your request didn't work

+

Your search is not specific enough. Too many brews meet this criteria for us to display them.

+
+ ); + } + + if(!brewCollection || brewCollection.length === 0) { + return ( +
+

We haven't found brews meeting your request.

+
+ ); + } + + return ( +
+ {`Brews Found: ${brewCollection.length}`} + + {brewCollection.map((brew, index)=>( + + ))} +
+ {page > 1 && ( + + )} + Page {page} + {page < totalPages && ( + + )} +
+
+ ); + }, + + + renderForm : function () { + return ( +
+

Brew Lookup

+ + { + 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. */} - - -
- ); - }, - + +
+ ); + }, - renderNavItems: function () { - return ( - - + + + renderNavItems : function () { + return ( + + Archive: Search for brews - - - - - - - - ); - }, + + + + + + + + ); + }, - render: function () { - return ( -
- + render : function () { + return ( +
+ - {this.renderNavItems()} + {this.renderNavItems()} -
-
-

Welcome to the Archive

-
-
-
{this.renderForm()}
-
-
-

Your results, my lordship

-
- {this.renderFoundBrews()} -
-
-
-
- ); - }, +
+
+

Welcome to the Archive

+
+
+
{this.renderForm()}
+
+
+

Your results, my lordship

+
+ {this.renderFoundBrews()} +
+
+
+
+ ); + }, }); module.exports = ArchivePage; diff --git a/server/archive.api.js b/server/archive.api.js index 4166bf80a..d959b27b8 100644 --- a/server/archive.api.js +++ b/server/archive.api.js @@ -3,49 +3,49 @@ const router = require('express').Router(); const asyncHandler = require('express-async-handler'); const archive = { - archiveApi: router, - /* Searches for matching title, also attempts to partial match */ - findBrews: async (req, res, next) => { - try { - const title = req.query.title || ''; - const page = parseInt(req.query.page) || 1; - console.log('try:',page); - const pageSize = 10; // Set a default page size - const skip = (page - 1) * pageSize; + archiveApi : router, + /* Searches for matching title, also attempts to partial match */ + findBrews : async (req, res, next)=>{ + try { + const title = req.query.title || ''; + const page = parseInt(req.query.page) || 1; + console.log('try:', page); + const pageSize = 10; // Set a default page size + const skip = (page - 1) * pageSize; - const titleQuery = { - title: { $regex: decodeURIComponent(title), $options: 'i' }, - published: true - }; + const titleQuery = { + title : { $regex: decodeURIComponent(title), $options: 'i' }, + published : true + }; - const projection = { - editId: 0, - googleId: 0, - text: 0, - textBin: 0, - }; + const projection = { + editId : 0, + googleId : 0, + text : 0, + textBin : 0, + }; - const brews = await HomebrewModel.find(titleQuery, projection) + const brews = await HomebrewModel.find(titleQuery, projection) .skip(skip) .limit(pageSize) .maxTimeMS(5000) .exec(); - if (!brews || brews.length === 0) { - // No published documents found with the given title - return res.status(404).json({ error: 'Published documents not found' }); - } + if(!brews || brews.length === 0) { + // No published documents found with the given title + 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}); - } catch (error) { - console.error(error); - return res.status(500).json({ error: 'Internal Server Error' }); - } - } + return res.json({ brews, page, totalPages }); + } catch (error) { + console.error(error); + return res.status(500).json({ error: 'Internal Server Error' }); + } + } }; router.get('/archive', asyncHandler(archive.findBrews));