hardcoded toast

This commit is contained in:
Priec
2026-06-17 20:12:31 +02:00
parent 7601fc704d
commit 67fd364761
6 changed files with 46 additions and 7 deletions

View File

@@ -1,5 +1,8 @@
use crate::{controllers::i18n::current_lang, shared::money::format_price, models::products};
use axum::{http::HeaderMap, response::Redirect};
use axum::{
http::{HeaderMap, StatusCode},
response::Redirect,
};
use axum_extra::extract::cookie::{Cookie, CookieJar, SameSite};
use loco_rs::prelude::*;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
@@ -73,6 +76,7 @@ async fn published_product(ctx: &AppContext, id: i32) -> Result<Option<products:
async fn add(
jar: CookieJar,
State(ctx): State<AppContext>,
headers: HeaderMap,
Form(form): Form<AddForm>,
) -> Result<Response> {
let Some(product) = published_product(&ctx, form.product_id).await? else {
@@ -88,9 +92,21 @@ async fn add(
}
items.retain(|(_, qty)| *qty > 0);
format::render()
.cookies(&[cart_cookie(serialize_cart(&items))])?
.redirect("/cart")
let jar = jar.add(cart_cookie(serialize_cart(&items)));
// Adding to the cart should never navigate away: htmx requests get an empty
// 204 (the header cart badge updates client-side), and a no-JS submit goes
// back to the page the customer was on rather than to the basket.
if headers.contains_key("HX-Request") {
Ok((jar, StatusCode::NO_CONTENT).into_response())
} else {
let back = headers
.get("referer")
.and_then(|v| v.to_str().ok())
.unwrap_or("/shop")
.to_string();
Ok((jar, Redirect::to(&back)).into_response())
}
}
#[debug_handler]