Save the full state each time

This commit is contained in:
Jeremy Wall 2022-11-24 10:28:02 -05:00
parent db470ddc36
commit 5480074d3a
4 changed files with 17 additions and 13 deletions

View File

@ -327,6 +327,19 @@ impl HttpStore {
} }
} }
pub async fn save_state(&self, state: std::rc::Rc<app_state::State>) -> 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> { pub async fn save_plan(&self, plan: Vec<(String, i32)>) -> Result<(), Error> {
let mut path = self.root.clone(); let mut path = self.root.clone();
path.push_str("/plan"); path.push_str("/plan");

View File

@ -18,6 +18,7 @@ use tracing::{debug, instrument, warn};
use recipes::{Ingredient, IngredientAccumulator, IngredientKey, Recipe}; use recipes::{Ingredient, IngredientAccumulator, IngredientKey, Recipe};
#[derive(Debug)]
pub struct State { pub struct State {
pub recipe_counts: RcSignal<BTreeMap<String, RcSignal<usize>>>, pub recipe_counts: RcSignal<BTreeMap<String, RcSignal<usize>>>,
pub extras: RcSignal<Vec<(usize, (RcSignal<String>, RcSignal<String>))>>, pub extras: RcSignal<Vec<(usize, (RcSignal<String>, RcSignal<String>))>>,

View File

@ -56,12 +56,8 @@ pub fn RecipePlan<G: Html>(cx: Scope) -> View<G> {
let store = HttpStore::get_from_context(cx); let store = HttpStore::get_from_context(cx);
let state = app_state::State::get_from_context(cx); let state = app_state::State::get_from_context(cx);
spawn_local_scoped(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 { async move {
store.save_plan(plan).await.expect("Failed to save plan"); store.save_state(state).await.expect("Failed to save plan");
} }
}) })
}); });

View File

@ -191,16 +191,10 @@ pub fn ShoppingList<G: Html>(cx: Scope) -> View<G> {
spawn_local_scoped(cx, { spawn_local_scoped(cx, {
let state = crate::app_state::State::get_from_context(cx); let state = crate::app_state::State::get_from_context(cx);
let store = crate::api::HttpStore::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 { async move {
debug!( debug!(?state, "Attempting save for inventory");
?filtered_ingredients,
?modified_amts,
"Attempting save for inventory"
);
store store
.save_inventory_data(filtered_ingredients, modified_amts) .save_state(state)
.await .await
.expect("Unable to save inventory data"); .expect("Unable to save inventory data");
} }