removing handwritten JS

This commit is contained in:
Priec
2026-05-16 22:00:39 +02:00
parent 4938314889
commit 86c18c552d
2 changed files with 67 additions and 41 deletions

View File

@@ -6,6 +6,8 @@
//! court. Booked slots are coloured; free slots are blank. The same grid is
//! reused by the admin dashboard with `is_admin = true`.
use axum::http::{header, HeaderMap};
use axum::response::Redirect;
use axum_extra::extract::cookie::CookieJar;
use chrono::{Datelike, Duration, NaiveDate, Utc};
use loco_rs::prelude::*;
@@ -312,6 +314,46 @@ pub async fn index(
format::render().view(&v, "calendar/week.html", &page)
}
pub fn routes() -> Routes {
Routes::new().add("/", get(index))
#[derive(Debug, Deserialize)]
pub struct LangForm {
pub lang: String,
}
/// Switches the UI language. The navbar's language buttons post here; the
/// `lang` cookie is set server-side and the visitor is bounced back to the
/// page they came from. This replaces the old client-side `setLang` script.
#[debug_handler]
pub async fn set_lang(headers: HeaderMap, Form(form): Form<LangForm>) -> Result<Response> {
// Only the two supported languages; anything else falls back to Slovak,
// matching `current_lang`.
let lang = if form.lang == "en" { "en" } else { "sk" };
let cookie = format!("lang={lang}; Path=/; Max-Age=31536000; SameSite=Lax");
Ok((
[(header::SET_COOKIE, cookie)],
Redirect::to(&back_path(&headers)),
)
.into_response())
}
/// On-site path of the page that submitted the form, read from `Referer`.
/// Scheme and host are stripped so a stale or foreign header can only ever
/// bounce the visitor to a path on this site, never off it.
fn back_path(headers: &HeaderMap) -> String {
let raw = headers
.get(header::REFERER)
.and_then(|v| v.to_str().ok())
.unwrap_or("/");
match raw.split_once("://") {
Some((_, rest)) => match rest.find('/') {
Some(i) => rest[i..].to_string(),
None => "/".to_string(),
},
None => raw.to_string(),
}
}
pub fn routes() -> Routes {
Routes::new()
.add("/", get(index))
.add("/lang", post(set_lang))
}