mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-01-09 20:12:41 +00:00
archive 2.0
This commit is contained in:
@@ -21,37 +21,37 @@ const ArchivePage = createClass({
|
|||||||
getDefaultProps: function () {
|
getDefaultProps: function () {
|
||||||
return {};
|
return {};
|
||||||
},
|
},
|
||||||
|
|
||||||
getInitialState: function () {
|
getInitialState: function () {
|
||||||
return {
|
return {
|
||||||
//request
|
//# request
|
||||||
title: this.props.query.title || '',
|
title: this.props.query.title || '',
|
||||||
//tags : {},
|
//tags: {},
|
||||||
legacy: `${this.props.query.legacy === 'false' ? false : true}`,
|
legacy: `${this.props.query.legacy === 'false' ? false : true}`,
|
||||||
v3: `${this.props.query.v3 === 'false' ? false : true}`,
|
v3: `${this.props.query.v3 === 'false' ? false : true}`,
|
||||||
pageSize: this.props.query.size || 10,
|
pageSize: this.props.query.size || 10,
|
||||||
page: parseInt(this.props.query.page) || 1,
|
page: parseInt(this.props.query.page) || 1,
|
||||||
|
|
||||||
//response
|
//# response
|
||||||
brewCollection: null,
|
brewCollection: null,
|
||||||
totalPages: null,
|
|
||||||
totalBrews: null,
|
totalBrews: null,
|
||||||
|
|
||||||
searching: false,
|
searching: false,
|
||||||
error: null,
|
error: null,
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
|
|
||||||
componentDidMount: function () {
|
componentDidMount: function () {
|
||||||
if (this.state.title !== '') {
|
if (this.state.title !== '') {
|
||||||
this.loadPage(this.state.page, false);
|
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({
|
this.setState({
|
||||||
brewCollection: brews || null,
|
brewCollection: brews || null,
|
||||||
page: parseInt(page) || 1,
|
page: parseInt(page) || 1,
|
||||||
totalPages: totalPages || 1,
|
|
||||||
totalBrews: totalBrews,
|
|
||||||
searching: false,
|
searching: false,
|
||||||
});
|
});
|
||||||
},
|
},
|
||||||
@@ -78,6 +78,7 @@ const ArchivePage = createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
loadPage: async function (page, update) {
|
loadPage: async function (page, update) {
|
||||||
|
console.log('running loadPage');
|
||||||
//load form data directly
|
//load form data directly
|
||||||
const title = document.getElementById('title').value || '';
|
const title = document.getElementById('title').value || '';
|
||||||
const size = document.getElementById('size').value || 10;
|
const size = document.getElementById('size').value || 10;
|
||||||
@@ -96,46 +97,58 @@ const ArchivePage = createClass({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (title !== '') {
|
if (title !== '') {
|
||||||
console.log('pagesize when querying: ', this.state.pageSize);
|
|
||||||
try {
|
try {
|
||||||
this.setState({ searching: true, error: null });
|
this.setState({ searching: true, error: null });
|
||||||
|
|
||||||
await request
|
await request.get(
|
||||||
.get(
|
`/api/archive?title=${title}&page=${page}&size=${size}&v3=${v3}&legacy=${legacy}`
|
||||||
`/api/archive?title=${title}&page=${page}&size=${size}&v3=${v3}&legacy=${legacy}`
|
).then((response) => {
|
||||||
)
|
if (response.ok) {
|
||||||
.then((response) => {
|
this.updateStateWithBrews(
|
||||||
if (response.ok) {
|
response.body.brews,
|
||||||
this.updateStateWithBrews(
|
page
|
||||||
response.body.brews,
|
);
|
||||||
page,
|
}
|
||||||
response.body.totalPages,
|
});
|
||||||
response.body.totalBrews
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
console.log('error at loadPage: ', error);
|
||||||
this.setState({ error: `${error.response.status}` });
|
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) {
|
if (!this.state.brewCollection) {
|
||||||
this.setState({ error: '404' });
|
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 () {
|
renderNavItems: function () {
|
||||||
return (
|
return (
|
||||||
<Navbar>
|
<Navbar>
|
||||||
<Nav.section>
|
<Nav.section>
|
||||||
<Nav.item className="brewTitle">
|
<Nav.item className="brewTitle">Archive: Search for brews</Nav.item>
|
||||||
Archive: Search for brews
|
|
||||||
</Nav.item>
|
|
||||||
</Nav.section>
|
</Nav.section>
|
||||||
<Nav.section>
|
<Nav.section>
|
||||||
<NewBrew />
|
<NewBrew />
|
||||||
@@ -161,6 +174,7 @@ const ArchivePage = createClass({
|
|||||||
defaultValue={this.state.title}
|
defaultValue={this.state.title}
|
||||||
onKeyDown={(e) => {
|
onKeyDown={(e) => {
|
||||||
if (e.key === 'Enter') {
|
if (e.key === 'Enter') {
|
||||||
|
this.loadTotal();
|
||||||
this.loadPage(1, true);
|
this.loadPage(1, true);
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
@@ -211,6 +225,7 @@ const ArchivePage = createClass({
|
|||||||
<button
|
<button
|
||||||
className="search"
|
className="search"
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
|
this.loadTotal();
|
||||||
this.loadPage(1, true);
|
this.loadPage(1, true);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
@@ -228,53 +243,64 @@ const ArchivePage = createClass({
|
|||||||
},
|
},
|
||||||
|
|
||||||
renderPaginationControls() {
|
renderPaginationControls() {
|
||||||
const title = encodeURIComponent(this.state.title);
|
if (this.state.totalBrews) {
|
||||||
const size = parseInt(this.state.pageSize);
|
const title = encodeURIComponent(this.state.title);
|
||||||
const { page, totalPages, legacy, v3 } = this.state;
|
const size = parseInt(this.state.pageSize);
|
||||||
const pages = new Array(totalPages).fill().map((_, index) => (
|
const { page, totalBrews} = this.state;
|
||||||
<a
|
|
||||||
key={index}
|
|
||||||
className={`pageNumber ${
|
|
||||||
page == index + 1 ? 'currentPage' : ''
|
|
||||||
}`}
|
|
||||||
href={`/archive?title=${title}&page=${
|
|
||||||
index + 1
|
|
||||||
}&size=${size}&v3=${v3}&legacy=${legacy}`}
|
|
||||||
>
|
|
||||||
{index + 1}
|
|
||||||
</a>
|
|
||||||
));
|
|
||||||
|
|
||||||
if (totalPages === null) {
|
const totalPages = Math.ceil(totalBrews / size);
|
||||||
return;
|
|
||||||
}
|
const pages = new Array(totalPages).fill().map((_, index) => (
|
||||||
|
<a
|
||||||
|
key={index}
|
||||||
|
className={`pageNumber ${
|
||||||
|
page == index + 1 ? 'currentPage' : ''
|
||||||
|
}`}
|
||||||
|
href={`/archive?title=${title}&page=${
|
||||||
|
index + 1
|
||||||
|
}&size=${size}&v3=${v3}&legacy=${legacy}`}
|
||||||
|
>
|
||||||
|
{index + 1}
|
||||||
|
</a>
|
||||||
|
));
|
||||||
|
|
||||||
|
if (totalPages === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="paginationControls">
|
||||||
|
{page > 1 && (
|
||||||
|
<button
|
||||||
|
className="previousPage"
|
||||||
|
onClick={() => this.loadPage(page - 1, false)}
|
||||||
|
>
|
||||||
|
<<
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
<ol className="pages">{pages}</ol>
|
||||||
|
{page < totalPages && (
|
||||||
|
<button
|
||||||
|
className="nextPage"
|
||||||
|
onClick={() => this.loadPage(page + 1, false)}
|
||||||
|
>
|
||||||
|
>>
|
||||||
|
</button>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
} else { return;}
|
||||||
|
|
||||||
return (
|
|
||||||
<div className="paginationControls">
|
|
||||||
{page > 1 && (
|
|
||||||
<button
|
|
||||||
className="previousPage"
|
|
||||||
onClick={() => this.loadPage(page - 1, false)}
|
|
||||||
>
|
|
||||||
<<
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
<ol className="pages">{pages}</ol>
|
|
||||||
{page < totalPages && (
|
|
||||||
<button
|
|
||||||
className="nextPage"
|
|
||||||
onClick={() => this.loadPage(page + 1, false)}
|
|
||||||
>
|
|
||||||
>>
|
|
||||||
</button>
|
|
||||||
)}
|
|
||||||
</div>
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
renderFoundBrews() {
|
renderFoundBrews() {
|
||||||
console.log('State when rendering:');
|
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 } =
|
const { title, brewCollection, page, totalPages, error, searching } =
|
||||||
this.state;
|
this.state;
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,6 @@ router.get('/admin/finduncompressed', mw.adminOnly, (req, res)=>{
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
/* Compresses the "text" field of a brew to binary */
|
/* Compresses the "text" field of a brew to binary */
|
||||||
router.put('/admin/compress/:id', (req, res)=>{
|
router.put('/admin/compress/:id', (req, res)=>{
|
||||||
HomebrewModel.findOne({ _id: req.params.id })
|
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)=>{
|
router.get('/admin/stats', mw.adminOnly, async (req, res)=>{
|
||||||
try {
|
try {
|
||||||
const totalBrewsCount = await HomebrewModel.countDocuments({});
|
const totalBrewsCount = await HomebrewModel.countDocuments({});
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ const router = require('express').Router();
|
|||||||
const asyncHandler = require('express-async-handler');
|
const asyncHandler = require('express-async-handler');
|
||||||
|
|
||||||
const archive = {
|
const archive = {
|
||||||
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 {
|
||||||
@@ -17,9 +16,11 @@ const archive = {
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
const bright = '\x1b[1m'; // Bright (bold) style
|
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
|
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);
|
console.table(req.query);
|
||||||
|
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
@@ -84,18 +85,90 @@ const archive = {
|
|||||||
.maxTimeMS(5000)
|
.maxTimeMS(5000)
|
||||||
.exec();
|
.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) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
||||||
if (error.response && error.response.status) {
|
if (error.response && error.response.status) {
|
||||||
const status = 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) {
|
if (status === 500) {
|
||||||
return res.status(500).json({
|
return res.status(500).json({
|
||||||
errorCode: '500',
|
errorCode: '500',
|
||||||
@@ -122,6 +195,7 @@ const archive = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
router.get('/api/archive/total', asyncHandler(archive.findTotal));
|
||||||
router.get('/api/archive', asyncHandler(archive.findBrews));
|
router.get('/api/archive', asyncHandler(archive.findBrews));
|
||||||
|
|
||||||
module.exports = router;
|
module.exports = router;
|
||||||
|
|||||||
@@ -29,12 +29,12 @@
|
|||||||
"baseSnippets": false,
|
"baseSnippets": false,
|
||||||
"path": "Blank"
|
"path": "Blank"
|
||||||
},
|
},
|
||||||
"Journal": {
|
"journal": {
|
||||||
"name": "Journal",
|
"name": "Journal",
|
||||||
"renderer": "V3",
|
"renderer": "V3",
|
||||||
"baseTheme": false,
|
"baseTheme": false,
|
||||||
"baseSnippets": "5ePHB",
|
"baseSnippets": "5ePHB",
|
||||||
"path": "Journal"
|
"path": "journal"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user