From 6b5eaa0ba647408d10ea952c28597f8d6ce6d2ad Mon Sep 17 00:00:00 2001 From: Romulus21 Date: Thu, 5 Nov 2026 22:30:57 +0100 Subject: [PATCH] =?UTF-8?q?Remplace=20=C2=AB=20+=20article=20=C2=BB=20par?= =?UTF-8?q?=20une=20recherche=20dans=20la=20liste?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Le bouton « ajouter » ouvre un champ unique : il retrouve les articles de la liste et du panier, un appui les fait passer de l'un à l'autre, et un bouton ajoute le nom saisi s'il n'existe pas encore. Le fond de la page et la couleur de la barre d'état prennent aussi la couleur de la liste ouverte. Co-Authored-By: Claude Opus 5.5 --- Cargo.toml | 2 +- src/app/mod.rs | 23 +++- src/app/sheets.rs | 282 +++++++++++++++++++++++++++++++++------------- src/model.rs | 12 +- 4 files changed, 235 insertions(+), 84 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a0fd924..b6aa36b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ dotenvy = { version = "0.15", optional = true } serde = { version = "1", features = ["derive"] } wasm-bindgen = { version = "=0.2.128", optional = true } js-sys = { version = "0.3", optional = true } -web-sys = { version = "0.3", features = ["CloseEvent", "Document", "Location", "MessageEvent", "VisibilityState", "WebSocket", "Window"], optional = true } +web-sys = { version = "0.3", features = ["CloseEvent", "Document", "Element", "Location", "MessageEvent", "VisibilityState", "WebSocket", "Window"], optional = true } send_wrapper = { version = "0.6", optional = true } console_error_panic_hook = { version = "0.1", optional = true } diff --git a/src/app/mod.rs b/src/app/mod.rs index dc39003..50cb331 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -196,6 +196,16 @@ fn ListsView(user: User) -> impl IntoView { }, ); + // Items of the open list with unsaved changes, for the add sheet. + let list_items = Memo::new(move |_| match (active_id.get(), items.get()) { + (Some(id), Some(Ok((items, _)))) => { + let mut items = ctx.sync.apply(id, &items); + items.sort_by_cached_key(|item| crate::model::fold_name(&item.name)); + items + } + _ => Vec::new(), + }); + view! {
@@ -241,7 +251,7 @@ fn ListsView(user: User) -> impl IntoView {
- + } @@ -319,12 +329,21 @@ fn ListsContent( let total = move || current.with(Vec::len); let remaining = Signal::derive(move || to_get.with(Vec::len)); + // The page takes the color of the open list. + let color = list.color; + Effect::new(move |_| { + if let Ok(Some(meta)) = document().query_selector("meta[name=theme-color]") { + _ = meta.set_attribute("content", color.hex()); + } + }); + let edit_list = { let list = list.clone(); move |_| ctx.list_editor.set(Some(ListEditor::Edit(list.clone()))) }; view! { +
@@ -382,7 +401,7 @@ fn ListsContent( class="tile-shadow fixed right-[max(1rem,calc(50vw-14rem+1rem))] bottom-[max(1.25rem,env(safe-area-inset-bottom))] z-10 rounded-2xl border-2 border-ink bg-pink px-6 py-4 font-display text-xl font-bold active:translate-y-1 active:shadow-none" on:click=move |_| ctx.item_editor.set(Some(ItemEditor::New)) > - "+ article" + "ajouter" } .into_any() diff --git a/src/app/sheets.rs b/src/app/sheets.rs index 4b3c671..a431f75 100644 --- a/src/app/sheets.rs +++ b/src/app/sheets.rs @@ -5,7 +5,7 @@ use leptos_router::hooks::use_navigate; use super::Ctx; use super::icons::{IconSvg, icon_label}; use crate::api::get_members; -use crate::model::{Category, Icon, Item, ItemInput, ItemOp, ListColor, Quantity, ShoppingList, Unit}; +use crate::model::{Category, Icon, Item, ItemInput, ItemOp, ListColor, Quantity, ShoppingList, Unit, fold_name}; #[derive(Clone, Debug, PartialEq)] pub enum ItemEditor { @@ -42,36 +42,177 @@ pub fn Sheet(title: String, on_close: impl Fn() + Copy + Send + Sync + 'static, } #[component] -pub fn ItemSheet(#[prop(into)] list_id: Signal>) -> impl IntoView { +pub fn ItemSheet( + #[prop(into)] list_id: Signal>, + /// Items of the open list, searched by the add sheet. + items: Memo>, +) -> impl IntoView { let ctx = expect_context::(); move || { let editor = ctx.item_editor.get()?; let list_id = list_id.get()?; - Some(view! { }) + Some(match editor { + ItemEditor::New => view! { }.into_any(), + ItemEditor::Edit(item) => view! { }.into_any(), + }) + } +} + +/// Search box over the list: a match is tapped to put it on the list or in +/// the basket, and an unknown name is added. +#[component] +fn AddForm(list_id: i64, items: Memo>) -> impl IntoView { + let ctx = expect_context::(); + let close = move || ctx.item_editor.set(None); + let query = RwSignal::new(String::new()); + let input = NodeRef::::new(); + let refocus = move || { + query.set(String::new()); + if let Some(input) = input.get_untracked() { + _ = input.focus(); + } + }; + // `autofocus` is ignored on elements added after page load. + Effect::new(move |_| { + if let Some(input) = input.get() { + _ = input.focus(); + } + }); + + let needle = Memo::new(move |_| fold_name(query.read().trim())); + let matches = Memo::new(move |_| { + let needle = needle.get(); + if needle.is_empty() { + return Vec::new(); + } + items.with(|items| { + items + .iter() + .filter(|item| fold_name(&item.name).contains(&needle)) + .cloned() + .collect::>() + }) + }); + let exact = move || { + let needle = needle.get(); + matches.with(|items| items.iter().find(|item| fold_name(&item.name) == needle).cloned()) + }; + let can_add = move || !needle.with(String::is_empty) && exact().is_none(); + + let submit = move |ev: ev::SubmitEvent| { + ev.prevent_default(); + match exact() { + // Enter on a name already in the basket puts it back on the list. + Some(item) => { + if item.checked { + ctx.change(ItemOp::SetChecked { id: item.id, checked: false }); + } + } + None if can_add() => { + let input = ItemInput { + name: query.get_untracked().trim().to_owned(), + quantity: None, + icon: Icon::Basket, + category: Category::Autre, + }; + ctx.change(ItemOp::Add { id: 0, list_id, input }); + } + None => return, + } + refocus(); + }; + + view! { + +
+ + + +
    + + + +
+
+ + + + +
+
+ } +} + +/// A list item found by the search: tap moves it between the list and the basket. +#[component] +fn SearchResult(item: Item, on_pick: impl Fn() + Copy + Send + Sync + 'static) -> impl IntoView { + let ctx = expect_context::(); + let selected = !item.checked; + let (id, icon, name) = (item.id, item.icon, item.name); + // Same looks as the tiles of the list and of the basket. + let (class, label_class) = if selected { + ( + format!("tile-shadow border-ink active:translate-y-1 active:shadow-none {}", item.category.tile_class()), + "font-semibold", + ) + } else { + ("border-transparent bg-mist text-ink/45 active:bg-ink/10".to_owned(), "font-medium line-through decoration-ink/30") + }; + let aria_label = format!("{name} : {}", if selected { "mettre dans le panier" } else { "remettre dans la liste" }); + view! { +
  • + +
  • } } #[component] -fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView { +fn ItemForm(item: Item) -> impl IntoView { let ctx = expect_context::(); let close = move || ctx.item_editor.set(None); - let existing = match &editor { - ItemEditor::Edit(item) => Some(item.clone()), - ItemEditor::New => None, - }; - let name = RwSignal::new(existing.as_ref().map(|i| i.name.clone()).unwrap_or_default()); + let name = RwSignal::new(item.name.clone()); let amount = RwSignal::new( - existing - .as_ref() - .and_then(|i| i.quantity) + item.quantity .map(|q| q.amount.to_string().replace('.', ",")) .unwrap_or_default(), ); - let unit = RwSignal::new(existing.as_ref().and_then(|i| i.quantity).map_or(Unit::Piece, |q| q.unit)); - let icon = RwSignal::new(existing.as_ref().map_or(Icon::Basket, |i| i.icon)); - let category = RwSignal::new(existing.as_ref().map_or(Category::Autre, |i| i.category)); - let item_id = existing.as_ref().map(|i| i.id); + let unit = RwSignal::new(item.quantity.map_or(Unit::Piece, |q| q.unit)); + let icon = RwSignal::new(item.icon); + let category = RwSignal::new(item.category); + let item_id = item.id; let invalid_amount = Memo::new(move |_| amount.with(|a| !a.trim().is_empty() && parse_amount(a).is_none())); @@ -86,23 +227,16 @@ fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView { icon: icon.get(), category: category.get(), }; - ctx.change(match item_id { - Some(id) => ItemOp::Update { id, input }, - None => ItemOp::Add { id: 0, list_id, input }, - }); + ctx.change(ItemOp::Update { id: item_id, input }); close(); }; let delete = move |_| { - if let Some(id) = item_id { - ctx.change(ItemOp::Delete { id }); - close(); - } + ctx.change(ItemOp::Delete { id: item_id }); + close(); }; - let title = if item_id.is_some() { "Modifier l'article" } else { "Nouvel article" }; - view! { - +