}
}
+async fn check_user_rights_recipe_step(
+ connection: &db::Connection,
+ user: &Option<model::User>,
+ step_id: i64,
+) -> Result<()> {
+ if user.is_none()
+ || !connection
+ .can_edit_recipe_step(user.as_ref().unwrap().id, step_id)
+ .await?
+ {
+ Err(ErrorResponse::from(ron_error(
+ StatusCode::UNAUTHORIZED,
+ "Action not authorized",
+ )))
+ } else {
+ Ok(())
+ }
+}
+
+async fn check_user_rights_recipe_ingredient(
+ connection: &db::Connection,
+ user: &Option<model::User>,
+ ingredient_id: i64,
+) -> Result<()> {
+ if user.is_none()
+ || !connection
+ .can_edit_recipe_ingredient(user.as_ref().unwrap().id, ingredient_id)
+ .await?
+ {
+ Err(ErrorResponse::from(ron_error(
+ StatusCode::UNAUTHORIZED,
+ "Action not authorized",
+ )))
+ } else {
+ Ok(())
+ }
+}
+
#[debug_handler]
pub async fn set_recipe_title(
State(connection): State<db::Connection>,
State(connection): State<db::Connection>,
recipe_id: Query<RecipeId>,
) -> Result<impl IntoResponse> {
- println!("PROUT");
// Here we don't check user rights on purpose.
Ok(ron_response(
StatusCode::OK,
Ok(StatusCode::OK)
}
+#[debug_handler]
+pub async fn set_step_action(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetStepAction>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe_step(&connection, &user, ron.step_id).await?;
+ connection.set_step_action(ron.step_id, &ron.action).await?;
+ Ok(StatusCode::OK)
+}
+
+#[debug_handler]
+pub async fn set_ingredient_name(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientName>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
+ connection
+ .set_ingredient_name(ron.ingredient_id, &ron.name)
+ .await?;
+ Ok(StatusCode::OK)
+}
+
+#[debug_handler]
+pub async fn set_ingredient_comment(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientComment>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
+ connection
+ .set_ingredient_comment(ron.ingredient_id, &ron.comment)
+ .await?;
+ Ok(StatusCode::OK)
+}
+
+#[debug_handler]
+pub async fn set_ingredient_quantity(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientQuantity>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
+ connection
+ .set_ingredient_quantity(ron.ingredient_id, ron.quantity)
+ .await?;
+ Ok(StatusCode::OK)
+}
+
+#[debug_handler]
+pub async fn set_ingredient_unit(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetIngredientUnit>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe_ingredient(&connection, &user, ron.ingredient_id).await?;
+ connection
+ .set_ingredient_unit(ron.ingredient_id, &ron.unit)
+ .await?;
+ Ok(StatusCode::OK)
+}
+
///// 404 /////
#[debug_handler]
pub async fn not_found(Extension(_user): Extension<Option<model::User>>) -> impl IntoResponse {
use gloo::{console::log, events::EventListener, net::http::Request, utils::document};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
-use web_sys::{Element, HtmlInputElement, HtmlSelectElement, HtmlTextAreaElement};
+use web_sys::{Element, Event, HtmlInputElement, HtmlSelectElement, HtmlTextAreaElement};
-use common::ron_api;
+use common::ron_api::{self, Ingredient};
use crate::{
request,
toast::{self, Level},
+ utils::{by_id, select, select_and_clone, SelectExt},
};
async fn reload_recipes_list(current_recipe_id: i64) {
pub fn recipe_edit(recipe_id: i64) -> Result<(), JsValue> {
// Title.
{
- let input_title = document().get_element_by_id("input-title").unwrap();
- let mut current_title = input_title.dyn_ref::<HtmlInputElement>().unwrap().value();
- let on_input_title_blur = EventListener::new(&input_title, "blur", move |_event| {
- let title = document()
- .get_element_by_id("input-title")
- .unwrap()
- .dyn_into::<HtmlInputElement>()
- .unwrap();
+ let title: HtmlInputElement = by_id("input-title");
+ let mut current_title = title.value();
+ EventListener::new(&title.clone(), "blur", move |_event| {
if title.value() != current_title {
current_title = title.value();
let body = ron_api::SetRecipeTitle {
reload_recipes_list(recipe_id).await;
});
}
- });
- on_input_title_blur.forget();
+ })
+ .forget();
}
// Description.
{
- let text_area_description = document()
- .get_element_by_id("text-area-description")
- .unwrap();
- let mut current_description = text_area_description
- .dyn_ref::<HtmlTextAreaElement>()
- .unwrap()
- .value();
+ let description: HtmlTextAreaElement = by_id("text-area-description");
+ let mut current_description = description.value();
let on_input_description_blur =
- EventListener::new(&text_area_description, "blur", move |_event| {
- let description = document()
- .get_element_by_id("text-area-description")
- .unwrap()
- .dyn_into::<HtmlTextAreaElement>()
- .unwrap();
+ EventListener::new(&description.clone(), "blur", move |_event| {
if description.value() != current_description {
current_description = description.value();
let body = ron_api::SetRecipeDescription {
// Estimated time.
{
- let input_estimated_time = document()
- .get_element_by_id("input-estimated-time")
- .unwrap();
- let mut current_time = input_estimated_time
- .dyn_ref::<HtmlInputElement>()
- .unwrap()
- .value();
+ let estimated_time: HtmlInputElement = by_id("input-estimated-time");
+ let mut current_time = estimated_time.value_as_number();
let on_input_estimated_time_blur =
- EventListener::new(&input_estimated_time, "blur", move |_event| {
- let estimated_time = document()
- .get_element_by_id("input-estimated-time")
- .unwrap()
- .dyn_into::<HtmlInputElement>()
- .unwrap();
- if estimated_time.value() != current_time {
- let time = if estimated_time.value().is_empty() {
+ EventListener::new(&estimated_time.clone(), "blur", move |_event| {
+ let n = estimated_time.value_as_number();
+ if n.is_nan() {
+ estimated_time.set_value("");
+ }
+ if n != current_time {
+ let time = if n.is_nan() {
None
- } else if let Ok(t) = estimated_time.value().parse::<u32>() {
- Some(t)
} else {
- estimated_time.set_value(¤t_time);
- return;
+ // TODO: Find a better way to validate integer numbers.
+ let n = n as u32;
+ estimated_time.set_value_as_number(n as f64);
+ Some(n)
};
-
- current_time = estimated_time.value();
+ current_time = n;
let body = ron_api::SetRecipeEstimatedTime {
recipe_id,
estimated_time: time,
// Difficulty.
{
- let select_difficulty = document().get_element_by_id("select-difficulty").unwrap();
- let mut current_difficulty = select_difficulty
- .dyn_ref::<HtmlSelectElement>()
- .unwrap()
- .value();
+ let difficulty: HtmlSelectElement = by_id("select-difficulty");
+ let mut current_difficulty = difficulty.value();
let on_select_difficulty_blur =
- EventListener::new(&select_difficulty, "blur", move |_event| {
- let difficulty = document()
- .get_element_by_id("select-difficulty")
- .unwrap()
- .dyn_into::<HtmlSelectElement>()
- .unwrap();
+ EventListener::new(&difficulty.clone(), "blur", move |_event| {
if difficulty.value() != current_difficulty {
current_difficulty = difficulty.value();
// Language.
{
- let select_language = document().get_element_by_id("select-language").unwrap();
- let mut current_language = select_language
- .dyn_ref::<HtmlSelectElement>()
- .unwrap()
- .value();
- let on_select_language_blur = EventListener::new(&select_language, "blur", move |_event| {
- let language = document()
- .get_element_by_id("select-language")
- .unwrap()
- .dyn_into::<HtmlSelectElement>()
- .unwrap();
- if language.value() != current_language {
- current_language = language.value();
-
- let body = ron_api::SetRecipeLanguage {
- recipe_id,
- lang: language.value(),
- };
- spawn_local(async move {
- let _ = request::put::<(), _>("recipe/set_language", body).await;
- });
- }
- });
+ let language: HtmlSelectElement = by_id("select-language");
+ let mut current_language = language.value();
+ let on_select_language_blur =
+ EventListener::new(&language.clone(), "blur", move |_event| {
+ if language.value() != current_language {
+ current_language = language.value();
+
+ let body = ron_api::SetRecipeLanguage {
+ recipe_id,
+ lang: language.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_language", body).await;
+ });
+ }
+ });
on_select_language_blur.forget();
}
// Is published.
{
- let input_is_published = document().get_element_by_id("input-is-published").unwrap();
+ let is_published: HtmlInputElement = by_id("input-is-published");
let on_input_is_published_blur =
- EventListener::new(&input_is_published, "input", move |_event| {
- let is_published = document()
- .get_element_by_id("input-is-published")
- .unwrap()
- .dyn_into::<HtmlInputElement>()
- .unwrap();
-
+ EventListener::new(&is_published.clone(), "input", move |_event| {
let body = ron_api::SetIsPublished {
recipe_id,
is_published: is_published.checked(),
on_input_is_published_blur.forget();
}
- // let groups_container = document().get_element_by_id("groups-container").unwrap();
- // if !groups_container.has_child_nodes() {
-
- // }
-
- fn create_group_element(group_id: i64) -> Element {
- let group_html = document()
- .query_selector("#hidden-templates .group")
- .unwrap()
- .unwrap()
- .clone_node_with_deep(true)
- .unwrap()
- .dyn_into::<Element>()
- .unwrap();
-
- group_html
- .set_attribute("id", &format!("group-{}", group_id))
+ fn create_group_element(group: &ron_api::Group) -> Element {
+ let group_id = group.id;
+ let group_element: Element = select_and_clone("#hidden-templates .group");
+ group_element
+ .set_attribute("id", &format!("group-{}", group.id))
.unwrap();
let groups_container = document().get_element_by_id("groups-container").unwrap();
- groups_container.append_child(&group_html).unwrap();
- group_html
+ groups_container.append_child(&group_element).unwrap();
+
+ // Group name.
+ let name = group_element.select::<HtmlInputElement>(".input-group-name");
+ name.set_value(&group.name);
+ let mut current_name = group.name.clone();
+ EventListener::new(&name.clone(), "blur", move |_event| {
+ if name.value() != current_name {
+ current_name = name.value();
+ let body = ron_api::SetGroupName {
+ group_id,
+ name: name.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_group_name", body).await;
+ })
+ }
+ })
+ .forget();
+
+ // Group comment.
+ let comment: HtmlInputElement = group_element.select(".input-group-comment");
+ comment.set_value(&group.comment);
+ let mut current_comment = group.comment.clone();
+ EventListener::new(&comment.clone(), "blur", move |_event| {
+ if comment.value() != current_comment {
+ current_comment = comment.value();
+ let body = ron_api::SetGroupComment {
+ group_id,
+ comment: comment.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_group_comment", body).await;
+ });
+ }
+ })
+ .forget();
+
+ // Delete button.
+ // TODO: add a user confirmation.
+ let delete_button: HtmlInputElement = group_element.select(".input-group-delete");
+ EventListener::new(&delete_button, "click", move |_event| {
+ spawn_local(async move {
+ let body = ron_api::RemoveRecipeGroup { group_id };
+ let _ = request::delete::<(), _>("recipe/remove_group", body).await;
+ by_id::<Element>(&format!("group-{}", group_id)).remove();
+ });
+ })
+ .forget();
+
+ group_element
}
- fn create_step_element(group_element: &Element, step_id: i64) -> Element {
- let step_html = document()
- .query_selector("#hidden-templates .step")
- .unwrap()
- .unwrap()
- .clone_node_with_deep(true)
- .unwrap()
- .dyn_into::<Element>()
- .unwrap();
- step_html
- .set_attribute("id", &format!("step-{}", step_id))
+ fn create_step_element(group_element: &Element, step: &ron_api::Step) -> Element {
+ let step_id = step.id;
+ let step_element: Element = select_and_clone("#hidden-templates .step");
+ step_element
+ .set_attribute("id", &format!("step-{}", step.id))
.unwrap();
+ group_element.append_child(&step_element).unwrap();
+
+ // Step action.
+ let action: HtmlTextAreaElement = step_element.select(".text-area-step-action");
+ action.set_value(&step.action);
+ let mut current_action = step.action.clone();
+ EventListener::new(&action.clone(), "blur", move |_event| {
+ if action.value() != current_action {
+ current_action = action.value();
+ let body = ron_api::SetStepAction {
+ step_id,
+ action: action.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_step_action", body).await;
+ });
+ }
+ })
+ .forget();
- group_element.append_child(&step_html).unwrap();
- step_html
+ step_element
}
- fn create_ingredient_element(step_element: &Element, ingredient_id: i64) -> Element {
- let ingredient_html = document()
- .query_selector("#hidden-templates .ingredient")
- .unwrap()
- .unwrap()
- .clone_node_with_deep(true)
- .unwrap()
- .dyn_into::<Element>()
- .unwrap();
- ingredient_html
- .set_attribute("id", &format!("step-{}", ingredient_id))
+ fn create_ingredient_element(
+ step_element: &Element,
+ ingredient: &ron_api::Ingredient,
+ ) -> Element {
+ let ingredient_id = ingredient.id;
+ let ingredient_element: Element = select_and_clone("#hidden-templates .ingredient");
+ ingredient_element
+ .set_attribute("id", &format!("step-{}", ingredient.id))
.unwrap();
+ step_element.append_child(&ingredient_element).unwrap();
+
+ // Ingredient name.
+ let name: HtmlInputElement = ingredient_element.select(".input-ingredient-name");
+ name.set_value(&ingredient.name);
+ let mut current_name = ingredient.name.clone();
+ EventListener::new(&name.clone(), "blur", move |_event| {
+ if name.value() != current_name {
+ current_name = name.value();
+ let body = ron_api::SetIngredientName {
+ ingredient_id,
+ name: name.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_ingredient_name", body).await;
+ });
+ }
+ })
+ .forget();
+
+ // Ingredient comment.
+ let comment: HtmlInputElement = ingredient_element.select(".input-ingredient-comment");
+ comment.set_value(&ingredient.comment);
+ let mut current_comment = ingredient.comment.clone();
+ EventListener::new(&comment.clone(), "blur", move |_event| {
+ if comment.value() != current_comment {
+ current_comment = comment.value();
+ let body = ron_api::SetIngredientComment {
+ ingredient_id,
+ comment: comment.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_ingredient_comment", body).await;
+ });
+ }
+ })
+ .forget();
+
+ // 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;
+ EventListener::new(&quantity.clone(), "blur", move |_event| {
+ let n = quantity.value_as_number();
+ if n.is_nan() {
+ quantity.set_value("");
+ }
+ if n != current_quantity {
+ let q = if n.is_nan() { None } else { Some(n) };
+ current_quantity = n;
+ let body = ron_api::SetIngredientQuantity {
+ ingredient_id,
+ quantity: q,
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_ingredient_quantity", body).await;
+ });
+ }
+ })
+ .forget();
+
+ // Ingredient unit.
+ let unit: HtmlInputElement = ingredient_element.select(".input-ingredient-unit");
+ unit.set_value(&ingredient.quantity_unit);
+ let mut current_unit = ingredient.quantity_unit.clone();
+ EventListener::new(&unit.clone(), "blur", move |_event| {
+ if unit.value() != current_unit {
+ current_unit = unit.value();
+ let body = ron_api::SetIngredientUnit {
+ ingredient_id,
+ unit: unit.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_ingredient_unit", body).await;
+ });
+ }
+ })
+ .forget();
- step_element.append_child(&ingredient_html).unwrap();
- ingredient_html
+ ingredient_element
}
// Load initial groups, steps and ingredients.
.unwrap();
for group in groups {
- let group_element = create_group_element(group.id);
- let input_name = group_element
- .query_selector(".input-group-name")
- .unwrap()
- .unwrap()
- .dyn_into::<HtmlInputElement>()
- .unwrap();
- input_name.set_value(&group.name);
-
- // document().get_element_by_id(&format!("group-{}", group_id))
+ let group_element = create_group_element(&group);
for step in group.steps {
- let step_element = create_step_element(&group_element, step.id);
- let text_area_action = step_element
- .query_selector(".text-area-step-action")
- .unwrap()
- .unwrap()
- .dyn_into::<HtmlTextAreaElement>()
- .unwrap();
- text_area_action.set_value(&step.action);
+ let step_element = create_step_element(&group_element, &step);
for ingredient in step.ingredients {
- let ingredient_element =
- create_ingredient_element(&step_element, ingredient.id);
- let input_name = ingredient_element
- .query_selector(".input-ingredient-name")
- .unwrap()
- .unwrap()
- .dyn_into::<HtmlInputElement>()
- .unwrap();
- input_name.set_value(&ingredient.name);
+ create_ingredient_element(&step_element, &ingredient);
}
}
}
-
- // log!(format!("{:?}", groups));
});
}
spawn_local(async move {
let response: ron_api::AddRecipeGroupResult =
request::post("recipe/add_group", body).await.unwrap();
- create_group_element(response.group_id);
+ create_group_element(&ron_api::Group {
+ id: response.group_id,
+ name: "".to_string(),
+ comment: "".to_string(),
+ steps: vec![],
+ });
// group_html.set_attribute("id", "test").unwrap();
});
});