Recipe edit (WIP): Buttons to add steps and inrgedients

This commit is contained in:
Greg Burri 2024-12-27 12:51:29 +01:00
parent 6876a254e1
commit d4962c98ff
9 changed files with 182 additions and 15 deletions

View file

@ -220,6 +220,25 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
})
.forget();
// Add step button.
let add_step_button: HtmlInputElement = group_element.select(".input-add-step");
EventListener::new(&add_step_button, "click", move |_event| {
spawn_local(async move {
let body = ron_api::AddRecipeStep { group_id };
let response: ron_api::AddRecipeStepResult =
request::post("recipe/add_step", body).await.unwrap();
create_step_element(
&by_id::<Element>(&format!("group-{}", group_id)),
&ron_api::Step {
id: response.step_id,
action: "".to_string(),
ingredients: vec![],
},
);
});
})
.forget();
group_element
}
@ -249,6 +268,27 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
})
.forget();
// Add ingredient button.
let add_ingredient_button: HtmlInputElement = step_element.select(".input-add-ingredient");
EventListener::new(&add_ingredient_button, "click", move |_event| {
spawn_local(async move {
let body = ron_api::AddRecipeIngredient { step_id };
let response: ron_api::AddRecipeIngredientResult =
request::post("recipe/add_ingredient", body).await.unwrap();
create_ingredient_element(
&by_id::<Element>(&format!("step-{}", step_id)),
&ron_api::Ingredient {
id: response.ingredient_id,
name: "".to_string(),
comment: "".to_string(),
quantity_value: None,
quantity_unit: "".to_string(),
},
);
});
})
.forget();
step_element
}
@ -259,7 +299,7 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
let ingredient_id = ingredient.id;
let ingredient_element: Element = select_and_clone("#hidden-templates .ingredient");
ingredient_element
.set_attribute("id", &format!("step-{}", ingredient.id))
.set_attribute("id", &format!("ingredient-{}", ingredient.id))
.unwrap();
step_element.append_child(&ingredient_element).unwrap();
@ -301,8 +341,12 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
// Ingredient quantity.
let quantity: HtmlInputElement = ingredient_element.select(".input-ingredient-quantity");
quantity.set_value(&ingredient.quantity_value.to_string());
let mut current_quantity = ingredient.quantity_value;
quantity.set_value(
&ingredient
.quantity_value
.map_or("".to_string(), |q| q.to_string()),
);
let mut current_quantity = quantity.value_as_number();
EventListener::new(&quantity.clone(), "blur", move |_event| {
let n = quantity.value_as_number();
if n.is_nan() {
@ -367,11 +411,9 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
// Add a new group.
{
let button_add_group = document().get_element_by_id("button-add-group").unwrap();
let button_add_group: HtmlInputElement = by_id("input-add-group");
let on_click_add_group = EventListener::new(&button_add_group, "click", move |_event| {
log!("Click!");
let body = ron_api::AddRecipeGroup { recipe_id };
spawn_local(async move {
let response: ron_api::AddRecipeGroupResult =
request::post("recipe/add_group", body).await.unwrap();
@ -381,7 +423,6 @@ pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
comment: "".to_string(),
steps: vec![],
});
// group_html.set_attribute("id", "test").unwrap();
});
});
on_click_add_group.forget();