diff --git a/app/src/recipe.rs b/app/src/recipe.rs index 6e4ac17..377f359 100644 --- a/app/src/recipe.rs +++ b/app/src/recipe.rs @@ -1,8 +1,8 @@ use std::rc::Rc; use api::{ - AddRecipeIngredientRequest, IngredientInfo, RecipeEditRating, RecipeEditStepsRequest, - RecipeInfo, RecipeIngredientEditRequest, RecipeRenameRequest, + AddRecipeIngredientRequest, IngredientInfo, RecipeEditPersonCount, RecipeEditRating, + RecipeEditStepsRequest, RecipeInfo, RecipeIngredientEditRequest, RecipeRenameRequest, }; use itertools::Itertools; use uuid::Uuid; @@ -758,6 +758,122 @@ fn EditRating(props: &EditRatingProps) -> Html { } } +async fn do_edit_person_count( + token: String, + household: Uuid, + recipe: i64, + person_count: u32, +) -> anyhow::Result<()> { + let rsp = + gloo_net::http::Request::patch(api!("household/{household}/recipe/{recipe}/person_count")) + .json(&RecipeEditPersonCount { person_count })? + .header("Authorization", &format!("Bearer {token}")) + .send() + .await?; + + if !rsp.ok() { + let body = rsp.text().await.unwrap_or_default(); + anyhow::bail!( + "Could not edit person_count (code={}): {body}", + rsp.status() + ); + } + + Ok(()) +} + +#[derive(Properties, PartialEq, Clone)] +struct EditPersonCountProps { + token: String, + household: Uuid, + recipe: i64, + person_count: u32, +} + +#[function_component] +fn EditPersonCount(props: &EditPersonCountProps) -> Html { + let person_count = use_state(|| props.person_count); + + let error = use_state(|| None::); + + let onchange = { + let person_count = person_count.clone(); + Callback::from(move |e: Event| { + let Some(target) = e.target() else { + return; + }; + + let Ok(target) = target.dyn_into::() else { + return; + }; + + if !target.report_validity() { + return; + } + + person_count.set(target.value().parse().expect("invalid number")); + }) + }; + let on_submit = { + let person_count = person_count.clone(); + let token = props.token.clone(); + let household = props.household; + let recipe = props.recipe; + let error = error.clone(); + Callback::from(move |_| { + let token = token.clone(); + let person_count = person_count.clone(); + let error = error.clone(); + + wasm_bindgen_futures::spawn_local(async move { + match do_edit_person_count(token.clone(), household, recipe, *person_count) + .await + { + Ok(_) => { + let modal = bs::Modal::get_instance("#rcpEditPersonCount"); + modal.hide(); + error.set(None); + } + Err(e) => { + error.set(Some(format!("Could not edit person count: {e}"))); + } + } + }); + }) + }; + + html! {<> + + {"Edit"} + + + if let Some(e) = &*error { + + } +
+ + +
+
+ } +} + #[derive(Properties, PartialEq, Clone)] struct RecipeInfoProps { token: String, @@ -832,17 +948,29 @@ fn RecipeInfoView(props: &RecipeInfoProps) -> Html { update={props.update.clone()} /> -
-
- - {"people"} +
+
+
+
+ + {"people"} +
+
+
+ +
if let Some(e) = &*error {