From 0a68c3d51cb7a4b97f00d07ca80848e151e495e6 Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Tue, 17 Jan 2023 19:42:48 -0500 Subject: [PATCH] Handle saving and loading for specific plan_dates --- web/src/api.rs | 98 ++++++++++++++++++++++++++++----- web/src/components/plan_list.rs | 2 +- 2 files changed, 85 insertions(+), 15 deletions(-) diff --git a/web/src/api.rs b/web/src/api.rs index 97e4ce5..10d6f97 100644 --- a/web/src/api.rs +++ b/web/src/api.rs @@ -572,24 +572,40 @@ impl HttpStore { #[instrument(skip_all)] pub async fn store_app_state(&self, state: AppState) -> Result<(), Error> { - // TODO(jwall): We need to be able to store state for the plan date if set. let mut plan = Vec::new(); for (key, count) in state.recipe_counts.iter() { plan.push((key.clone(), *count as i32)); } - debug!("Saving plan data"); - self.store_plan(plan).await?; - debug!("Saving inventory data"); - self.store_inventory_data( - state.filtered_ingredients, - state.modified_amts, - state - .extras - .iter() - .cloned() - .collect::>(), - ) - .await + if let Some(cached_plan_date) = &state.selected_plan_date { + debug!("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 + .extras + .iter() + .cloned() + .collect::>(), + cached_plan_date, + ) + .await + } else { + debug!("Saving plan data"); + self.store_plan(plan).await?; + debug!("Saving inventory data"); + self.store_inventory_data( + state.filtered_ingredients, + state.modified_amts, + state + .extras + .iter() + .cloned() + .collect::>(), + ) + .await + } } pub async fn store_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> { @@ -608,6 +624,28 @@ impl HttpStore { } } + pub async fn store_plan_for_date( + &self, + plan: Vec<(String, i32)>, + date: &NaiveDate, + ) -> Result<(), Error> { + let mut path = self.v2_path(); + path.push_str("/plan"); + path.push_str("/at"); + path.push_str(&format!("/{}", date)); + let resp = reqwasm::http::Request::post(&path) + .body(to_string(&plan).expect("Unable to encode plan as json")) + .header("content-type", "application/json") + .send() + .await?; + if resp.status() != 200 { + Err(format!("Status: {}", resp.status()).into()) + } else { + debug!("We got a valid response back!"); + Ok(()) + } + } + pub async fn fetch_plan_dates(&self) -> Result>, Error> { let mut path = self.v2_path(); path.push_str("/plan"); @@ -746,6 +784,38 @@ impl HttpStore { } } + #[instrument] + pub async fn store_inventory_data_for_date( + &self, + filtered_ingredients: BTreeSet, + modified_amts: BTreeMap, + extra_items: Vec<(String, String)>, + date: &NaiveDate, + ) -> Result<(), Error> { + let mut path = self.v2_path(); + path.push_str("/inventory"); + path.push_str("/at"); + path.push_str(&format!("/{}", date)); + let filtered_ingredients: Vec = filtered_ingredients.into_iter().collect(); + let modified_amts: Vec<(IngredientKey, String)> = modified_amts.into_iter().collect(); + debug!("Storing inventory data in cache"); + let serialized_inventory = to_string(&(filtered_ingredients, modified_amts, extra_items)) + .expect("Unable to encode plan as json"); + debug!("Storing inventory data via API"); + let resp = reqwasm::http::Request::post(&path) + .body(&serialized_inventory) + .header("content-type", "application/json") + .send() + .await?; + if resp.status() != 200 { + debug!("Invalid response back"); + Err(format!("Status: {}", resp.status()).into()) + } else { + debug!("We got a valid response back!"); + Ok(()) + } + } + #[instrument] pub async fn store_inventory_data( &self, diff --git a/web/src/components/plan_list.rs b/web/src/components/plan_list.rs index fb20db6..56f1e63 100644 --- a/web/src/components/plan_list.rs +++ b/web/src/components/plan_list.rs @@ -34,7 +34,7 @@ pub fn PlanList<'ctx, G: Html>(cx: Scope<'ctx>, props: PlanListProps<'ctx>) -> V view=move |cx, date| { let date_display = format!("{}", date); view!{cx, - div(on:click=move |_| { + button(on:click=move |_| { sh.dispatch(cx, Message::SelectPlanDate(date)) }) { (date_display) } }