40 lines
1.2 KiB
Rust
40 lines
1.2 KiB
Rust
//! Storefront display-currency switcher.
|
|
//!
|
|
//! Sets the `currency` cookie to the buyer's chosen display currency, then sends
|
|
//! them back where they were. EUR is the base; any other code must name an
|
|
//! enabled row in `currencies` or it falls back to EUR on the next render.
|
|
|
|
use axum::{
|
|
http::{header, HeaderMap},
|
|
response::Redirect,
|
|
};
|
|
use loco_rs::prelude::*;
|
|
use serde::Deserialize;
|
|
|
|
use crate::controllers::i18n::back_path;
|
|
use crate::shared::currency::{BASE_CODE, COOKIE};
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub struct CurrencyForm {
|
|
pub currency: String,
|
|
}
|
|
|
|
#[debug_handler]
|
|
async fn set_currency(headers: HeaderMap, Form(form): Form<CurrencyForm>) -> Result<Response> {
|
|
// Store the code uppercased; validation against the enabled set happens at
|
|
// render time (shared::currency::resolve), which falls back to EUR.
|
|
let code = form.currency.trim().to_uppercase();
|
|
let code = if code.is_empty() { BASE_CODE.to_string() } else { code };
|
|
let cookie = format!("{COOKIE}={code}; Path=/; Max-Age=31536000; SameSite=Lax");
|
|
|
|
Ok((
|
|
[(header::SET_COOKIE, cookie)],
|
|
Redirect::to(&back_path(&headers)),
|
|
)
|
|
.into_response())
|
|
}
|
|
|
|
pub fn routes() -> Routes {
|
|
Routes::new().add("/currency", post(set_currency))
|
|
}
|