UI for recipe deletion

This commit is contained in:
Jeremy Wall 2023-01-07 15:43:19 -05:00
parent 170c45fd72
commit 9af13c5bb4
3 changed files with 37 additions and 20 deletions

View File

@ -497,6 +497,23 @@ impl HttpStore {
} }
} }
#[instrument]
pub async fn delete_recipe<S>(&self, recipe: S) -> Result<(), Error>
where
S: AsRef<str> + 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()))] #[instrument(skip(recipes), fields(count=recipes.len()))]
pub async fn store_recipes(&self, recipes: Vec<RecipeEntry>) -> Result<(), Error> { pub async fn store_recipes(&self, recipes: Vec<RecipeEntry>) -> Result<(), Error> {
let mut path = self.v2_path(); let mut path = self.v2_path();

View File

@ -61,7 +61,7 @@ pub enum Message {
UpdateExtra(usize, String, String), UpdateExtra(usize, String, String),
SaveRecipe(RecipeEntry), SaveRecipe(RecipeEntry),
SetRecipe(String, Recipe), SetRecipe(String, Recipe),
SetCategoryMap(BTreeMap<String, String>), RemoveRecipe(String),
UpdateCategory(String, String), UpdateCategory(String, String),
ResetInventory, ResetInventory,
AddFilteredIngredient(IngredientKey), AddFilteredIngredient(IngredientKey),
@ -95,7 +95,7 @@ impl Debug for Message {
Self::SetRecipe(arg0, arg1) => { Self::SetRecipe(arg0, arg1) => {
f.debug_tuple("SetRecipe").field(arg0).field(arg1).finish() 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) => { Self::UpdateCategory(i, c) => {
f.debug_tuple("UpdateCategory").field(i).field(c).finish() f.debug_tuple("UpdateCategory").field(i).field(c).finish()
} }
@ -120,12 +120,11 @@ pub struct StateMachine {
} }
#[instrument] #[instrument]
fn filter_recipes( fn parse_recipes(
recipe_entries: &Option<Vec<RecipeEntry>>, recipe_entries: &Option<Vec<RecipeEntry>>,
) -> Result<(Option<Recipe>, Option<BTreeMap<String, Recipe>>), String> { ) -> Result<Option<BTreeMap<String, Recipe>>, String> {
match recipe_entries { match recipe_entries {
Some(parsed) => { Some(parsed) => {
let mut staples = None;
let mut parsed_map = BTreeMap::new(); let mut parsed_map = BTreeMap::new();
for r in parsed { for r in parsed {
let recipe = match parse::as_recipe(&r.recipe_text()) { let recipe = match parse::as_recipe(&r.recipe_text()) {
@ -135,15 +134,11 @@ fn filter_recipes(
continue; continue;
} }
}; };
if recipe.title == "Staples" { parsed_map.insert(r.recipe_id().to_owned(), recipe);
staples = Some(recipe);
} else {
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(); let mut state = original.get().as_ref().clone();
info!("Synchronizing Recipes"); info!("Synchronizing Recipes");
let recipe_entries = &store.fetch_recipes().await?; 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 { if let Some(recipes) = recipes {
state.recipes = recipes; state.recipes = recipes;
}; };
@ -329,14 +325,14 @@ impl MessageMapper<Message, AppState> for StateMachine {
} }
}); });
} }
Message::SetCategoryMap(map) => { Message::RemoveRecipe(recipe) => {
original_copy.category_map = map.clone(); original_copy.recipe_counts.remove(&recipe);
let list = map.iter().map(|(i, c)| (i.clone(), c.clone())).collect(); original_copy.recipes.remove(&recipe);
self.local_store.set_categories(Some(&list)); self.local_store.delete_recipe_entry(&recipe);
let store = self.store.clone(); let store = self.store.clone();
spawn_local_scoped(cx, async move { spawn_local_scoped(cx, async move {
if let Err(e) = store.store_categories(&list).await { if let Err(err) = store.delete_recipe(&recipe).await {
error!(?e, "Failed to save categories"); error!(?err, "Failed to delete recipe");
} }
}); });
} }

View File

@ -117,7 +117,11 @@ pub fn Editor<'ctx, G: Html>(cx: Scope<'ctx>, props: RecipeComponentProps<'ctx>)
}); });
} else { } 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" }
} }
} }