Compare commits

...

2 commits

Author SHA1 Message Date
82b8b447dc fix(item-form): update tag handling to support key-value pairs
- Changed `tags` signal type from `Vec<String>` to `Vec<(String, String)>` for richer data representation.
- Updated `on_submit` callback to align with the new `tags` structure.
- Modified form submission logic to accommodate changes in tag handling.
2024-12-09 19:20:18 +03:00
8c03fa35a1 fix: fix App component signal handling and on_submit prop
- Replaced incorrect path statement in `add_item` with proper use of `set_items.update` for signal mutation.
- Fixed dynamic dispatch for `on_submit` by boxing the `add_item` closure.
- Simplified `items_signal` usage by removing unnecessary `.clone()`.
- Improved code consistency and alignment with the latest Leptos updates.
2024-12-09 14:58:05 +03:00
2 changed files with 11 additions and 11 deletions

View file

@ -1,15 +1,16 @@
use leptos::*; use leptos::*;
use crate::components::{item_form::ItemForm, items_list::ItemsList}; use crate::components::{item_form::ItemForm, items_list::ItemsList};
use crate::models::item::Item; use crate::models::item::Item;
use uuid::Uuid;
#[component] #[component]
pub fn App() -> impl IntoView { pub fn App() -> impl IntoView {
let (items_signal, set_items) = create_signal(Vec::<Item>::new()); let (items_signal, set_items) = create_signal(Vec::<Item>::new());
let add_item = move |name: String, description: String, tags: Vec<(String, String)>| { let add_item = move |name: String, description: String, tags: Vec<(String, String)>| {
set_items;(|mut items: Vec<Item>| { set_items.update(|items| {
items.push(Item { items.push(Item {
id: uuid::Uuid::new_v4().to_string(), id: Uuid::new_v4().to_string(),
name, name,
description, description,
tags, tags,
@ -19,9 +20,9 @@ pub fn App() -> impl IntoView {
view! { view! {
<div> <div>
<h1>CompareWare</h1> <h1>{ "CompareWare" }</h1>
<ItemForm on_submit=Box::new(add_item) /> <ItemForm on_submit=Box::new(add_item) />
<ItemsList items={items_signal.get().clone()} /> <ItemsList items=items_signal.get() />
</div> </div>
} }
} }

View file

@ -1,15 +1,14 @@
use leptos::*; use leptos::*;
use leptos_dom::ev::SubmitEvent; // Import the correct event type use leptos_dom::ev::SubmitEvent;
#[component] #[component]
pub fn ItemForm(on_submit: Box<dyn Fn(String, String, Vec<String>)>) -> impl IntoView { pub fn ItemForm(on_submit: Box<dyn Fn(String, String, Vec<(String, String)>)>) -> impl IntoView {
let (name, set_name) = create_signal(String::new()); let (name, set_name) = create_signal(String::new());
let (description, set_description) = create_signal(String::new()); let (description, set_description) = create_signal(String::new());
let (tags, set_tags) = create_signal(Vec::<String>::new()); let (tags, set_tags) = create_signal(Vec::<(String, String)>::new());
// Use SubmitEvent for the form submission handler
let handle_submit = move |ev: SubmitEvent| { let handle_submit = move |ev: SubmitEvent| {
ev.prevent_default(); // Prevent form submission from reloading the page ev.prevent_default();
on_submit(name.get(), description.get(), tags.get().clone()); on_submit(name.get(), description.get(), tags.get().clone());
// Reset values after submission // Reset values after submission
@ -34,4 +33,4 @@ pub fn ItemForm(on_submit: Box<dyn Fn(String, String, Vec<String>)>) -> impl Int
<button type="submit">{ "Add Item" }</button> <button type="submit">{ "Add Item" }</button>
</form> </form>
} }
} }