Various bugfixes

This commit is contained in:
Jeremy Wall 2023-01-18 20:12:29 -05:00
parent 47183b6e7a
commit 2267744087
3 changed files with 28 additions and 10 deletions

View File

@ -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<S: AsRef<str> + 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(())
}

View File

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

View File

@ -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<Message, AppState> 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<Message, AppState> 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;