Refactor the pagination data retrieval
parent
4b3867e47f
commit
5f81173919
|
@ -616,33 +616,45 @@ const newId = () => {
|
|||
return new ObjectId();
|
||||
};
|
||||
|
||||
const getData = (req, page, query) => {
|
||||
/**
|
||||
* @param {boolean} frontend // whether or not this is an front or admin call
|
||||
* @param {req} req // express `req` object
|
||||
* @param {integer} page // The page number
|
||||
* @param {string} collection // The collection to search
|
||||
* @param {object} query // The mongo query
|
||||
* @param {object} sort // The mongo sort
|
||||
*/
|
||||
const paginateData = (frontend, req, page, collection, query, sort) => {
|
||||
const db = req.app.db;
|
||||
const config = getConfig();
|
||||
const numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
||||
let numberItems = 10;
|
||||
if(frontend){
|
||||
numberItems = config.productsPerPage ? config.productsPerPage : 6;
|
||||
}
|
||||
|
||||
let skip = 0;
|
||||
if(page > 1){
|
||||
skip = (page - 1) * numberProducts;
|
||||
skip = (page - 1) * numberItems;
|
||||
}
|
||||
|
||||
if(!query){
|
||||
query = {};
|
||||
}
|
||||
|
||||
query.productPublished = { $ne: false };
|
||||
if(!sort){
|
||||
sort = {};
|
||||
}
|
||||
|
||||
// Run our queries
|
||||
return Promise.all([
|
||||
db.products.find(query).skip(skip).limit(parseInt(numberProducts)).toArray(),
|
||||
db.products.countDocuments(query)
|
||||
db[collection].find(query).skip(skip).limit(parseInt(numberItems)).sort(sort).toArray(),
|
||||
db[collection].countDocuments(query)
|
||||
])
|
||||
.then((result) => {
|
||||
const returnData = { data: result[0], totalProducts: result[1] };
|
||||
const returnData = { data: result[0], totalItems: result[1] };
|
||||
return returnData;
|
||||
})
|
||||
.catch((err) => {
|
||||
throw new Error('Error retrieving products');
|
||||
throw new Error('Error retrieving paginated data');
|
||||
});
|
||||
};
|
||||
|
||||
|
@ -702,7 +714,7 @@ module.exports = {
|
|||
sendEmail,
|
||||
getId,
|
||||
newId,
|
||||
getData,
|
||||
paginateData,
|
||||
hooker,
|
||||
getCountryList,
|
||||
cleanAmount
|
||||
|
|
|
@ -80,8 +80,8 @@ $(document).ready(function (){
|
|||
|
||||
if($('#pager').length){
|
||||
var pageNum = $('#pageNum').val();
|
||||
var pageLen = $('#productsPerPage').val();
|
||||
var productCount = $('#totalProductCount').val();
|
||||
var pageLen = $('#itemsPerPage').val();
|
||||
var itemCount = $('#totalItemCount').val();
|
||||
var paginateUrl = $('#paginateUrl').val();
|
||||
var searchTerm = $('#searchTerm').val();
|
||||
|
||||
|
@ -90,11 +90,12 @@ $(document).ready(function (){
|
|||
}
|
||||
|
||||
var pagerHref = '/' + paginateUrl + '/' + searchTerm + '{{number}}';
|
||||
var totalProducts = Math.ceil(productCount / pageLen);
|
||||
var totalItems = Math.ceil(itemCount / pageLen);
|
||||
|
||||
if(parseInt(productCount) > parseInt(pageLen)){
|
||||
if(parseInt(itemCount) > parseInt(pageLen)){
|
||||
console.log('here?');
|
||||
$('#pager').bootpag({
|
||||
total: totalProducts,
|
||||
total: totalItems,
|
||||
page: pageNum,
|
||||
maxVisible: 5,
|
||||
href: pagerHref,
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -16,7 +16,7 @@ const {
|
|||
updateTotalCart,
|
||||
emptyCart,
|
||||
updateSubscriptionCheck,
|
||||
getData,
|
||||
paginateData,
|
||||
addSitemapProducts,
|
||||
getCountryList
|
||||
} = require('../lib/common');
|
||||
|
@ -642,38 +642,38 @@ router.get('/search/:searchTerm/:pageNum?', (req, res) => {
|
|||
}
|
||||
|
||||
Promise.all([
|
||||
getData(req, pageNum, { _id: { $in: lunrIdArray } }),
|
||||
paginateData(true, req, pageNum, 'products', { _id: { $in: lunrIdArray } }),
|
||||
getMenu(db)
|
||||
])
|
||||
.then(([results, menu]) => {
|
||||
// If JSON query param return json instead
|
||||
if(req.query.json === 'true'){
|
||||
res.status(200).json(results.data);
|
||||
return;
|
||||
}
|
||||
.then(([results, menu]) => {
|
||||
// If JSON query param return json instead
|
||||
if(req.query.json === 'true'){
|
||||
res.status(200).json(results.data);
|
||||
return;
|
||||
}
|
||||
|
||||
res.render(`${config.themeViews}index`, {
|
||||
title: 'Results',
|
||||
results: results.data,
|
||||
filtered: true,
|
||||
session: req.session,
|
||||
metaDescription: req.app.config.cartTitle + ' - Search term: ' + searchTerm,
|
||||
searchTerm: searchTerm,
|
||||
message: clearSessionValue(req.session, 'message'),
|
||||
messageType: clearSessionValue(req.session, 'messageType'),
|
||||
productsPerPage: numberProducts,
|
||||
totalProductCount: results.totalProducts,
|
||||
pageNum: pageNum,
|
||||
paginateUrl: 'search',
|
||||
config: config,
|
||||
menu: sortMenu(menu),
|
||||
helpers: req.handlebars.helpers,
|
||||
showFooter: 'showFooter'
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(colors.red('Error searching for products', err));
|
||||
res.render(`${config.themeViews}index`, {
|
||||
title: 'Results',
|
||||
results: results.data,
|
||||
filtered: true,
|
||||
session: req.session,
|
||||
metaDescription: req.app.config.cartTitle + ' - Search term: ' + searchTerm,
|
||||
searchTerm: searchTerm,
|
||||
message: clearSessionValue(req.session, 'message'),
|
||||
messageType: clearSessionValue(req.session, 'messageType'),
|
||||
productsPerPage: numberProducts,
|
||||
totalProductCount: results.totalItems,
|
||||
pageNum: pageNum,
|
||||
paginateUrl: 'search',
|
||||
config: config,
|
||||
menu: sortMenu(menu),
|
||||
helpers: req.handlebars.helpers,
|
||||
showFooter: 'showFooter'
|
||||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
console.error(colors.red('Error searching for products', err));
|
||||
});
|
||||
});
|
||||
|
||||
// search products
|
||||
|
@ -695,7 +695,7 @@ router.get('/category/:cat/:pageNum?', (req, res) => {
|
|||
}
|
||||
|
||||
Promise.all([
|
||||
getData(req, pageNum, { _id: { $in: lunrIdArray } }),
|
||||
paginateData(true, req, pageNum, 'products', { _id: { $in: lunrIdArray } }),
|
||||
getMenu(db)
|
||||
])
|
||||
.then(([results, menu]) => {
|
||||
|
@ -717,7 +717,7 @@ router.get('/category/:cat/:pageNum?', (req, res) => {
|
|||
message: clearSessionValue(req.session, 'message'),
|
||||
messageType: clearSessionValue(req.session, 'messageType'),
|
||||
productsPerPage: numberProducts,
|
||||
totalProductCount: results.totalProducts,
|
||||
totalProductCount: results.totalItems,
|
||||
pageNum: pageNum,
|
||||
menuLink: _.find(sortedMenu.items, (obj) => { return obj.link === searchTerm; }),
|
||||
paginateUrl: 'category',
|
||||
|
@ -777,7 +777,7 @@ router.get('/page/:pageNum', (req, res, next) => {
|
|||
const numberProducts = config.productsPerPage ? config.productsPerPage : 6;
|
||||
|
||||
Promise.all([
|
||||
getData(req, req.params.pageNum),
|
||||
paginateData(true, req, req.params.pageNum, 'products'),
|
||||
getMenu(db)
|
||||
])
|
||||
.then(([results, menu]) => {
|
||||
|
@ -796,7 +796,7 @@ router.get('/page/:pageNum', (req, res, next) => {
|
|||
metaDescription: req.app.config.cartTitle + ' - Products page: ' + req.params.pageNum,
|
||||
config: req.app.config,
|
||||
productsPerPage: numberProducts,
|
||||
totalProductCount: results.totalProducts,
|
||||
totalProductCount: results.totalItems,
|
||||
pageNum: req.params.pageNum,
|
||||
paginateUrl: 'page',
|
||||
helpers: req.handlebars.helpers,
|
||||
|
@ -818,7 +818,7 @@ router.get('/:page?', async (req, res, next) => {
|
|||
// if no page is specified, just render page 1 of the cart
|
||||
if(!req.params.page){
|
||||
Promise.all([
|
||||
getData(req, 1, {}),
|
||||
paginateData(true, req, 1, 'products', {}),
|
||||
getMenu(db)
|
||||
])
|
||||
.then(([results, menu]) => {
|
||||
|
@ -828,6 +828,8 @@ router.get('/:page?', async (req, res, next) => {
|
|||
return;
|
||||
}
|
||||
|
||||
console.log('results', results);
|
||||
|
||||
res.render(`${config.themeViews}index`, {
|
||||
title: `${config.cartTitle} - Shop`,
|
||||
theme: config.theme,
|
||||
|
@ -837,7 +839,7 @@ router.get('/:page?', async (req, res, next) => {
|
|||
messageType: clearSessionValue(req.session, 'messageType'),
|
||||
config,
|
||||
productsPerPage: numberProducts,
|
||||
totalProductCount: results.totalProducts,
|
||||
totalProductCount: results.totalItems,
|
||||
pageNum: 1,
|
||||
paginateUrl: 'page',
|
||||
helpers: req.handlebars.helpers,
|
||||
|
|
|
@ -58,9 +58,9 @@
|
|||
{{/each}}
|
||||
</div>
|
||||
</div>
|
||||
<input type="hidden" id="productsPerPage" value="{{productsPerPage}}" >
|
||||
<input type="hidden" id="itemsPerPage" value="{{productsPerPage}}" >
|
||||
<input type="hidden" id="pageNum" value="{{pageNum}}">
|
||||
<input type="hidden" id="totalProductCount" value="{{totalProductCount}}">
|
||||
<input type="hidden" id="totalItemCount" value="{{totalProductCount}}">
|
||||
<input type="hidden" id="paginateUrl" value="{{paginateUrl}}">
|
||||
<input type="hidden" id="searchTerm" value="{{searchTerm}}">
|
||||
<div class="col-md-12">
|
||||
|
|
Loading…
Reference in New Issue