Handle saving and loading for specific plan_dates

This commit is contained in:
Jeremy Wall 2023-01-17 19:42:48 -05:00
parent 80b6126e10
commit 0a68c3d51c
2 changed files with 85 additions and 15 deletions

View File

@ -572,11 +572,26 @@ 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));
}
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::<Vec<(String, String)>>(),
cached_plan_date,
)
.await
} else {
debug!("Saving plan data");
self.store_plan(plan).await?;
debug!("Saving inventory data");
@ -591,6 +606,7 @@ impl HttpStore {
)
.await
}
}
pub async fn store_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> {
let mut path = self.v2_path();
@ -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<Option<Vec<NaiveDate>>, 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<IngredientKey>,
modified_amts: BTreeMap<IngredientKey, String>,
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<IngredientKey> = 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,

View File

@ -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) }
}