Fix incorrect dirty form handling for recipe and categories

This commit is contained in:
Jeremy Wall 2022-10-26 15:25:01 -04:00
parent b957ed7f3a
commit 2d1c11e5d7
2 changed files with 19 additions and 4 deletions

View File

@ -44,6 +44,8 @@ pub fn Categories<G: Html>(cx: Scope) -> View<G> {
let save_signal = create_signal(cx, ()); let save_signal = create_signal(cx, ());
let error_text = create_signal(cx, String::new()); let error_text = create_signal(cx, String::new());
let category_text: &Signal<String> = create_signal(cx, String::new()); let category_text: &Signal<String> = create_signal(cx, String::new());
let dirty = create_signal(cx, false);
spawn_local_scoped(cx, { spawn_local_scoped(cx, {
let store = crate::api::HttpStore::get_from_context(cx); let store = crate::api::HttpStore::get_from_context(cx);
async move { async move {
@ -58,8 +60,10 @@ pub fn Categories<G: Html>(cx: Scope) -> View<G> {
}); });
create_effect(cx, move || { create_effect(cx, move || {
// TODO(jwall): This is triggering on load which is not desired.
save_signal.track(); save_signal.track();
if !*dirty.get() {
return;
}
spawn_local_scoped(cx, { spawn_local_scoped(cx, {
let store = crate::api::HttpStore::get_from_context(cx); let store = crate::api::HttpStore::get_from_context(cx);
async move { async move {
@ -70,6 +74,8 @@ pub fn Categories<G: Html>(cx: Scope) -> View<G> {
{ {
error!(?e, "Failed to save categories"); error!(?e, "Failed to save categories");
error_text.set(format!("{:?}", e)); error_text.set(format!("{:?}", e));
} else {
dirty.set(false);
} }
} }
}); });
@ -94,7 +100,9 @@ pub fn Categories<G: Html>(cx: Scope) -> View<G> {
view! {cx, view! {cx,
(dialog_view) (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 |_| { span(role="button", on:click=move |_| {
check_category_text_parses(category_text.get().as_str(), error_text); check_category_text_parses(category_text.get().as_str(), error_text);
}) { "Check" } " " }) { "Check" } " "

View File

@ -45,10 +45,13 @@ fn Editor<G: Html>(cx: Scope, recipe: RecipeEntry) -> View<G> {
let text = create_signal(cx, recipe.recipe_text().to_owned()); let text = create_signal(cx, recipe.recipe_text().to_owned());
let error_text = create_signal(cx, String::new()); let error_text = create_signal(cx, String::new());
let save_signal = create_signal(cx, ()); let save_signal = create_signal(cx, ());
let dirty = create_signal(cx, false);
create_effect(cx, move || { create_effect(cx, move || {
// TODO(jwall): This is triggering on load which is not desired.
save_signal.track(); save_signal.track();
if !*dirty.get() {
return;
}
spawn_local_scoped(cx, { spawn_local_scoped(cx, {
let store = crate::api::HttpStore::get_from_context(cx); let store = crate::api::HttpStore::get_from_context(cx);
async move { async move {
@ -61,6 +64,8 @@ fn Editor<G: Html>(cx: Scope, recipe: RecipeEntry) -> View<G> {
{ {
error!(?e, "Failed to save recipe"); error!(?e, "Failed to save recipe");
error_text.set(format!("{:?}", e)); error_text.set(format!("{:?}", e));
} else {
dirty.set(false);
}; };
} }
}); });
@ -85,7 +90,9 @@ fn Editor<G: Html>(cx: Scope, recipe: RecipeEntry) -> View<G> {
view! {cx, view! {cx,
(dialog_view) (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 |_| { span(role="button", on:click=move |_| {
let unparsed = text.get(); let unparsed = text.get();
check_recipe_parses(unparsed.as_str(), error_text.clone()); check_recipe_parses(unparsed.as_str(), error_text.clone());