Add the lang prefix to all links
authorGreg Burri <greg.burri@gmail.com>
Wed, 26 Mar 2025 11:03:31 +0000 (12:03 +0100)
committerGreg Burri <greg.burri@gmail.com>
Wed, 26 Mar 2025 11:03:31 +0000 (12:03 +0100)
backend/src/services/recipe.rs
backend/src/services/user.rs
backend/templates/base_with_header.html
backend/templates/profile.html
backend/templates/recipe_view.html
frontend/src/lib.rs
frontend/src/pages/recipe_edit.rs

index 9f5c1f2..b05b461 100644 (file)
@@ -21,7 +21,12 @@ pub async fn create(
 ) -> Result<Response> {
     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())
     }
index 2e82969..37fc389 100644 (file)
@@ -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<db::Connection>,
+    Extension(tr): Extension<translation::Tr>,
     req: Request<Body>,
 ) -> 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 ///
index 3e4433c..7525fab 100644 (file)
@@ -8,7 +8,7 @@
             {% match user %}
             {% when Some with (user) %}
                 <a class="create-recipe" href="/recipe/new" >{{ tr.t(Sentence::CreateNewRecipe) }}</a>
-                <a href="/user/edit">
+                <a href="/{{ tr.current_lang_code() }}/user/edit">
                     {% if user.name == "" %}
                         {{ user.email }}
                     {% else %}
@@ -16,7 +16,7 @@
                     {% endif %}
                 </a> / <a href="/signout">{{ tr.t(Sentence::SignOut) }}</a>
             {% when None %}
-                <a href="/signin" >{{ tr.t(Sentence::SignInMenu) }}</a>/<a href="/signup">{{ tr.t(Sentence::SignUpMenu) }}</a>/<a href="/ask_reset_password">{{ tr.t(Sentence::LostPassword) }}</a>
+                <a href="/{{ tr.current_lang_code() }}/signin" >{{ tr.t(Sentence::SignInMenu) }}</a>/<a href="/{{ tr.current_lang_code() }}/signup">{{ tr.t(Sentence::SignUpMenu) }}</a>/<a href="/{{ tr.current_lang_code() }}/ask_reset_password">{{ tr.t(Sentence::LostPassword) }}</a>
             {% endmatch %}
 
             <select id="select-website-language">
index a140640..4623144 100644 (file)
@@ -7,7 +7,7 @@
 <div class="content" id="user-edit">
     <h1>{{ tr.t(Sentence::ProfileTitle) }}</h1>
 
-    <form action="/user/edit" method="post">
+    <form action="/{{ tr.current_lang_code() }}/user/edit" method="post">
 
         <label for="input-name">{{ tr.t(Sentence::Name) }}</label>
         <input
index 4a0956d..86a4ddd 100644 (file)
@@ -7,7 +7,7 @@
 
     {% if let Some(user) = user %}
         {% if crate::data::model::can_user_edit_recipe(user, recipe) %}
-            <a class="edit-recipe" href="/recipe/edit/{{ recipe.id }}" >Edit</a>
+            <a class="edit-recipe" href="/{{ tr.current_lang_code() }}/recipe/edit/{{ recipe.id }}" >Edit</a>
         {% endif %}
     {% endif %}
 
index 11dba8d..53d1680 100644 (file)
@@ -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::<HtmlElement>("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();
index 9fbf10e..d1da43b 100644 (file)
@@ -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();
             }
         });
     })