From 5480074d3a3abe44522e894fe3eda49105e9655a Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Thu, 24 Nov 2022 10:28:02 -0500 Subject: [PATCH] Save the full state each time --- web/src/api.rs | 13 +++++++++++++ web/src/app_state.rs | 1 + web/src/components/recipe_plan.rs | 6 +----- web/src/components/shopping_list.rs | 10 ++-------- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/web/src/api.rs b/web/src/api.rs index f4b31ad..be44f01 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -327,6 +327,19 @@ impl HttpStore { } } + pub async fn save_state(&self, state: std::rc::Rc) -> Result<(), Error> { + let mut plan = Vec::new(); + for (key, count) in state.recipe_counts.get_untracked().iter() { + plan.push((key.clone(), *count.get_untracked() as i32)); + } + self.save_plan(plan).await?; + self.save_inventory_data( + state.filtered_ingredients.get_untracked().as_ref().clone(), + state.get_current_modified_amts(), + ) + .await + } + pub async fn save_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> { let mut path = self.root.clone(); path.push_str("/plan"); diff --git a/web/src/app_state.rs b/web/src/app_state.rs index 73a6277..46093dc 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -18,6 +18,7 @@ use tracing::{debug, instrument, warn}; use recipes::{Ingredient, IngredientAccumulator, IngredientKey, Recipe}; +#[derive(Debug)] pub struct State { pub recipe_counts: RcSignal>>, pub extras: RcSignal, RcSignal))>>, diff --git a/web/src/components/recipe_plan.rs b/web/src/components/recipe_plan.rs index d716554..ab409fb 100644 --- a/web/src/components/recipe_plan.rs +++ b/web/src/components/recipe_plan.rs @@ -56,12 +56,8 @@ pub fn RecipePlan(cx: Scope) -> View { let store = HttpStore::get_from_context(cx); let state = app_state::State::get_from_context(cx); spawn_local_scoped(cx, { - let mut plan = Vec::new(); - for (key, count) in state.recipe_counts.get_untracked().iter() { - plan.push((key.clone(), *count.get_untracked() as i32)); - } async move { - store.save_plan(plan).await.expect("Failed to save plan"); + store.save_state(state).await.expect("Failed to save plan"); } }) }); diff --git a/web/src/components/shopping_list.rs b/web/src/components/shopping_list.rs index 5c196b8..36bd680 100644 --- a/web/src/components/shopping_list.rs +++ b/web/src/components/shopping_list.rs @@ -191,16 +191,10 @@ pub fn ShoppingList(cx: Scope) -> View { spawn_local_scoped(cx, { let state = crate::app_state::State::get_from_context(cx); let store = crate::api::HttpStore::get_from_context(cx); - let filtered_ingredients = state.filtered_ingredients.get_untracked().as_ref().clone(); - let modified_amts = state.get_current_modified_amts(); async move { - debug!( - ?filtered_ingredients, - ?modified_amts, - "Attempting save for inventory" - ); + debug!(?state, "Attempting save for inventory"); store - .save_inventory_data(filtered_ingredients, modified_amts) + .save_state(state) .await .expect("Unable to save inventory data"); }