app: Correctly handle person count when editing ingredients

This commit is contained in:
traxys 2023-06-30 19:38:10 +02:00
parent 725c2ff3be
commit 66f9b729b5

View file

@ -262,6 +262,7 @@ struct EditIngredientProps {
recipe: i64, recipe: i64,
ingredient: i64, ingredient: i64,
amount: f64, amount: f64,
person_count: u32,
update: Callback<()>, update: Callback<()>,
} }
@ -290,7 +291,7 @@ async fn do_edit_ingredient_recipe(
#[function_component] #[function_component]
fn EditIngredient(props: &EditIngredientProps) -> Html { fn EditIngredient(props: &EditIngredientProps) -> Html {
let amount = use_state(|| props.amount); let amount = use_state(|| props.amount * props.person_count as f64);
let am = amount.clone(); let am = amount.clone();
let onchange = Callback::from(move |e: Event| { let onchange = Callback::from(move |e: Event| {
@ -317,8 +318,10 @@ fn EditIngredient(props: &EditIngredientProps) -> Html {
let ingredient = props.ingredient; let ingredient = props.ingredient;
let update = props.update.clone(); let update = props.update.clone();
let mid = modal_id.clone(); let mid = modal_id.clone();
let pc = props.person_count as f64;
let on_submit = Callback::from(move |_| { let on_submit = Callback::from(move |_| {
let future = do_edit_ingredient_recipe(token.clone(), household, recipe, ingredient, *am); let future =
do_edit_ingredient_recipe(token.clone(), household, recipe, ingredient, *am / pc);
let err = err.clone(); let err = err.clone();
let update = update.clone(); let update = update.clone();
@ -826,9 +829,7 @@ fn EditPersonCount(props: &EditPersonCountProps) -> Html {
let error = error.clone(); let error = error.clone();
wasm_bindgen_futures::spawn_local(async move { wasm_bindgen_futures::spawn_local(async move {
match do_edit_person_count(token.clone(), household, recipe, *person_count) match do_edit_person_count(token.clone(), household, recipe, *person_count).await {
.await
{
Ok(_) => { Ok(_) => {
let modal = bs::Modal::get_instance("#rcpEditPersonCount"); let modal = bs::Modal::get_instance("#rcpEditPersonCount");
modal.hide(); modal.hide();
@ -1012,6 +1013,7 @@ fn RecipeInfoView(props: &RecipeInfoProps) -> Html {
ingredient={*id} ingredient={*id}
household={props.household} household={props.household}
amount={*amount} amount={*amount}
person_count={*person_count}
update={props.update.clone()} update={props.update.clone()}
/> />
</div> </div>