Cart rendering, bug fixes with cart adding/rming

This commit is contained in:
2026-02-08 19:15:48 +07:00
parent e8a61c05b3
commit 6409d66dfd
12 changed files with 252 additions and 38 deletions

View File

@@ -3,14 +3,31 @@ from typing import Optional
from aiogram.types import InlineKeyboardButton, InlineKeyboardMarkup
from aiogram.utils.keyboard import InlineKeyboardBuilder
from db.models import Category
from db.models.products import Product
from db.models import Category, Product
from dto.cart import CartItemDTO
def pagination_row(
*,
page: int,
prev_cb: Optional[str] = None,
next_cb: Optional[str] = None,
) -> list[InlineKeyboardButton]:
row = []
if prev_cb:
row.append(InlineKeyboardButton(text="⬅️", callback_data=prev_cb))
row.append(InlineKeyboardButton(text=str(page + 1), callback_data="..."))
if next_cb:
row.append(InlineKeyboardButton(text="➡️", callback_data=next_cb))
return row
def cart_btn(cart_items: int) -> InlineKeyboardButton:
return InlineKeyboardButton(
text=f"🛒 Корзина ({cart_items})", callback_data="cart"
)
return InlineKeyboardButton(text=f"🛒 Корзина ({cart_items})", callback_data="cart")
def get_back_to_catalogue(
@@ -75,23 +92,11 @@ def render_products(
for product in products
]
nav = []
if show_prev:
nav.append(
InlineKeyboardButton(
text="⬅️", callback_data=f"products:{cat_id}:{page - 1}"
)
)
nav.append(InlineKeyboardButton(text=str(page), callback_data="..."))
if show_next:
nav.append(
InlineKeyboardButton(
text="➡️", callback_data=f"products:{cat_id}:{page + 1}"
)
)
nav = pagination_row(
page=page,
prev_cb=f"products:{cat_id}:{page - 1}" if show_prev else None,
next_cb=f"products:{cat_id}:{page + 1}" if show_next else None,
)
return InlineKeyboardBuilder(
[
@@ -145,3 +150,38 @@ def render_product_interactions(
btns.append([InlineKeyboardButton(text="⬅️", callback_data=f"cat:{category_id}")])
return InlineKeyboardBuilder(btns).as_markup()
def render_cart(
cart_items: list[CartItemDTO],
*,
page: int = 0,
show_prev: Optional[bool] = False,
show_next: Optional[bool] = False,
) -> InlineKeyboardMarkup:
btns = [
InlineKeyboardButton(
text=f"{cart_item.quantity} | {cart_item.name} | {cart_item.total}",
callback_data=f"product:{cart_item.product_id}",
)
for cart_item in cart_items
]
nav = pagination_row(
page=page,
prev_cb=f"cart:{page - 1}" if show_prev else None,
next_cb=f"cart:{page + 1}" if show_next else None,
)
return InlineKeyboardBuilder(
[
[
InlineKeyboardButton(
text="⬅️ Назад",
callback_data="menu:main",
)
]
]
+ [btns[i : i + 2] for i in range(0, len(cart_items), 2)]
+ [nav]
).as_markup()