Skip to main content

max / makenotwork

2.2 KB · 65 lines History Blame Raw
1 //! The Askama entry point for the described "remove from cart" button.
2 //!
3 //! Shape 8 (`b279b9eb`), through the glue-module ruling `27d5e5b8`.
4 //!
5 //! # The confirm is carried across rather than settled here
6 //!
7 //! This is the weakest case in the shape's destructive column: taking a line
8 //! off a cart is undone by adding it again, and the prompt asks about a
9 //! decision the reader has made by pressing a button labelled Remove. Whether
10 //! it earns its keep is a question for the copy, not for the conversion, so the
11 //! prompt moves as it was written and the question is filed rather than
12 //! answered by a translation.
13 //!
14 //! The tone is not in question: the row goes.
15 //!
16 //! `DELETE /api/cart/{item}` is a plain API route the description layer does
17 //! not serve; the page already gives the row an id, so the control names it.
18 //! See [`Action::replaces`].
19 //!
20 //! Ids arrive as `impl Display` rather than `&str`: the caller is Askama and
21 //! the server's ids are typed newtypes over `Uuid`, so asking for a string
22 //! would push a `to_string()` into every call site to say what `format!`
23 //! already says here.
24
25 use std::fmt;
26
27 use makeover_layout::Tone;
28 use quasi_router::screen::Act;
29 use quasi_router::{Action, Node};
30
31 /// The remove button for one cart line.
32 #[must_use]
33 pub fn html(item_id: impl fmt::Display) -> String {
34 use quasi_axum::Serves as _;
35
36 let act = Act::new(
37 "Remove",
38 Action::delete(format!("/api/cart/{item_id}")).replacing(format!("cart-row-{item_id}")),
39 )
40 .tone(Tone::Danger)
41 .confirm("Remove this item from your cart?");
42
43 quasi_webview::Webview::new().fragment(&Node::Act(act))
44 }
45
46 #[cfg(test)]
47 mod tests {
48 #[test]
49 fn a_removal_names_its_route_its_row_and_its_prompt() {
50 let html = super::html("i7");
51
52 assert!(html.contains(r#"hx-delete="/api/cart/i7""#), "{html}");
53 assert!(html.contains(r##"hx-target="#cart-row-i7""##), "{html}");
54 assert!(
55 html.contains(r#"hx-confirm="Remove this item from your cart?""#),
56 "{html}"
57 );
58 }
59
60 #[test]
61 fn a_removal_says_it_is_dangerous() {
62 assert!(super::html("i7").contains(r#"data-tone="danger""#));
63 }
64 }
65