From 3089ede6eb8c613c7095a3080b5741bbcaa1bf36 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 26 Mar 2025 12:03:31 +0100 Subject: [PATCH] Add the lang prefix to all links --- backend/src/services/recipe.rs | 7 ++++++- backend/src/services/user.rs | 8 ++++++-- backend/templates/base_with_header.html | 4 ++-- backend/templates/profile.html | 2 +- backend/templates/recipe_view.html | 2 +- frontend/src/lib.rs | 21 +++++++++++++++++++-- frontend/src/pages/recipe_edit.rs | 7 +++++-- 7 files changed, 40 insertions(+), 11 deletions(-) diff --git a/backend/src/services/recipe.rs b/backend/src/services/recipe.rs index 9f5c1f2..b05b461 100644 --- a/backend/src/services/recipe.rs +++ b/backend/src/services/recipe.rs @@ -21,7 +21,12 @@ pub async fn create( ) -> Result { if let Some(user) = user { let recipe_id = connection.create_recipe(user.id).await?; - Ok(Redirect::to(&format!("/recipe/edit/{}", recipe_id)).into_response()) + Ok(Redirect::to(&format!( + "/{}/recipe/edit/{}", + tr.current_lang_code(), + recipe_id + )) + .into_response()) } else { Ok(Html(MessageTemplate::new(tr.t(Sentence::NotLoggedIn), tr).render()?).into_response()) } diff --git a/backend/src/services/user.rs b/backend/src/services/user.rs index 2e82969..37fc389 100644 --- a/backend/src/services/user.rs +++ b/backend/src/services/user.rs @@ -343,7 +343,10 @@ pub async fn sign_in_post( )), db::user::SignInResult::Ok(token, _user_id) => { let cookie = Cookie::new(consts::COOKIE_AUTH_TOKEN_NAME, token); - Ok((jar.add(cookie), Redirect::to("/").into_response())) + Ok(( + jar.add(cookie), + Redirect::to(&format!("/{}/", tr.current_lang_code())).into_response(), + )) } } } @@ -353,6 +356,7 @@ pub async fn sign_in_post( #[debug_handler] pub async fn sign_out( State(connection): State, + Extension(tr): Extension, req: Request, ) -> Result<(CookieJar, Redirect)> { let mut jar = CookieJar::from_headers(req.headers()); @@ -361,7 +365,7 @@ pub async fn sign_out( jar = jar.remove(consts::COOKIE_AUTH_TOKEN_NAME); connection.sign_out(&token).await?; } - Ok((jar, Redirect::to("/"))) + Ok((jar, Redirect::to(&format!("/{}/", tr.current_lang_code())))) } /// RESET PASSWORD /// diff --git a/backend/templates/base_with_header.html b/backend/templates/base_with_header.html index 3e4433c..7525fab 100644 --- a/backend/templates/base_with_header.html +++ b/backend/templates/base_with_header.html @@ -8,7 +8,7 @@ {% match user %} {% when Some with (user) %} {{ tr.t(Sentence::CreateNewRecipe) }} - + {% if user.name == "" %} {{ user.email }} {% else %} @@ -16,7 +16,7 @@ {% endif %} / {{ tr.t(Sentence::SignOut) }} {% when None %} - {{ tr.t(Sentence::SignInMenu) }}/{{ tr.t(Sentence::SignUpMenu) }}/{{ tr.t(Sentence::LostPassword) }} + {{ tr.t(Sentence::SignInMenu) }}/{{ tr.t(Sentence::SignUpMenu) }}/{{ tr.t(Sentence::LostPassword) }} {% endmatch %} Edit + Edit {% endif %} {% endif %} diff --git a/frontend/src/lib.rs b/frontend/src/lib.rs index 11dba8d..53d1680 100644 --- a/frontend/src/lib.rs +++ b/frontend/src/lib.rs @@ -25,12 +25,19 @@ pub fn main() -> Result<(), JsValue> { let lang = utils::get_current_lang(); let location = window().location().pathname()?; + let path: Vec<&str> = location .split('/') .skip(1) .skip_while(|part| *part == lang) .collect(); + let mut location_without_lang = String::new(); + for part in &path { + location_without_lang.push('/'); + location_without_lang.push_str(part); + } + let is_user_logged = selector::("html") .dataset() .get("userLogged") @@ -54,10 +61,20 @@ pub fn main() -> Result<(), JsValue> { let select_language: HtmlSelectElement = by_id("select-website-language"); EventListener::new(&select_language.clone(), "input", move |_event| { let lang = select_language.value(); - let body = ron_api::SetLang { lang }; + let body = ron_api::SetLang { lang: lang.clone() }; + let location_without_lang = location_without_lang.clone(); spawn_local(async move { let _ = request::put::<(), _>("set_lang", body).await; - let _ = window().location().reload(); + + window() + .location() + .set_href(&format!( + "/{}{}{}", + lang, + location_without_lang, + window().location().search().unwrap().as_str() + )) + .unwrap(); }); }) .forget(); diff --git a/frontend/src/pages/recipe_edit.rs b/frontend/src/pages/recipe_edit.rs index 9fbf10e..d1da43b 100644 --- a/frontend/src/pages/recipe_edit.rs +++ b/frontend/src/pages/recipe_edit.rs @@ -16,7 +16,7 @@ use web_sys::{ use crate::{ modal_dialog, request, toast::{self, Level}, - utils::{SelectorExt, by_id, selector, selector_and_clone}, + utils::{SelectorExt, by_id, get_current_lang, selector, selector_and_clone}, }; pub fn setup_page(recipe_id: i64) { @@ -262,7 +262,10 @@ pub fn setup_page(recipe_id: i64) { { let body = ron_api::Id { id: recipe_id }; let _ = request::delete::<(), _>("recipe/remove", body).await; - window().location().set_href("/").unwrap(); + window() + .location() + .set_href(&format!("/{}/", get_current_lang())) + .unwrap(); } }); }) -- 2.49.0