From 2267744087bae36ce1e42db6aff28abb45ac1831 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Wed, 18 Jan 2023 20:12:29 -0500 Subject: [PATCH] Various bugfixes --- kitchen/src/web/storage/mod.rs | 3 +++ web/src/api.rs | 14 +++++++------- web/src/app_state.rs | 21 ++++++++++++++++++--- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/kitchen/src/web/storage/mod.rs b/kitchen/src/web/storage/mod.rs index 9e35014..29db0ca 100644 --- a/kitchen/src/web/storage/mod.rs +++ b/kitchen/src/web/storage/mod.rs @@ -621,11 +621,13 @@ impl APIStore for SqliteStore { Ok(Some(result)) } + #[instrument(skip_all, fields(user_id=user_id.as_ref(), date))] async fn delete_meal_plan_for_date + Send>( &self, user_id: S, date: NaiveDate, ) -> Result<()> { + debug!("Processing delete request"); let user_id = user_id.as_ref(); let mut transaction = self.pool.as_ref().begin().await?; sqlx::query!( @@ -656,6 +658,7 @@ impl APIStore for SqliteStore { ) .execute(&mut transaction) .await?; + transaction.commit().await?; Ok(()) } diff --git a/web/src/api.rs b/web/src/api.rs index 848e7a8..c97280a 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -257,7 +257,7 @@ impl LocalStore { } } - pub fn delete_plan_for_date(&self, date: &NaiveDate) { + pub fn delete_plan(&self) { self.store.delete("plan").expect("Failed to delete plan"); self.store .delete("inventory") @@ -578,18 +578,18 @@ impl HttpStore { } #[instrument(skip_all)] - pub async fn store_app_state(&self, state: AppState) -> Result<(), Error> { + pub async fn store_app_state(&self, state: &AppState) -> Result<(), Error> { let mut plan = Vec::new(); for (key, count) in state.recipe_counts.iter() { plan.push((key.clone(), *count as i32)); } if let Some(cached_plan_date) = &state.selected_plan_date { - debug!("Saving plan data"); + debug!(?plan, "Saving plan data"); self.store_plan_for_date(plan, cached_plan_date).await?; debug!("Saving inventory data"); self.store_inventory_data_for_date( - state.filtered_ingredients, - state.modified_amts, + state.filtered_ingredients.clone(), + state.modified_amts.clone(), state .extras .iter() @@ -603,8 +603,8 @@ impl HttpStore { self.store_plan(plan).await?; debug!("Saving inventory data"); self.store_inventory_data( - state.filtered_ingredients, - state.modified_amts, + state.filtered_ingredients.clone(), + state.modified_amts.clone(), state .extras .iter() diff --git a/web/src/app_state.rs b/web/src/app_state.rs index 105a3b9..aedc1bd 100644 --- a/web/src/app_state.rs +++ b/web/src/app_state.rs @@ -211,6 +211,11 @@ impl StateMachine { plan_map.insert(id, count as usize); } state.recipe_counts = plan_map; + for (id, _) in state.recipes.iter() { + if !state.recipe_counts.contains_key(id) { + state.recipe_counts.insert(id.clone(), 0); + } + } } else { if let Some(plan) = local_store.get_plan() { state.recipe_counts = plan.iter().map(|(k, v)| (k.clone(), *v as usize)).collect(); @@ -416,14 +421,24 @@ impl MessageMapper for StateMachine { original_copy.auth = Some(user_data); } Message::SaveState(f) => { - let original_copy = original_copy.clone(); + let mut original_copy = original_copy.clone(); let store = self.store.clone(); spawn_local_scoped(cx, async move { - if let Err(e) = store.store_app_state(original_copy).await { + if original_copy.selected_plan_date.is_none() { + original_copy.selected_plan_date = Some(chrono::Local::now().date_naive()); + } + original_copy + .plan_dates + .insert(original_copy.selected_plan_date.map(|d| d.clone()).unwrap()); + if let Err(e) = store.store_app_state(&original_copy).await { error!(err=?e, "Error saving app state") }; + original.set(original_copy); f.map(|f| f()); }); + // NOTE(jwall): We set the original signal in the async above + // so we return immediately here. + return; } Message::LoadState(f) => { let store = self.store.clone(); @@ -494,7 +509,7 @@ impl MessageMapper for StateMachine { .delete_plan_for_date(&date) .await .expect("Failed to delete meal plan for date"); - local_store.delete_plan_for_date(&date); + local_store.delete_plan(); original_copy.plan_dates.remove(&date); // Reset all meal planning state;