mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2025-12-26 20:22:42 +00:00
archive 2.0
This commit is contained in:
@@ -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 (
|
||||
<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>
|
||||
<NewBrew />
|
||||
@@ -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({
|
||||
<button
|
||||
className="search"
|
||||
onClick={() => {
|
||||
this.loadTotal();
|
||||
this.loadPage(1, true);
|
||||
}}
|
||||
>
|
||||
@@ -228,53 +243,64 @@ const ArchivePage = createClass({
|
||||
},
|
||||
|
||||
renderPaginationControls() {
|
||||
const title = encodeURIComponent(this.state.title);
|
||||
const size = parseInt(this.state.pageSize);
|
||||
const { page, totalPages, legacy, v3 } = this.state;
|
||||
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 (this.state.totalBrews) {
|
||||
const title = encodeURIComponent(this.state.title);
|
||||
const size = parseInt(this.state.pageSize);
|
||||
const { page, totalBrews} = this.state;
|
||||
|
||||
if (totalPages === null) {
|
||||
return;
|
||||
}
|
||||
const totalPages = Math.ceil(totalBrews / size);
|
||||
|
||||
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() {
|
||||
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;
|
||||
|
||||
|
||||
@@ -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({});
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -29,12 +29,12 @@
|
||||
"baseSnippets": false,
|
||||
"path": "Blank"
|
||||
},
|
||||
"Journal": {
|
||||
"journal": {
|
||||
"name": "Journal",
|
||||
"renderer": "V3",
|
||||
"baseTheme": false,
|
||||
"baseSnippets": "5ePHB",
|
||||
"path": "Journal"
|
||||
"path": "journal"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user