126 lines
4.6 KiB
Rust
126 lines
4.6 KiB
Rust
use leptos::ev;
|
|
use leptos::prelude::*;
|
|
|
|
use super::icons::IconSvg;
|
|
use super::{Ctx, ItemEditor};
|
|
use crate::model::{Item, ItemInput, Suggestion};
|
|
|
|
const LONG_PRESS_MS: u64 = 450;
|
|
|
|
/// Item still to buy: tap moves it to the basket, long press opens the editor.
|
|
#[component]
|
|
pub fn ItemTile(item: Item) -> impl IntoView {
|
|
let ctx = expect_context::<Ctx>();
|
|
let item = StoredValue::new(item);
|
|
let timer = StoredValue::new(None::<TimeoutHandle>);
|
|
// Set once the long press fired, so the following click is ignored.
|
|
let long_pressed = StoredValue::new(false);
|
|
|
|
let open_editor = move || {
|
|
long_pressed.set_value(true);
|
|
ctx.item_editor.set(Some(ItemEditor::Edit(item.get_value())));
|
|
};
|
|
let cancel_timer = move |_| {
|
|
if let Some(handle) = timer.get_value() {
|
|
handle.clear();
|
|
timer.set_value(None);
|
|
}
|
|
};
|
|
let start_timer = move |_: ev::PointerEvent| {
|
|
long_pressed.set_value(false);
|
|
let handle = set_timeout_with_handle(open_editor, std::time::Duration::from_millis(LONG_PRESS_MS)).ok();
|
|
timer.set_value(handle);
|
|
};
|
|
let on_click = move |_| {
|
|
if long_pressed.get_value() {
|
|
long_pressed.set_value(false);
|
|
} else {
|
|
item.with_value(|item| ctx.toggle(item));
|
|
}
|
|
};
|
|
let on_context_menu = move |ev: ev::MouseEvent| {
|
|
ev.prevent_default();
|
|
cancel_timer(());
|
|
open_editor();
|
|
};
|
|
|
|
let (name, icon, category, badge) = item.with_value(|item| {
|
|
(item.name.clone(), item.icon, item.category, item.quantity.map(|q| q.label()))
|
|
});
|
|
let aria_label = format!("{name} : mettre dans le panier");
|
|
|
|
view! {
|
|
<button
|
|
class=format!(
|
|
"tile-shadow relative flex aspect-square touch-manipulation flex-col items-center gap-1 rounded-2xl border-2 border-ink p-2 pb-2.5 text-center select-none active:translate-y-1 active:shadow-none [-webkit-touch-callout:none] {}",
|
|
category.tile_class(),
|
|
)
|
|
aria-label=aria_label
|
|
on:click=on_click
|
|
on:contextmenu=on_context_menu
|
|
on:pointerdown=start_timer
|
|
on:pointerup=move |_| cancel_timer(())
|
|
on:pointerleave=move |_| cancel_timer(())
|
|
on:pointercancel=move |_| cancel_timer(())
|
|
>
|
|
{badge
|
|
.map(|badge| {
|
|
view! {
|
|
<span class="absolute top-2 right-2 rounded-lg bg-ink px-1.5 py-0.5 font-mono text-xs text-paper">
|
|
{badge}
|
|
</span>
|
|
}
|
|
})}
|
|
<IconSvg icon class="min-h-0 w-full flex-1 text-ink" />
|
|
<span class="line-clamp-2 text-[0.95rem] leading-tight font-semibold break-words hyphens-auto">
|
|
{name}
|
|
</span>
|
|
</button>
|
|
}
|
|
}
|
|
|
|
/// Dashed "+ Beurre" tile: one tap adds a frequently bought product.
|
|
#[component]
|
|
pub fn SuggestionTile(suggestion: Suggestion, list_id: i64) -> impl IntoView {
|
|
let ctx = expect_context::<Ctx>();
|
|
let Suggestion { name, icon, category } = suggestion;
|
|
let label = format!("+ {name}");
|
|
|
|
let add = move |_| {
|
|
let input = ItemInput { name: name.clone(), quantity: None, icon, category };
|
|
ctx.mutate(crate::api::add_item(list_id, input), |_| ());
|
|
};
|
|
|
|
view! {
|
|
<button
|
|
class="relative flex aspect-square flex-col items-center gap-1 rounded-2xl border-2 border-dashed border-ink/35 p-2 pb-2.5 text-center text-ink/55 active:bg-ink/5"
|
|
on:click=add
|
|
>
|
|
<IconSvg icon class="min-h-0 w-full flex-1 opacity-60" />
|
|
<span class="line-clamp-2 text-[0.95rem] leading-tight font-semibold">{label}</span>
|
|
</button>
|
|
}
|
|
}
|
|
|
|
/// Greyed tile in "Déjà dans le panier": tap puts the item back on the list.
|
|
#[component]
|
|
pub fn BasketTile(item: Item) -> impl IntoView {
|
|
let ctx = expect_context::<Ctx>();
|
|
let (name, icon) = (item.name.clone(), item.icon);
|
|
let aria_label = format!("{name} : remettre dans la liste");
|
|
let item = StoredValue::new(item);
|
|
|
|
view! {
|
|
<button
|
|
class="flex aspect-square flex-col items-center gap-0.5 rounded-2xl bg-mist p-1.5 text-center text-ink/45 active:bg-ink/10"
|
|
aria-label=aria_label
|
|
on:click=move |_| item.with_value(|item| ctx.toggle(item))
|
|
>
|
|
<IconSvg icon class="min-h-0 w-full flex-1" />
|
|
<span class="line-clamp-2 text-xs leading-tight font-medium line-through decoration-ink/30">
|
|
{name}
|
|
</span>
|
|
</button>
|
|
}
|
|
}
|