Update dependencies, add translation endpoint, and display a user message when logged in

This commit is contained in:
Greg Burri 2025-05-05 01:59:30 +02:00
parent 6b043620b8
commit 348b0f24e9
19 changed files with 170 additions and 34 deletions

View file

@ -36,6 +36,7 @@ web-sys = { version = "0.3", features = [
"DataTransfer",
"DomRect",
"KeyboardEvent",
"History",
"Element",
"DomStringMap",
"HtmlDocument",

View file

@ -65,6 +65,44 @@ pub fn main() -> Result<(), JsValue> {
_ => log!("Path unknown: ", location),
}
// User message handling.
let user_message_params = utils::extract_get_parameters(&[
common::consts::GET_PARAMETER_USER_MESSAGE,
common::consts::GET_PARAMETER_USER_MESSAGE_LEVEL,
]);
if let Some(mess_id) = user_message_params.iter().find_map(|(k, v)| {
if k == common::consts::GET_PARAMETER_USER_MESSAGE {
v.parse::<i64>().ok()
} else {
None
}
}) {
let level_id = user_message_params.iter().find_map(|(k, v)| {
if k == common::consts::GET_PARAMETER_USER_MESSAGE_LEVEL {
v.parse::<usize>().ok()
} else {
None
}
});
// Request the message to display.
spawn_local(async move {
let translation: ron_api::Value<String> =
request::get("translation", ron_api::Id { id: mess_id })
.await
.unwrap();
if let Some(level_id) = level_id {
toast::show_message_level(
common::toast::Level::from_repr(level_id)
.unwrap_or(common::toast::Level::Unknown),
&translation.value,
);
} else {
toast::show_message(&translation.value);
}
});
}
// Language handling.
let select_language: HtmlSelectElement = by_id("select-website-language");
EventListener::new(&select_language.clone(), "input", move |_event| {

View file

@ -39,7 +39,7 @@ impl ShoppingList {
} else {
request::patch(
"shopping_list/checked",
ron_api::Value {
ron_api::KeyValue {
id: item_id,
value: is_checked,
},

View file

@ -1,16 +1,9 @@
pub use common::toast::Level;
use gloo::{events::EventListener, timers::callback::Timeout};
use web_sys::{Element, HtmlElement, HtmlImageElement};
use crate::utils::{SelectorExt, by_id, selector_and_clone};
pub enum Level {
Success,
Error,
Info,
Warning,
Unknown,
}
const TIME_ANIMATION: u32 = 500; // [ms].
const TIME_DISPLAYED: u32 = 5_000; // [ms].

View file

@ -1,7 +1,7 @@
use std::str::FromStr;
use chrono::Locale;
use gloo::utils::document;
use gloo::utils::{document, window};
use wasm_bindgen::prelude::*;
use web_sys::Element;
@ -133,3 +133,45 @@ pub fn get_locale() -> Locale {
.replace("-", "_");
Locale::from_str(&lang_and_territory).unwrap_or_default()
}
/// Extracts and remove some URL parameters from the current URL.
pub fn extract_get_parameters(names: &[&str]) -> Vec<(String, String)> {
let mut param_values = vec![];
let location = window().location();
if let Ok(search) = location.search() {
if !search.is_empty() && search.starts_with('?') {
let mut search_chars = search.chars();
search_chars.next(); // To remove the first character which is '?'.
let mut new_search = String::with_capacity(search.len());
for kv in search_chars.as_str().split('&') {
match kv.split('=').collect::<Vec<&str>>()[..] {
[key, value] if names.contains(&key) => {
param_values.push((key.to_string(), value.to_string()));
}
_ => {
if !new_search.is_empty() {
new_search.push('&');
}
new_search.push_str(kv);
}
}
}
if !param_values.is_empty() {
let mut new_url = location.pathname().unwrap();
if !new_search.is_empty() {
new_url.push('?');
new_url.push_str(&new_search);
}
window()
.history()
.unwrap()
.push_state_with_url(&JsValue::null(), "", Some(&new_url))
.unwrap();
}
}
}
param_values
}