Delet subcategory function in handleCategorySelection

This commit is contained in:
NW 2024-12-13 18:24:24 +00:00
parent 95d5fe644d
commit a400d12d16

View File

@ -177,45 +177,45 @@ export default class UserProductHandler {
const chatId = callbackQuery.message.chat.id; const chatId = callbackQuery.message.chat.id;
const messageId = callbackQuery.message.message_id; const messageId = callbackQuery.message.message_id;
const [locationId, categoryId] = callbackQuery.data.replace('shop_category_', '').split('_'); const [locationId, categoryId] = callbackQuery.data.replace('shop_category_', '').split('_');
try { try {
const subcategories = await CategoryService.getSubcategoriesByCategoryId(categoryId); // Получаем товары для выбранной категории
const location = await LocationService.getLocationById(locationId); const products = await ProductService.getProductsByCategoryId(categoryId);
if (subcategories.length === 0) { if (products.length === 0) {
await bot.editMessageText( await bot.editMessageText(
'No products available in this category yet.', 'No products available in this category.',
{ {
chat_id: chatId, chat_id: chatId,
message_id: messageId, message_id: messageId,
reply_markup: { reply_markup: {
inline_keyboard: [[ inline_keyboard: [[
{ { text: '« Back', callback_data: `shop_district_${locationId}` }
text: '« Back to Categories',
callback_data: `shop_district_${location.country}_${location.city}_${location.district}`
}
]] ]]
} }
} }
); );
return; return;
} }
// Создаем клавиатуру с товарами
const keyboard = { const keyboard = {
inline_keyboard: [ inline_keyboard: products.map(product => [
...subcategories.map(sub => [{ {
text: sub.name, text: `${product.name} - $${product.price}`,
callback_data: `shop_subcategory_${locationId}_${categoryId}_${sub.id}` callback_data: `shop_product_${product.id}`
}]), }
[{ ])
text: '« Back to Categories',
callback_data: `shop_district_${location.country}_${location.city}_${location.district}`
}]
]
}; };
// Добавляем кнопку "Назад"
keyboard.inline_keyboard.push([
{ text: '« Back', callback_data: `shop_district_${locationId}` }
]);
// Отправляем сообщение с товарами
await bot.editMessageText( await bot.editMessageText(
'📦 Select subcategory:', 'Select a product:',
{ {
chat_id: chatId, chat_id: chatId,
message_id: messageId, message_id: messageId,
@ -224,7 +224,7 @@ export default class UserProductHandler {
); );
} catch (error) { } catch (error) {
console.error('Error in handleCategorySelection:', error); console.error('Error in handleCategorySelection:', error);
await bot.sendMessage(chatId, 'Error loading subcategories. Please try again.'); await bot.sendMessage(chatId, 'Error loading products. Please try again.');
} }
} }