products pagination

This commit is contained in:
Artyom Ashirov 2024-11-15 08:19:54 +03:00
parent df3149e59a
commit e1eda05afe
2 changed files with 602 additions and 508 deletions

View File

@ -198,7 +198,10 @@ export default class AdminProductHandler {
{
reply_markup: {
inline_keyboard: [[
{ text: '« Back to Categories', callback_data: `prod_district_${location.country}_${location.city}_${location.district}` }
{
text: '« Back to Categories',
callback_data: `prod_district_${location.country}_${location.city}_${location.district}`
}
]]
}
}
@ -259,7 +262,10 @@ export default class AdminProductHandler {
}]),
[{text: ' Add Subcategory', callback_data: `add_subcategory_${locationId}_${categoryId}`}],
[{text: '✏️ Edit Category', callback_data: `edit_category_${locationId}_${categoryId}`}],
[{ text: '« Back', callback_data: `prod_district_${location.country}_${location.city}_${location.district}` }]
[{
text: '« Back',
callback_data: `prod_district_${location.country}_${location.city}_${location.district}`
}]
]
};
@ -297,7 +303,10 @@ export default class AdminProductHandler {
{
reply_markup: {
inline_keyboard: [[
{ text: '« Back to Subcategories', callback_data: `prod_category_${locationId}_${categoryId}` }
{
text: '« Back to Subcategories',
callback_data: `prod_category_${locationId}_${categoryId}`
}
]]
}
}
@ -336,20 +345,40 @@ export default class AdminProductHandler {
);
}
async handleSubcategorySelection(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [locationId, categoryId, subcategoryId] = callbackQuery.data.replace('prod_subcategory_', '').split('_');
async viewProductsPage(locationId, categoryId, subcategoryId, page) {
try {
const limit = 10;
const offset = (page || 0) * limit;
const previousPage = page > 0 ? page - 1 : 0;
const nextPage = page + 1;
const products = await db.allAsync(
`SELECT id, name, price, quantity_in_stock
FROM products
WHERE location_id = ? AND category_id = ? AND subcategory_id = ?
ORDER BY name`,
[locationId, categoryId, subcategoryId]
ORDER BY name
LIMIT ?
OFFSET ?
`,
[locationId, categoryId, subcategoryId, limit, offset]
);
if ((products.length === 0) && (page == 0)) {
return {
text: 'No products for this location',
markup: {
inline_keyboard: [
[{text: '« Back', callback_data: `prod_category_${locationId}_${categoryId}`}]
]
}
};
}
if ((products.length === 0) && (page > 0)) {
return await this.viewProductsPage(locationId, categoryId, subcategoryId, previousPage);
}
const subcategory = await db.getAsync('SELECT name FROM subcategories WHERE id = ?', [subcategoryId]);
const category = await db.getAsync('SELECT name FROM categories WHERE id = ?', [categoryId]);
@ -359,17 +388,48 @@ export default class AdminProductHandler {
text: `${prod.name} - $${prod.price} (${prod.quantity_in_stock} left)`,
callback_data: `view_product_${prod.id}`
}]),
[{ text: '📥 Import Products', callback_data: `add_product_${locationId}_${categoryId}_${subcategoryId}` }],
[{ text: '« Back', callback_data: `prod_category_${locationId}_${categoryId}` }]
[{
text: '📥 Import Products',
callback_data: `add_product_${locationId}_${categoryId}_${subcategoryId}`
}],
]
};
keyboard.inline_keyboard.push([
{text: `«`, callback_data: `list_products_${locationId}_${categoryId}_${subcategoryId}_${previousPage}`},
{text: `»`, callback_data: `list_products_${locationId}_${categoryId}_${subcategoryId}_${nextPage}`},
]);
keyboard.inline_keyboard.push([
{text: '« Back', callback_data: `prod_category_${locationId}_${categoryId}`}
]);
return {
text: `📦 ${category.name} > ${subcategory.name}\nSelect product or import new ones:`,
markup: keyboard
}
} catch (error) {
console.error('Error in handleSubcategorySelection:', error);
return {text: 'Error loading products. Please try again.'};
}
}
async handleSubcategorySelection(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
const [locationId, categoryId, subcategoryId] = callbackQuery.data.replace('prod_subcategory_', '').split('_');
try {
const {text, markup} = await this.viewProductsPage(locationId, categoryId, subcategoryId, 0);
await this.bot.editMessageText(
`📦 ${category.name} > ${subcategory.name}\nSelect product or import new ones:`,
text,
{
chat_id: chatId,
message_id: messageId,
reply_markup: keyboard
reply_markup: markup
}
);
} catch (error) {
@ -378,6 +438,28 @@ export default class AdminProductHandler {
}
}
async handleProductListPage(callbackQuery) {
if (!this.isAdmin(callbackQuery.from.id)) {
return;
}
const chatId = callbackQuery.message.chat.id;
const [locationId, categoryId, subcategoryId, page] = callbackQuery.data.replace('list_products_', '').split("_");
try {
const {text, markup} = await this.viewProductsPage(locationId, categoryId, subcategoryId, parseInt(page));
await this.bot.editMessageText(text, {
chat_id: chatId,
message_id: callbackQuery.message.message_id,
reply_markup: markup,
parse_mode: 'HTML'
});
} catch (e) {
return;
}
}
async handleAddProduct(callbackQuery) {
const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id;
@ -421,7 +503,10 @@ export default class AdminProductHandler {
parse_mode: 'HTML',
reply_markup: {
inline_keyboard: [[
{ text: '❌ Cancel', callback_data: `prod_subcategory_${locationId}_${categoryId}_${subcategoryId}` }
{
text: '❌ Cancel',
callback_data: `prod_subcategory_${locationId}_${categoryId}_${subcategoryId}`
}
]]
}
});
@ -498,7 +583,10 @@ export default class AdminProductHandler {
{
reply_markup: {
inline_keyboard: [[
{ text: '« Back to Products', callback_data: `prod_subcategory_${state.locationId}_${state.categoryId}_${state.subcategoryId}` }
{
text: '« Back to Products',
callback_data: `prod_subcategory_${state.locationId}_${state.categoryId}_${state.subcategoryId}`
}
]]
}
}
@ -558,7 +646,10 @@ Coordinates: ${product.hidden_coordinates}
{text: '✏️ Edit', callback_data: `edit_product_${productId}`},
{text: '❌ Delete', callback_data: `delete_product_${productId}`}
],
[{ text: '« Back', callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}` }]
[{
text: '« Back',
callback_data: `prod_subcategory_${product.location_id}_${product.category_id}_${product.subcategory_id}`
}]
]
};

View File

@ -269,6 +269,9 @@ bot.on('callback_query', async (callbackQuery) => {
} else if (action.startsWith('prod_subcategory_')) {
logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleSubcategorySelection(callbackQuery);
} else if (action.startsWith('list_products_')) {
logDebug(action, 'handleSubcategorySelection');
await adminProductHandler.handleProductListPage(callbackQuery);
} else if (action.startsWith('add_product_')) {
logDebug(action, 'handleAddProduct');
await adminProductHandler.handleAddProduct(callbackQuery);