Remplace « + article » par une recherche dans la liste
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
444319f9a5
commit
6b5eaa0ba6
4 files changed
+194
-43
No files matched your search
+1
-1
@@ -22,7 +22,7 @@ dotenvy = { version = "0.15", optional = true }
|
|||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
wasm-bindgen = { version = "=0.2.128", optional = true }
|
wasm-bindgen = { version = "=0.2.128", optional = true }
|
||||||
js-sys = { version = "0.3", 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 }
|
send_wrapper = { version = "0.6", optional = true }
|
||||||
console_error_panic_hook = { version = "0.1", optional = true }
|
console_error_panic_hook = { version = "0.1", optional = true }
|
||||||
|
|
||||||
|
|||||||
+21
-2
@@ -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! {
|
view! {
|
||||||
<main class="mx-auto flex min-h-dvh max-w-md flex-col px-4 pt-[max(1.5rem,env(safe-area-inset-top))] pb-32">
|
<main class="mx-auto flex min-h-dvh max-w-md flex-col px-4 pt-[max(1.5rem,env(safe-area-inset-top))] pb-32">
|
||||||
<header class="flex items-start justify-between gap-4">
|
<header class="flex items-start justify-between gap-4">
|
||||||
@@ -241,7 +251,7 @@ fn ListsView(user: User) -> impl IntoView {
|
|||||||
</Transition>
|
</Transition>
|
||||||
</main>
|
</main>
|
||||||
|
|
||||||
<ItemSheet list_id=active_id />
|
<ItemSheet list_id=active_id items=list_items />
|
||||||
<ListSheet />
|
<ListSheet />
|
||||||
<AccountSheet user />
|
<AccountSheet user />
|
||||||
}
|
}
|
||||||
@@ -319,12 +329,21 @@ fn ListsContent(
|
|||||||
let total = move || current.with(Vec::len);
|
let total = move || current.with(Vec::len);
|
||||||
let remaining = Signal::derive(move || to_get.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 edit_list = {
|
||||||
let list = list.clone();
|
let list = list.clone();
|
||||||
move |_| ctx.list_editor.set(Some(ListEditor::Edit(list.clone())))
|
move |_| ctx.list_editor.set(Some(ListEditor::Edit(list.clone())))
|
||||||
};
|
};
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
|
<div class=format!("fixed inset-0 -z-10 {}", color.dot_class()) aria-hidden="true"></div>
|
||||||
<ListTabs lists active=Some(active_id) remaining />
|
<ListTabs lists active=Some(active_id) remaining />
|
||||||
|
|
||||||
<section class="ticket relative mt-6 bg-paper px-5 pt-6 pb-8">
|
<section class="ticket relative mt-6 bg-paper px-5 pt-6 pb-8">
|
||||||
@@ -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"
|
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))
|
on:click=move |_| ctx.item_editor.set(Some(ItemEditor::New))
|
||||||
>
|
>
|
||||||
"+ article"
|
"ajouter"
|
||||||
</button>
|
</button>
|
||||||
}
|
}
|
||||||
.into_any()
|
.into_any()
|
||||||
|
|||||||
+161
-39
@@ -5,7 +5,7 @@ use leptos_router::hooks::use_navigate;
|
|||||||
use super::Ctx;
|
use super::Ctx;
|
||||||
use super::icons::{IconSvg, icon_label};
|
use super::icons::{IconSvg, icon_label};
|
||||||
use crate::api::get_members;
|
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)]
|
#[derive(Clone, Debug, PartialEq)]
|
||||||
pub enum ItemEditor {
|
pub enum ItemEditor {
|
||||||
@@ -42,36 +42,177 @@ pub fn Sheet(title: String, on_close: impl Fn() + Copy + Send + Sync + 'static,
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
pub fn ItemSheet(#[prop(into)] list_id: Signal<Option<i64>>) -> impl IntoView {
|
pub fn ItemSheet(
|
||||||
|
#[prop(into)] list_id: Signal<Option<i64>>,
|
||||||
|
/// Items of the open list, searched by the add sheet.
|
||||||
|
items: Memo<Vec<Item>>,
|
||||||
|
) -> impl IntoView {
|
||||||
let ctx = expect_context::<Ctx>();
|
let ctx = expect_context::<Ctx>();
|
||||||
move || {
|
move || {
|
||||||
let editor = ctx.item_editor.get()?;
|
let editor = ctx.item_editor.get()?;
|
||||||
let list_id = list_id.get()?;
|
let list_id = list_id.get()?;
|
||||||
Some(view! { <ItemForm editor list_id /> })
|
Some(match editor {
|
||||||
|
ItemEditor::New => view! { <AddForm list_id items /> }.into_any(),
|
||||||
|
ItemEditor::Edit(item) => view! { <ItemForm item /> }.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<Vec<Item>>) -> impl IntoView {
|
||||||
|
let ctx = expect_context::<Ctx>();
|
||||||
|
let close = move || ctx.item_editor.set(None);
|
||||||
|
let query = RwSignal::new(String::new());
|
||||||
|
let input = NodeRef::<leptos::html::Input>::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::<Vec<_>>()
|
||||||
|
})
|
||||||
|
});
|
||||||
|
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! {
|
||||||
|
<Sheet title="Ajouter".to_owned() on_close=close>
|
||||||
|
<form class="mt-4 flex flex-col gap-4" on:submit=submit>
|
||||||
|
<input
|
||||||
|
class="field"
|
||||||
|
type="search"
|
||||||
|
enterkeyhint="done"
|
||||||
|
maxlength="60"
|
||||||
|
placeholder="Avocats, pain…"
|
||||||
|
aria-label="Article"
|
||||||
|
node_ref=input
|
||||||
|
bind:value=query
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Show when=move || !matches.with(Vec::is_empty)>
|
||||||
|
<ul class="grid grid-cols-3 gap-3">
|
||||||
|
<For
|
||||||
|
each=move || matches.get()
|
||||||
|
key=|item| (item.id, item.checked, item.name.clone(), item.icon)
|
||||||
|
let:item
|
||||||
|
>
|
||||||
|
<SearchResult item on_pick=refocus />
|
||||||
|
</For>
|
||||||
|
</ul>
|
||||||
|
</Show>
|
||||||
|
|
||||||
|
<Show when=can_add>
|
||||||
|
<button
|
||||||
|
type="submit"
|
||||||
|
class="tile-shadow rounded-2xl border-2 border-ink bg-pink px-4 py-3 text-left font-display text-lg font-bold active:translate-y-1 active:shadow-none"
|
||||||
|
>
|
||||||
|
{move || format!("+ Ajouter « {} »", query.read().trim())}
|
||||||
|
</button>
|
||||||
|
</Show>
|
||||||
|
</form>
|
||||||
|
</Sheet>
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// 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::<Ctx>();
|
||||||
|
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! {
|
||||||
|
<li>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
class=format!("flex aspect-square w-full flex-col items-center gap-1 rounded-2xl border-2 p-2 pb-2.5 text-center {class}")
|
||||||
|
aria-label=aria_label
|
||||||
|
aria-pressed=selected.to_string()
|
||||||
|
on:click=move |_| {
|
||||||
|
ctx.change(ItemOp::SetChecked { id, checked: selected });
|
||||||
|
on_pick();
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<IconSvg icon class="min-h-0 w-full flex-1" />
|
||||||
|
<span class=format!("line-clamp-2 text-[0.95rem] leading-tight break-words hyphens-auto {label_class}")>
|
||||||
|
{name}
|
||||||
|
</span>
|
||||||
|
</button>
|
||||||
|
</li>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView {
|
fn ItemForm(item: Item) -> impl IntoView {
|
||||||
let ctx = expect_context::<Ctx>();
|
let ctx = expect_context::<Ctx>();
|
||||||
let close = move || ctx.item_editor.set(None);
|
let close = move || ctx.item_editor.set(None);
|
||||||
|
|
||||||
let existing = match &editor {
|
let name = RwSignal::new(item.name.clone());
|
||||||
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 amount = RwSignal::new(
|
let amount = RwSignal::new(
|
||||||
existing
|
item.quantity
|
||||||
.as_ref()
|
|
||||||
.and_then(|i| i.quantity)
|
|
||||||
.map(|q| q.amount.to_string().replace('.', ","))
|
.map(|q| q.amount.to_string().replace('.', ","))
|
||||||
.unwrap_or_default(),
|
.unwrap_or_default(),
|
||||||
);
|
);
|
||||||
let unit = RwSignal::new(existing.as_ref().and_then(|i| i.quantity).map_or(Unit::Piece, |q| q.unit));
|
let unit = RwSignal::new(item.quantity.map_or(Unit::Piece, |q| q.unit));
|
||||||
let icon = RwSignal::new(existing.as_ref().map_or(Icon::Basket, |i| i.icon));
|
let icon = RwSignal::new(item.icon);
|
||||||
let category = RwSignal::new(existing.as_ref().map_or(Category::Autre, |i| i.category));
|
let category = RwSignal::new(item.category);
|
||||||
let item_id = existing.as_ref().map(|i| i.id);
|
let item_id = item.id;
|
||||||
|
|
||||||
let invalid_amount = Memo::new(move |_| amount.with(|a| !a.trim().is_empty() && parse_amount(a).is_none()));
|
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(),
|
icon: icon.get(),
|
||||||
category: category.get(),
|
category: category.get(),
|
||||||
};
|
};
|
||||||
ctx.change(match item_id {
|
ctx.change(ItemOp::Update { id: item_id, input });
|
||||||
Some(id) => ItemOp::Update { id, input },
|
|
||||||
None => ItemOp::Add { id: 0, list_id, input },
|
|
||||||
});
|
|
||||||
close();
|
close();
|
||||||
};
|
};
|
||||||
let delete = move |_| {
|
let delete = move |_| {
|
||||||
if let Some(id) = item_id {
|
ctx.change(ItemOp::Delete { id: item_id });
|
||||||
ctx.change(ItemOp::Delete { id });
|
|
||||||
close();
|
close();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
let title = if item_id.is_some() { "Modifier l'article" } else { "Nouvel article" };
|
|
||||||
|
|
||||||
view! {
|
view! {
|
||||||
<Sheet title=title.to_owned() on_close=close>
|
<Sheet title="Modifier l'article".to_owned() on_close=close>
|
||||||
<form class="mt-4 flex flex-col gap-5" on:submit=submit>
|
<form class="mt-4 flex flex-col gap-5" on:submit=submit>
|
||||||
<label class="flex flex-col gap-1.5">
|
<label class="flex flex-col gap-1.5">
|
||||||
<span class="font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">"Nom"</span>
|
<span class="font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">"Nom"</span>
|
||||||
@@ -110,7 +244,6 @@ fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView {
|
|||||||
class="field"
|
class="field"
|
||||||
required
|
required
|
||||||
maxlength="60"
|
maxlength="60"
|
||||||
autofocus
|
|
||||||
placeholder="Avocats"
|
placeholder="Avocats"
|
||||||
bind:value=name
|
bind:value=name
|
||||||
/>
|
/>
|
||||||
@@ -184,10 +317,6 @@ fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView {
|
|||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
|
|
||||||
// New items keep the basket: the icon can be changed when editing.
|
|
||||||
{item_id
|
|
||||||
.map(|_| {
|
|
||||||
view! {
|
|
||||||
<fieldset class="flex flex-col gap-1.5">
|
<fieldset class="flex flex-col gap-1.5">
|
||||||
<legend class="mb-1.5 font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">
|
<legend class="mb-1.5 font-mono text-xs tracking-[0.2em] text-ink/60 uppercase">
|
||||||
"Icône"
|
"Icône"
|
||||||
@@ -221,13 +350,8 @@ fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView {
|
|||||||
.collect_view()}
|
.collect_view()}
|
||||||
</div>
|
</div>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
}
|
|
||||||
})}
|
|
||||||
|
|
||||||
<div class="flex gap-3">
|
<div class="flex gap-3">
|
||||||
{item_id
|
|
||||||
.map(|_| {
|
|
||||||
view! {
|
|
||||||
<button
|
<button
|
||||||
type="button"
|
type="button"
|
||||||
class="rounded-2xl border-2 border-ink px-4 py-3 font-semibold"
|
class="rounded-2xl border-2 border-ink px-4 py-3 font-semibold"
|
||||||
@@ -235,13 +359,11 @@ fn ItemForm(editor: ItemEditor, list_id: i64) -> impl IntoView {
|
|||||||
>
|
>
|
||||||
"Supprimer"
|
"Supprimer"
|
||||||
</button>
|
</button>
|
||||||
}
|
|
||||||
})}
|
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
class="tile-shadow flex-1 rounded-2xl border-2 border-ink bg-pink px-4 py-3 font-display text-lg font-bold active:translate-y-1 active:shadow-none"
|
class="tile-shadow flex-1 rounded-2xl border-2 border-ink bg-pink px-4 py-3 font-display text-lg font-bold active:translate-y-1 active:shadow-none"
|
||||||
>
|
>
|
||||||
{if item_id.is_some() { "Enregistrer" } else { "Ajouter" }}
|
"Enregistrer"
|
||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|||||||
+11
-1
@@ -95,6 +95,16 @@ impl ListColor {
|
|||||||
ListColor::Peach => "bg-peach",
|
ListColor::Peach => "bg-peach",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Same color as `dot_class`, for the browser's `theme-color`.
|
||||||
|
pub fn hex(self) -> &'static str {
|
||||||
|
match self {
|
||||||
|
ListColor::Lime => "#bfe383",
|
||||||
|
ListColor::Pink => "#ffadd6",
|
||||||
|
ListColor::Sky => "#7fdffb",
|
||||||
|
ListColor::Peach => "#ffbd76",
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
@@ -117,7 +127,7 @@ impl Item {
|
|||||||
|
|
||||||
/// Lowercases and strips French accents so "Œufs" sorts with "o" and
|
/// Lowercases and strips French accents so "Œufs" sorts with "o" and
|
||||||
/// "Épinards" with "e".
|
/// "Épinards" with "e".
|
||||||
fn fold_name(name: &str) -> String {
|
pub fn fold_name(name: &str) -> String {
|
||||||
let mut folded = String::with_capacity(name.len());
|
let mut folded = String::with_capacity(name.len());
|
||||||
for c in name.chars().flat_map(char::to_lowercase) {
|
for c in name.chars().flat_map(char::to_lowercase) {
|
||||||
match c {
|
match c {
|
||||||
|
|||||||
Reference in new issue
Block a user