diff --git a/cart/sx/layouts.sx b/cart/sx/layouts.sx index 93c2a23..30822fd 100644 --- a/cart/sx/layouts.sx +++ b/cart/sx/layouts.sx @@ -116,3 +116,21 @@ (defcomp ~cart-orders-rows (&key rows next-scroll) (<> rows next-scroll)) + +;; Composition defcomp — replaces Python loop in render_orders_rows +(defcomp ~cart-orders-rows-content (&key orders detail-url-prefix page total-pages next-url) + (~cart-orders-rows + :rows (map (lambda (od) + (~order-row-pair :order od :detail-url-prefix detail-url-prefix)) + (or orders (list))) + :next-scroll (if (< page total-pages) + (~infinite-scroll :url next-url :page page + :total-pages total-pages :id-prefix "orders" :colspan 5) + (~order-end-row)))) + +;; Composition defcomp — replaces conditional composition in render_checkout_error_page +(defcomp ~cart-checkout-error-from-data (&key msg order-id back-url) + (~checkout-error-content + :msg msg + :order (when order-id (~checkout-error-order-id :oid (str "#" order-id))) + :back-url back-url)) diff --git a/cart/sxc/pages/renders.py b/cart/sxc/pages/renders.py index 6237a81..a920167 100644 --- a/cart/sxc/pages/renders.py +++ b/cart/sxc/pages/renders.py @@ -1,8 +1,6 @@ """Cart render functions — called from bp routes.""" from __future__ import annotations -from shared.sx.parser import SxExpr - from .utils import _serialize_order, _serialize_calendar_entry @@ -32,20 +30,10 @@ def render_orders_rows(ctx, orders, page, total_pages, url_for_fn, qs_fn): list_url = pfx + url_for_fn("orders.list_orders") detail_url_prefix = pfx + url_for_fn("orders.order.order_detail", order_id=0).rsplit("0/", 1)[0] order_dicts = [_serialize_order(o) for o in orders] - parts = [] - for od in order_dicts: - parts.append(sx_call("order-row-pair", order=od, detail_url_prefix=detail_url_prefix)) - next_scroll = "" - if page < total_pages: - next_url = list_url + qs_fn(page=page + 1) - next_scroll = sx_call("infinite-scroll", url=next_url, page=page, - total_pages=total_pages, id_prefix="orders", colspan=5) - else: - next_scroll = sx_call("order-end-row") - return sx_call("cart-orders-rows", - rows=SxExpr("(<> " + " ".join(parts) + ")"), - next_scroll=next_scroll, - ) + next_url = list_url + qs_fn(page=page + 1) if page < total_pages else "" + return sx_call("cart-orders-rows-content", + orders=order_dicts, detail_url_prefix=detail_url_prefix, + page=page, total_pages=total_pages, next_url=next_url) async def render_orders_oob(ctx, orders, page, total_pages, search, search_count, url_for_fn, qs_fn): @@ -112,11 +100,11 @@ async def render_checkout_error_page(ctx, error=None, order=None): from shared.sx.helpers import sx_call, render_to_sx_with_env, full_page_sx from shared.infrastructure.urls import cart_url err_msg = error or "Unexpected error while creating the hosted checkout session." - order_sx = sx_call("checkout-error-order-id", oid=f"#{order.id}") if order else None hdr = await render_to_sx_with_env("layout-root-full", {}) filt = sx_call("checkout-error-header") - content = sx_call("checkout-error-content", msg=err_msg, - order=order_sx or None, back_url=cart_url("/")) + content = sx_call("cart-checkout-error-from-data", + msg=err_msg, order_id=order.id if order else None, + back_url=cart_url("/")) return await full_page_sx(ctx, header_rows=hdr, filter=filt, content=content)