mirror of
https://github.com/zaphar/kitchen.git
synced 2025-07-22 19:40:14 -04:00
Add plan deletion to the UI
This commit is contained in:
parent
ad62c5f31f
commit
47183b6e7a
@ -519,7 +519,9 @@ fn mk_v2_routes() -> Router {
|
|||||||
.route("/plan/since/:date", get(api_plan_since))
|
.route("/plan/since/:date", get(api_plan_since))
|
||||||
.route(
|
.route(
|
||||||
"/plan/at/:date",
|
"/plan/at/:date",
|
||||||
get(api_plan_for_date).post(api_save_plan_for_date),
|
get(api_plan_for_date)
|
||||||
|
.post(api_save_plan_for_date)
|
||||||
|
.delete(api_delete_plan_for_date),
|
||||||
)
|
)
|
||||||
.route("/plan/all", get(api_all_plans))
|
.route("/plan/all", get(api_all_plans))
|
||||||
.route(
|
.route(
|
||||||
|
@ -257,6 +257,13 @@ impl LocalStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn delete_plan_for_date(&self, date: &NaiveDate) {
|
||||||
|
self.store.delete("plan").expect("Failed to delete plan");
|
||||||
|
self.store
|
||||||
|
.delete("inventory")
|
||||||
|
.expect("Failed to delete inventory data");
|
||||||
|
}
|
||||||
|
|
||||||
pub fn set_plan_date(&self, date: &NaiveDate) {
|
pub fn set_plan_date(&self, date: &NaiveDate) {
|
||||||
self.store
|
self.store
|
||||||
.set(
|
.set(
|
||||||
@ -664,6 +671,19 @@ impl HttpStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn delete_plan_for_date(&self, 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::delete(&path).send().await?;
|
||||||
|
if resp.status() != 200 {
|
||||||
|
Err(format!("Status: {}", resp.status()).into())
|
||||||
|
} else {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn fetch_plan_for_date(
|
pub async fn fetch_plan_for_date(
|
||||||
&self,
|
&self,
|
||||||
date: &NaiveDate,
|
date: &NaiveDate,
|
||||||
|
@ -75,6 +75,7 @@ pub enum Message {
|
|||||||
SaveState(Option<Box<dyn FnOnce()>>),
|
SaveState(Option<Box<dyn FnOnce()>>),
|
||||||
LoadState(Option<Box<dyn FnOnce()>>),
|
LoadState(Option<Box<dyn FnOnce()>>),
|
||||||
UpdateStaples(String, Option<Box<dyn FnOnce()>>),
|
UpdateStaples(String, Option<Box<dyn FnOnce()>>),
|
||||||
|
DeletePlan(NaiveDate, Option<Box<dyn FnOnce()>>),
|
||||||
SelectPlanDate(NaiveDate, Option<Box<dyn FnOnce()>>),
|
SelectPlanDate(NaiveDate, Option<Box<dyn FnOnce()>>),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,6 +118,7 @@ impl Debug for Message {
|
|||||||
Self::LoadState(_) => write!(f, "LoadState"),
|
Self::LoadState(_) => write!(f, "LoadState"),
|
||||||
Self::UpdateStaples(arg, _) => f.debug_tuple("UpdateStaples").field(arg).finish(),
|
Self::UpdateStaples(arg, _) => f.debug_tuple("UpdateStaples").field(arg).finish(),
|
||||||
Self::SelectPlanDate(arg, _) => f.debug_tuple("SelectPlanDate").field(arg).finish(),
|
Self::SelectPlanDate(arg, _) => f.debug_tuple("SelectPlanDate").field(arg).finish(),
|
||||||
|
Self::DeletePlan(arg, _) => f.debug_tuple("DeletePlan").field(arg).finish(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -484,6 +486,28 @@ impl MessageMapper<Message, AppState> for StateMachine {
|
|||||||
// the original signal.
|
// the original signal.
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
Message::DeletePlan(date, callback) => {
|
||||||
|
let store = self.store.clone();
|
||||||
|
let local_store = self.local_store.clone();
|
||||||
|
spawn_local_scoped(cx, async move {
|
||||||
|
store
|
||||||
|
.delete_plan_for_date(&date)
|
||||||
|
.await
|
||||||
|
.expect("Failed to delete meal plan for date");
|
||||||
|
local_store.delete_plan_for_date(&date);
|
||||||
|
|
||||||
|
original_copy.plan_dates.remove(&date);
|
||||||
|
// Reset all meal planning state;
|
||||||
|
let _ = original_copy.recipe_counts.iter_mut().map(|(_, v)| *v = 0);
|
||||||
|
original_copy.filtered_ingredients = BTreeSet::new();
|
||||||
|
original_copy.modified_amts = BTreeMap::new();
|
||||||
|
original_copy.extras = Vec::new();
|
||||||
|
original.set(original_copy);
|
||||||
|
|
||||||
|
callback.map(|f| f());
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
original.set(original_copy);
|
original.set(original_copy);
|
||||||
}
|
}
|
||||||
|
@ -42,6 +42,11 @@ pub fn PlanList<'ctx, G: Html>(cx: Scope<'ctx>, props: PlanListProps<'ctx>) -> V
|
|||||||
sh.dispatch(cx, Message::SelectPlanDate(date, None))
|
sh.dispatch(cx, Message::SelectPlanDate(date, None))
|
||||||
}) { (date_display) }
|
}) { (date_display) }
|
||||||
}
|
}
|
||||||
|
td() {
|
||||||
|
span(role="button", class="destructive", on:click=move |_| {
|
||||||
|
sh.dispatch(cx, Message::DeletePlan(date, None))
|
||||||
|
}) { "Delete Plan" }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -50,7 +50,7 @@ pub fn RecipeSelection<'ctx, G: Html>(
|
|||||||
.get()
|
.get()
|
||||||
.recipe_counts
|
.recipe_counts
|
||||||
.get(id_for_count.as_ref())
|
.get(id_for_count.as_ref())
|
||||||
.unwrap()
|
.unwrap_or(&0)
|
||||||
});
|
});
|
||||||
let count = create_signal(cx, format!("{}", *current_count.get_untracked()));
|
let count = create_signal(cx, format!("{}", *current_count.get_untracked()));
|
||||||
create_effect(cx, || {
|
create_effect(cx, || {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user