From da720a48d3f40d8b916249e592425396f502cac1 Mon Sep 17 00:00:00 2001 From: Ryan Mwangi Date: Wed, 18 Dec 2024 16:00:40 +0300 Subject: [PATCH] refactor(item_list): Refactor `ItemsList` component to improve readability and reuse. - Extracted item selection toggle logic into a dedicated `toggle_selection` function. - Simplified access to item properties in views by using references (`&`). - Improved code clarity and maintainability by reducing inline complexity. --- src/components/items_list.rs | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/src/components/items_list.rs b/src/components/items_list.rs index 1274ad5..19102c8 100644 --- a/src/components/items_list.rs +++ b/src/components/items_list.rs @@ -8,6 +8,17 @@ pub fn ItemsList(items: ReadSignal>) -> impl IntoView { // Create a signal for selected items let (selected_items_signal, set_selected_items) = create_signal(Vec::::new()); + // Function to toggle selection of an item + let toggle_selection = move |i: usize| { + set_selected_items.update(|items| { + if items.contains(&i) { + items.retain(|&x| x != i); + } else { + items.push(i); + } + }); + }; + view! {

{ "Items" }

@@ -18,18 +29,12 @@ pub fn ItemsList(items: ReadSignal>) -> impl IntoView { {"Select item for comparison"}
- { item.name.clone() } - { item.description.clone() } + { &item.name } - { &item.description }

    { "Tags:" }

    @@ -44,7 +49,7 @@ pub fn ItemsList(items: ReadSignal>) -> impl IntoView { }).collect::>()}
- }).collect::>()} + }).collect::>() } // Comparison Table @@ -65,8 +70,8 @@ pub fn ItemsList(items: ReadSignal>) -> impl IntoView { let item = &items.get()[i]; view! { - { item.name.clone() } - { item.description.clone() } + { &item.name } + { &item.description } {item.tags.iter().map(|(key, value)| view! { { key.clone() + ": " + value + " " } @@ -85,4 +90,4 @@ pub fn ItemsList(items: ReadSignal>) -> impl IntoView {
} -} \ No newline at end of file +}