From 2d1c11e5d75db0f1b326254d61301ffb0e9ee0dd Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 26 Oct 2022 15:25:01 -0400 Subject: [PATCH] Fix incorrect dirty form handling for recipe and categories --- web/src/components/categories.rs | 12 ++++++++++-- web/src/components/recipe.rs | 11 +++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/web/src/components/categories.rs b/web/src/components/categories.rs index 57b2c6d..68338d6 100644 --- a/web/src/components/categories.rs +++ b/web/src/components/categories.rs @@ -44,6 +44,8 @@ pub fn Categories(cx: Scope) -> View { let save_signal = create_signal(cx, ()); let error_text = create_signal(cx, String::new()); let category_text: &Signal = create_signal(cx, String::new()); + let dirty = create_signal(cx, false); + spawn_local_scoped(cx, { let store = crate::api::HttpStore::get_from_context(cx); async move { @@ -58,8 +60,10 @@ pub fn Categories(cx: Scope) -> View { }); create_effect(cx, move || { - // TODO(jwall): This is triggering on load which is not desired. save_signal.track(); + if !*dirty.get() { + return; + } spawn_local_scoped(cx, { let store = crate::api::HttpStore::get_from_context(cx); async move { @@ -70,6 +74,8 @@ pub fn Categories(cx: Scope) -> View { { error!(?e, "Failed to save categories"); error_text.set(format!("{:?}", e)); + } else { + dirty.set(false); } } }); @@ -94,7 +100,9 @@ pub fn Categories(cx: Scope) -> View { view! {cx, (dialog_view) - textarea(bind:value=category_text, rows=20) + textarea(bind:value=category_text, rows=20, on:change=move |_| { + dirty.set(true); + }) span(role="button", on:click=move |_| { check_category_text_parses(category_text.get().as_str(), error_text); }) { "Check" } " " diff --git a/web/src/components/recipe.rs b/web/src/components/recipe.rs index 95b64c3..d42e6ca 100644 --- a/web/src/components/recipe.rs +++ b/web/src/components/recipe.rs @@ -45,10 +45,13 @@ fn Editor(cx: Scope, recipe: RecipeEntry) -> View { let text = create_signal(cx, recipe.recipe_text().to_owned()); let error_text = create_signal(cx, String::new()); let save_signal = create_signal(cx, ()); + let dirty = create_signal(cx, false); create_effect(cx, move || { - // TODO(jwall): This is triggering on load which is not desired. save_signal.track(); + if !*dirty.get() { + return; + } spawn_local_scoped(cx, { let store = crate::api::HttpStore::get_from_context(cx); async move { @@ -61,6 +64,8 @@ fn Editor(cx: Scope, recipe: RecipeEntry) -> View { { error!(?e, "Failed to save recipe"); error_text.set(format!("{:?}", e)); + } else { + dirty.set(false); }; } }); @@ -85,7 +90,9 @@ fn Editor(cx: Scope, recipe: RecipeEntry) -> View { view! {cx, (dialog_view) - textarea(bind:value=text, rows=20) + textarea(bind:value=text, rows=20, on:change=move |_| { + dirty.set(true); + }) span(role="button", on:click=move |_| { let unparsed = text.get(); check_recipe_parses(unparsed.as_str(), error_text.clone());