Retire les suggestions et le décompte sous le titre de la liste
Les tuiles « + Beurre » disparaissent avec leur requête, leur fonction SQL et leurs styles. La ligne « Encore N articles à attraper. » est retirée : le nombre reste affiché dans l'onglet de la liste. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
e47db8b070
commit
ca42b85f24
7 files changed
+7
-163
No files matched your search
+1
-10
@@ -1,7 +1,7 @@
|
|||||||
use leptos::prelude::*;
|
use leptos::prelude::*;
|
||||||
use leptos::server_fn::codec::Json;
|
use leptos::server_fn::codec::Json;
|
||||||
|
|
||||||
use crate::model::{Account, Item, ItemOp, ListColor, Member, ShoppingList, Suggestion, SyncReport, User};
|
use crate::model::{Account, Item, ItemOp, ListColor, Member, ShoppingList, SyncReport, User};
|
||||||
|
|
||||||
pub const NOT_LOGGED_IN: &str = "Session expirée, reconnecte-toi.";
|
pub const NOT_LOGGED_IN: &str = "Session expirée, reconnecte-toi.";
|
||||||
|
|
||||||
@@ -452,12 +452,3 @@ pub async fn sync_items(ops: Vec<ItemOp>) -> Result<SyncReport, ServerFnError> {
|
|||||||
notify(users);
|
notify(users);
|
||||||
Ok(SyncReport { created, rejected })
|
Ok(SyncReport { created, rejected })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[server]
|
|
||||||
pub async fn get_suggestions(list_id: i64) -> Result<Vec<Suggestion>, ServerFnError> {
|
|
||||||
use server::*;
|
|
||||||
let pool = pool()?;
|
|
||||||
let user = require_user(&pool).await?;
|
|
||||||
require_list(&pool, &user, list_id).await?;
|
|
||||||
crate::db::suggestions(&pool, user.id, list_id, 3).await.map_err(db_err)
|
|
||||||
}
|
|
||||||
+4
-53
@@ -16,13 +16,13 @@ use leptos_router::components::{A, Redirect, Route, Router, Routes};
|
|||||||
use leptos_router::hooks::use_params_map;
|
use leptos_router::hooks::use_params_map;
|
||||||
use leptos_router::path;
|
use leptos_router::path;
|
||||||
|
|
||||||
use crate::api::{get_items, get_lists, get_suggestions};
|
use crate::api::{get_items, get_lists};
|
||||||
use crate::model::{Item, ItemOp, ShoppingList, Suggestion, User};
|
use crate::model::{Item, ItemOp, ShoppingList, User};
|
||||||
use account::{AccountSheet, Auth, LoginPage, RegisterPage, RequireUser};
|
use account::{AccountSheet, Auth, LoginPage, RegisterPage, RequireUser};
|
||||||
use admin::AdminPage;
|
use admin::AdminPage;
|
||||||
use sheets::{ItemEditor, ItemSheet, ListEditor, ListSheet};
|
use sheets::{ItemEditor, ItemSheet, ListEditor, ListSheet};
|
||||||
use sync::Sync;
|
use sync::Sync;
|
||||||
use tiles::{BasketTile, ItemTile, SuggestionTile};
|
use tiles::{BasketTile, ItemTile};
|
||||||
|
|
||||||
pub fn shell(options: LeptosOptions) -> impl IntoView {
|
pub fn shell(options: LeptosOptions) -> impl IntoView {
|
||||||
view! {
|
view! {
|
||||||
@@ -186,15 +186,6 @@ fn ListsView(user: User) -> impl IntoView {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
let suggestions = Resource::new(
|
|
||||||
move || (active_id.get(), ctx.refresh.get()),
|
|
||||||
|(id, _)| async move {
|
|
||||||
match id {
|
|
||||||
Some(id) => get_suggestions(id).await,
|
|
||||||
None => Ok(Vec::new()),
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
// Items of the open list with unsaved changes, for the add sheet.
|
// Items of the open list with unsaved changes, for the add sheet.
|
||||||
let list_items = Memo::new(move |_| match (active_id.get(), items.get()) {
|
let list_items = Memo::new(move |_| match (active_id.get(), items.get()) {
|
||||||
@@ -242,9 +233,8 @@ fn ListsView(user: User) -> impl IntoView {
|
|||||||
{move || Suspend::new(async move {
|
{move || Suspend::new(async move {
|
||||||
let lists = lists.await?;
|
let lists = lists.await?;
|
||||||
let (items, saved) = items.await?;
|
let (items, saved) = items.await?;
|
||||||
let suggestions = suggestions.await?;
|
|
||||||
Ok::<_, ServerFnError>(view! {
|
Ok::<_, ServerFnError>(view! {
|
||||||
<ListsContent lists items saved suggestions active_id=active_id.get() />
|
<ListsContent lists items saved active_id=active_id.get() />
|
||||||
})
|
})
|
||||||
})}
|
})}
|
||||||
</ErrorBoundary>
|
</ErrorBoundary>
|
||||||
@@ -263,7 +253,6 @@ fn ListsContent(
|
|||||||
items: Vec<Item>,
|
items: Vec<Item>,
|
||||||
/// Batches of changes saved when `items` was fetched.
|
/// Batches of changes saved when `items` was fetched.
|
||||||
saved: u32,
|
saved: u32,
|
||||||
suggestions: Vec<Suggestion>,
|
|
||||||
active_id: Option<i64>,
|
active_id: Option<i64>,
|
||||||
) -> impl IntoView {
|
) -> impl IntoView {
|
||||||
let ctx = expect_context::<Ctx>();
|
let ctx = expect_context::<Ctx>();
|
||||||
@@ -295,19 +284,6 @@ fn ListsContent(
|
|||||||
items.sort_by_cached_key(Item::sort_key);
|
items.sort_by_cached_key(Item::sort_key);
|
||||||
items
|
items
|
||||||
});
|
});
|
||||||
// Hides a suggestion as soon as it is added.
|
|
||||||
let suggestions = StoredValue::new(suggestions);
|
|
||||||
let suggestions = Memo::new(move |_| {
|
|
||||||
current.with(|items| {
|
|
||||||
suggestions.with_value(|suggestions| {
|
|
||||||
suggestions
|
|
||||||
.iter()
|
|
||||||
.filter(|s| !items.iter().any(|i| i.name.to_lowercase() == s.name.to_lowercase()))
|
|
||||||
.cloned()
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
});
|
|
||||||
let to_get = Memo::new(move |_| {
|
let to_get = Memo::new(move |_| {
|
||||||
current.with(|items| {
|
current.with(|items| {
|
||||||
items
|
items
|
||||||
@@ -371,15 +347,11 @@ fn ListsContent(
|
|||||||
</p>
|
</p>
|
||||||
}
|
}
|
||||||
})}
|
})}
|
||||||
<p class="lead">{move || remaining_text(remaining.get(), total())}</p>
|
|
||||||
|
|
||||||
<div class="tiles">
|
<div class="tiles">
|
||||||
<For each=move || to_get.get() key=|item| (item.id, item.quantity.map(|q| q.label()), item.name.clone(), item.icon, item.category) let:item>
|
<For each=move || to_get.get() key=|item| (item.id, item.quantity.map(|q| q.label()), item.name.clone(), item.icon, item.category) let:item>
|
||||||
<ItemTile item />
|
<ItemTile item />
|
||||||
</For>
|
</For>
|
||||||
<For each=move || suggestions.get() key=|suggestion| suggestion.name.clone() let:suggestion>
|
|
||||||
<SuggestionTile suggestion list_id=active_id />
|
|
||||||
</For>
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<Show when=move || !in_basket.with(Vec::is_empty)>
|
<Show when=move || !in_basket.with(Vec::is_empty)>
|
||||||
@@ -493,24 +465,3 @@ fn EmptyState() -> impl IntoView {
|
|||||||
fn TicketSkeleton() -> impl IntoView {
|
fn TicketSkeleton() -> impl IntoView {
|
||||||
view! { <div class="ticket ticket-skeleton"></div> }
|
view! { <div class="ticket ticket-skeleton"></div> }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn remaining_text(remaining: usize, total: usize) -> String {
|
|
||||||
match (remaining, total) {
|
|
||||||
(_, 0) => "Liste vide : ajoute ton premier article.".to_owned(),
|
|
||||||
(0, _) => "Tout est dans le panier !".to_owned(),
|
|
||||||
(1, _) => "Encore 1 article à attraper.".to_owned(),
|
|
||||||
(n, _) => format!("Encore {n} articles à attraper."),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn remaining_wording() {
|
|
||||||
assert_eq!(remaining_text(5, 7), "Encore 5 articles à attraper.");
|
|
||||||
assert_eq!(remaining_text(1, 2), "Encore 1 article à attraper.");
|
|
||||||
assert_eq!(remaining_text(0, 2), "Tout est dans le panier !");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+1
-26
@@ -4,7 +4,7 @@ use leptos::prelude::*;
|
|||||||
use super::icons::IconSvg;
|
use super::icons::IconSvg;
|
||||||
use super::transition;
|
use super::transition;
|
||||||
use super::{Ctx, ItemEditor};
|
use super::{Ctx, ItemEditor};
|
||||||
use crate::model::{Item, ItemInput, ItemOp, Suggestion};
|
use crate::model::Item;
|
||||||
|
|
||||||
const LONG_PRESS_MS: u64 = 450;
|
const LONG_PRESS_MS: u64 = 450;
|
||||||
|
|
||||||
@@ -78,31 +78,6 @@ pub fn ItemTile(item: Item) -> impl IntoView {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// 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 transition_name = transition::suggestion_name(&name);
|
|
||||||
|
|
||||||
let add = move |_| {
|
|
||||||
let input = ItemInput { name: name.clone(), quantity: None, icon, category };
|
|
||||||
ctx.change(ItemOp::Add { id: 0, list_id, input });
|
|
||||||
};
|
|
||||||
|
|
||||||
view! {
|
|
||||||
<button
|
|
||||||
class="tile tile-suggestion"
|
|
||||||
style:view-transition-name=transition_name
|
|
||||||
on:click=add
|
|
||||||
>
|
|
||||||
<IconSvg icon />
|
|
||||||
<span class="tile-label">{label}</span>
|
|
||||||
</button>
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Greyed tile in "Déjà dans le panier": tap puts the item back on the list.
|
/// Greyed tile in "Déjà dans le panier": tap puts the item back on the list.
|
||||||
#[component]
|
#[component]
|
||||||
pub fn BasketTile(item: Item) -> impl IntoView {
|
pub fn BasketTile(item: Item) -> impl IntoView {
|
||||||
|
|||||||
@@ -15,13 +15,6 @@ pub fn item_name(id: i64) -> String {
|
|||||||
format!("item-{id}")
|
format!("item-{id}")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `view-transition-name` of a suggestion tile. Product names may contain
|
|
||||||
/// spaces or accents, which are not allowed in an identifier: hex-encode them.
|
|
||||||
pub fn suggestion_name(name: &str) -> String {
|
|
||||||
let hex: String = name.bytes().map(|b| format!("{b:02x}")).collect();
|
|
||||||
format!("suggestion-{hex}")
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(feature = "hydrate")]
|
#[cfg(feature = "hydrate")]
|
||||||
mod browser {
|
mod browser {
|
||||||
use js_sys::{Function, Promise, Reflect};
|
use js_sys::{Function, Promise, Reflect};
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use sqlx::{Row, SqliteConnection};
|
|||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::model::{
|
use crate::model::{
|
||||||
Account, Category, Icon, Item, ItemInput, ItemOp, ListColor, Member, Quantity, ShoppingList, Suggestion, Unit, User,
|
Account, Category, Icon, Item, ItemInput, ItemOp, ListColor, Member, Quantity, ShoppingList, Unit, User,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub async fn connect(url: &str) -> Result<SqlitePool, sqlx::Error> {
|
pub async fn connect(url: &str) -> Result<SqlitePool, sqlx::Error> {
|
||||||
@@ -439,32 +439,6 @@ pub async fn apply_item_ops(pool: &SqlitePool, user_id: i64, ops: &[ItemOp]) ->
|
|||||||
Ok(created)
|
Ok(created)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The user's most used products not already on the list.
|
|
||||||
pub async fn suggestions(pool: &SqlitePool, user_id: i64, list_id: i64, limit: i64) -> Result<Vec<Suggestion>, sqlx::Error> {
|
|
||||||
sqlx::query(
|
|
||||||
"SELECT name, icon, category FROM catalog c
|
|
||||||
WHERE c.user_id = ? AND NOT EXISTS (
|
|
||||||
SELECT 1 FROM items i WHERE i.list_id = ? AND i.name = c.name COLLATE NOCASE
|
|
||||||
)
|
|
||||||
ORDER BY use_count DESC, name
|
|
||||||
LIMIT ?",
|
|
||||||
)
|
|
||||||
.bind(user_id)
|
|
||||||
.bind(list_id)
|
|
||||||
.bind(limit)
|
|
||||||
.fetch_all(pool)
|
|
||||||
.await?
|
|
||||||
.iter()
|
|
||||||
.map(|row| {
|
|
||||||
Ok(Suggestion {
|
|
||||||
name: row.try_get("name")?,
|
|
||||||
icon: decode(Icon::parse(row.try_get("icon")?), "icon")?,
|
|
||||||
category: decode(Category::parse(row.try_get("category")?), "category")?,
|
|
||||||
})
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -587,23 +561,6 @@ mod tests {
|
|||||||
assert!(all[0].checked);
|
assert!(all[0].checked);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn suggestions_skip_items_on_list() {
|
|
||||||
let pool = pool().await;
|
|
||||||
let me = user(&pool, "a@x.fr").await;
|
|
||||||
let list = lists(&pool, me).await.unwrap()[0].id;
|
|
||||||
add_item(&mut *conn(&pool).await, me, list, &input("beurre")).await.unwrap();
|
|
||||||
|
|
||||||
let names: Vec<_> = suggestions(&pool, me, list, 3)
|
|
||||||
.await
|
|
||||||
.unwrap()
|
|
||||||
.into_iter()
|
|
||||||
.map(|s| s.name)
|
|
||||||
.collect();
|
|
||||||
assert!(!names.iter().any(|n| n.eq_ignore_ascii_case("beurre")));
|
|
||||||
assert_eq!(names.len(), 3);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn deleting_list_cascades() {
|
async fn deleting_list_cascades() {
|
||||||
let pool = pool().await;
|
let pool = pool().await;
|
||||||
|
|||||||
@@ -282,14 +282,6 @@ text_enum!(Icon {
|
|||||||
Basket => "basket",
|
Basket => "basket",
|
||||||
});
|
});
|
||||||
|
|
||||||
/// A frequently bought product, offered as a dashed "+ Beurre" tile.
|
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
|
||||||
pub struct Suggestion {
|
|
||||||
pub name: String,
|
|
||||||
pub icon: Icon,
|
|
||||||
pub category: Category,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Payload for creating or editing an item.
|
/// Payload for creating or editing an item.
|
||||||
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
|
||||||
pub struct ItemInput {
|
pub struct ItemInput {
|
||||||
|
|||||||
@@ -445,21 +445,6 @@ fieldset {
|
|||||||
line-height: 1rem;
|
line-height: 1rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Suggestion : tuile en pointillés. */
|
|
||||||
.tile-suggestion {
|
|
||||||
border-style: dashed;
|
|
||||||
border-color: var(--ink-35);
|
|
||||||
color: var(--ink-55);
|
|
||||||
}
|
|
||||||
|
|
||||||
.tile-suggestion:active {
|
|
||||||
background: var(--ink-5);
|
|
||||||
}
|
|
||||||
|
|
||||||
.tile-suggestion svg {
|
|
||||||
opacity: 0.6;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Article dans le panier : tuile grise barrée. */
|
/* Article dans le panier : tuile grise barrée. */
|
||||||
.tile-off {
|
.tile-off {
|
||||||
border-color: transparent;
|
border-color: transparent;
|
||||||
|
|||||||
Reference in new issue
Block a user