From a89b575b267beccaaaa07cbd53d8962d7f7f2715 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Losada=20Hern=C3=A1ndez?= Date: Mon, 13 May 2024 13:37:53 +0200 Subject: [PATCH] archive 2.0 --- .../pages/archivePage/archivePage.jsx | 168 ++++++++++-------- server/admin.api.js | 2 - server/archive.api.js | 94 ++++++++-- themes/themes.json | 4 +- 4 files changed, 183 insertions(+), 85 deletions(-) diff --git a/client/homebrew/pages/archivePage/archivePage.jsx b/client/homebrew/pages/archivePage/archivePage.jsx index eb8e78cc7..71e47f965 100644 --- a/client/homebrew/pages/archivePage/archivePage.jsx +++ b/client/homebrew/pages/archivePage/archivePage.jsx @@ -21,37 +21,37 @@ const ArchivePage = createClass({ getDefaultProps: function () { return {}; }, + getInitialState: function () { return { - //request + //# request title: this.props.query.title || '', - //tags : {}, + //tags: {}, legacy: `${this.props.query.legacy === 'false' ? false : true}`, v3: `${this.props.query.v3 === 'false' ? false : true}`, pageSize: this.props.query.size || 10, page: parseInt(this.props.query.page) || 1, - //response + //# response brewCollection: null, - totalPages: null, totalBrews: null, searching: false, error: null, }; }, + componentDidMount: function () { if (this.state.title !== '') { this.loadPage(this.state.page, false); } + this.state.totalBrews || this.loadTotal(); // Load total if not already loaded }, - updateStateWithBrews: function (brews, page, totalPages, totalBrews) { + updateStateWithBrews: function (brews, page) { this.setState({ brewCollection: brews || null, page: parseInt(page) || 1, - totalPages: totalPages || 1, - totalBrews: totalBrews, searching: false, }); }, @@ -78,6 +78,7 @@ const ArchivePage = createClass({ }, loadPage: async function (page, update) { + console.log('running loadPage'); //load form data directly const title = document.getElementById('title').value || ''; const size = document.getElementById('size').value || 10; @@ -96,46 +97,58 @@ const ArchivePage = createClass({ } if (title !== '') { - console.log('pagesize when querying: ', this.state.pageSize); try { this.setState({ searching: true, error: null }); - await request - .get( - `/api/archive?title=${title}&page=${page}&size=${size}&v3=${v3}&legacy=${legacy}` - ) - .then((response) => { - if (response.ok) { - this.updateStateWithBrews( - response.body.brews, - page, - response.body.totalPages, - response.body.totalBrews - ); - } - }); + await request.get( + `/api/archive?title=${title}&page=${page}&size=${size}&v3=${v3}&legacy=${legacy}` + ).then((response) => { + if (response.ok) { + this.updateStateWithBrews( + response.body.brews, + page + ); + } + }); } catch (error) { + console.log('error at loadPage: ', error); this.setState({ error: `${error.response.status}` }); - this.updateStateWithBrews([], 1, 1, 0); + this.updateStateWithBrews([], 1); } - console.log('a', - !this.state.brewCollection || this.state.brewCollection.length === 0 - ); if (!this.state.brewCollection) { this.setState({ error: '404' }); } } + }, - + loadTotal: async function () { + console.log('running loadTotal'); + const {title, v3, legacy} = this.state; + + if (title !== '') { + + try { + await request + .get( + `/api/archive/total?title=${title}&v3=${v3}&legacy=${legacy}` + ).then((response) => { + if (response.ok) { + this.setState({totalBrews : response.body.totalBrews}); + } + }); + } catch (error) { + console.log('error at loadTotal: ', error); + this.setState({ error: `${error.response.status}` }); + this.updateStateWithBrews([], 1); + } + } }, renderNavItems: function () { return ( - - Archive: Search for brews - + Archive: Search for brews @@ -161,6 +174,7 @@ const ArchivePage = createClass({ defaultValue={this.state.title} onKeyDown={(e) => { if (e.key === 'Enter') { + this.loadTotal(); this.loadPage(1, true); } }} @@ -211,6 +225,7 @@ const ArchivePage = createClass({ + )} +
    {pages}
+ {page < totalPages && ( + + )} + + ); + } else { return;} - return ( -
- {page > 1 && ( - - )} -
    {pages}
- {page < totalPages && ( - - )} -
- ); + }, renderFoundBrews() { console.log('State when rendering:'); - console.table(this.state); + const stateWithoutBrewCollection = { ...this.state }; + delete stateWithoutBrewCollection.brewCollection; + console.table(stateWithoutBrewCollection); + console.table(this.state.brewCollection); + const { title, brewCollection, page, totalPages, error, searching } = this.state; diff --git a/server/admin.api.js b/server/admin.api.js index 626e63d68..5ddba0902 100644 --- a/server/admin.api.js +++ b/server/admin.api.js @@ -100,7 +100,6 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{ }); }); - /* Compresses the "text" field of a brew to binary */ router.put('/admin/compress/:id', (req, res)=>{ HomebrewModel.findOne({ _id: req.params.id }) @@ -122,7 +121,6 @@ router.put('/admin/compress/:id', (req, res)=>{ }); }); - router.get('/admin/stats', mw.adminOnly, async (req, res)=>{ try { const totalBrewsCount = await HomebrewModel.countDocuments({}); diff --git a/server/archive.api.js b/server/archive.api.js index a38eb269e..4643e7181 100644 --- a/server/archive.api.js +++ b/server/archive.api.js @@ -3,7 +3,6 @@ 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 { @@ -17,9 +16,11 @@ const archive = { */ const bright = '\x1b[1m'; // Bright (bold) style - const yellow = '\x1b[93m'; // yellow color + const yellow = '\x1b[93m'; // yellow color const reset = '\x1b[0m'; // Reset to default style - console.log(`Query as received in ${bright + yellow}archive api${reset}:`); + console.log( + `Query as received in ${bright + yellow}archive api${reset}:` + ); console.table(req.query); const title = req.query.title || ''; @@ -84,18 +85,90 @@ const archive = { .maxTimeMS(5000) .exec(); - const totalBrews = brews.length; + return res.json({ brews, page}); - const totalPages = Math.ceil(totalBrews / pageSize); - console.log('Total brews: ', totalBrews); - console.log('Total pages: ', totalPages); - return res.json({ brews, page, totalPages, totalBrews }); } catch (error) { console.error(error); - + if (error.response && error.response.status) { const status = error.response.status; - + + if (status === 500) { + return res.status(500).json({ + errorCode: '500', + message: 'Internal Server Error', + }); + } else if (status === 503) { + return res.status(503).json({ + errorCode: '503', + message: 'Service Unavailable', + }); + } else { + return res.status(status).json({ + errorCode: status.toString(), + message: 'Internal Server Error', + }); + } + } else { + return res.status(500).json({ + errorCode: '500', + message: 'Internal Server Error', + }); + } + } + }, + findTotal: async (req, res) => { + try { + const title = req.query.title || ''; + + const brewsQuery = { + $or: [], + published: true, + }; + if (req.query.legacy === 'true') { + brewsQuery.$or.push({ renderer: 'legacy' }); + } + if (req.query.v3 === 'true') { + brewsQuery.$or.push({ renderer: 'V3' }); + } + // If user wants to use RegEx it needs to format like /text/ + const titleConditionsArray = + title.startsWith('/') && title.endsWith('/') + ? [ + { + 'title': { + $regex: title.slice(1, -1), + $options: 'i', + }, + }, + ] + : buildTitleConditions(title); + + function buildTitleConditions(inputString) { + return [ + { + $text: { + $search: inputString, + $caseSensitive: false, + }, + }, + ]; + } + const titleQuery = { + $and: [brewsQuery, ...titleConditionsArray], + }; + console.table(req.query); + + const totalBrews = await HomebrewModel.countDocuments(titleQuery); + + return res.json({totalBrews}); + + } catch (error) { + console.error(error); + + if (error.response && error.response.status) { + const status = error.response.status; + if (status === 500) { return res.status(500).json({ errorCode: '500', @@ -122,6 +195,7 @@ const archive = { }, }; +router.get('/api/archive/total', asyncHandler(archive.findTotal)); router.get('/api/archive', asyncHandler(archive.findBrews)); module.exports = router; diff --git a/themes/themes.json b/themes/themes.json index 0d28c7394..bc719185d 100644 --- a/themes/themes.json +++ b/themes/themes.json @@ -29,12 +29,12 @@ "baseSnippets": false, "path": "Blank" }, - "Journal": { + "journal": { "name": "Journal", "renderer": "V3", "baseTheme": false, "baseSnippets": "5ePHB", - "path": "Journal" + "path": "journal" } } } \ No newline at end of file