From 9af13c5bb4f8ceee4cfc90c959fb4229fe404993 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sat, 7 Jan 2023 15:43:19 -0500 Subject: [PATCH] UI for recipe deletion --- web/src/api.rs | 17 +++++++++++++++++ web/src/app_state.rs | 34 +++++++++++++++------------------- web/src/components/recipe.rs | 6 +++++- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/web/src/api.rs b/web/src/api.rs index 726666d..6b58877 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -497,6 +497,23 @@ impl HttpStore { } } + #[instrument] + pub async fn delete_recipe(&self, recipe: S) -> Result<(), Error> + where + S: AsRef + std::fmt::Debug, + { + let mut path = self.v2_path(); + path.push_str("/recipe"); + path.push_str(&format!("/{}", recipe.as_ref())); + let resp = reqwasm::http::Request::delete(&path).send().await?; + if resp.status() != 200 { + Err(format!("Status: {}", resp.status()).into()) + } else { + debug!("We got a valid response back!"); + Ok(()) + } + } + #[instrument(skip(recipes), fields(count=recipes.len()))] pub async fn store_recipes(&self, recipes: Vec) -> Result<(), Error> { let mut path = self.v2_path(); diff --git a/web/src/app_state.rs b/web/src/app_state.rs index 03647d5..3534deb 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -61,7 +61,7 @@ pub enum Message { UpdateExtra(usize, String, String), SaveRecipe(RecipeEntry), SetRecipe(String, Recipe), - SetCategoryMap(BTreeMap), + RemoveRecipe(String), UpdateCategory(String, String), ResetInventory, AddFilteredIngredient(IngredientKey), @@ -95,7 +95,7 @@ impl Debug for Message { Self::SetRecipe(arg0, arg1) => { f.debug_tuple("SetRecipe").field(arg0).field(arg1).finish() } - Self::SetCategoryMap(arg0) => f.debug_tuple("SetCategoryMap").field(arg0).finish(), + Self::RemoveRecipe(arg0) => f.debug_tuple("SetCategoryMap").field(arg0).finish(), Self::UpdateCategory(i, c) => { f.debug_tuple("UpdateCategory").field(i).field(c).finish() } @@ -120,12 +120,11 @@ pub struct StateMachine { } #[instrument] -fn filter_recipes( +fn parse_recipes( recipe_entries: &Option>, -) -> Result<(Option, Option>), String> { +) -> Result>, String> { match recipe_entries { Some(parsed) => { - let mut staples = None; let mut parsed_map = BTreeMap::new(); for r in parsed { let recipe = match parse::as_recipe(&r.recipe_text()) { @@ -135,15 +134,11 @@ fn filter_recipes( continue; } }; - if recipe.title == "Staples" { - staples = Some(recipe); - } else { - parsed_map.insert(r.recipe_id().to_owned(), recipe); - } + parsed_map.insert(r.recipe_id().to_owned(), recipe); } - Ok((staples, Some(parsed_map))) + Ok(Some(parsed_map)) } - None => Ok((None, None)), + None => Ok(None), } } @@ -160,7 +155,8 @@ impl StateMachine { let mut state = original.get().as_ref().clone(); info!("Synchronizing Recipes"); let recipe_entries = &store.fetch_recipes().await?; - let (_old_staples, recipes) = filter_recipes(&recipe_entries)?; + let recipes = parse_recipes(&recipe_entries)?; + if let Some(recipes) = recipes { state.recipes = recipes; }; @@ -329,14 +325,14 @@ impl MessageMapper for StateMachine { } }); } - Message::SetCategoryMap(map) => { - original_copy.category_map = map.clone(); - let list = map.iter().map(|(i, c)| (i.clone(), c.clone())).collect(); - self.local_store.set_categories(Some(&list)); + Message::RemoveRecipe(recipe) => { + original_copy.recipe_counts.remove(&recipe); + original_copy.recipes.remove(&recipe); + self.local_store.delete_recipe_entry(&recipe); let store = self.store.clone(); spawn_local_scoped(cx, async move { - if let Err(e) = store.store_categories(&list).await { - error!(?e, "Failed to save categories"); + if let Err(err) = store.delete_recipe(&recipe).await { + error!(?err, "Failed to delete recipe"); } }); } diff --git a/web/src/components/recipe.rs b/web/src/components/recipe.rs index e642d41..79bc8ba 100644 --- a/web/src/components/recipe.rs +++ b/web/src/components/recipe.rs @@ -117,7 +117,11 @@ pub fn Editor<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>) }); } else { } - }) { "Save" } + }) { "Save" } " " + span(role="button", on:click=move |_| { + sh.dispatch(cx, Message::RemoveRecipe(id.get_untracked().as_ref().to_owned())); + sycamore_router::navigate("/ui/planning/plan") + }) { "delete" } } }