ron_utils::{ron_error, ron_response},
};
+const NOT_AUTHORIZED_MESSAGE: &str = "Action not authorized";
+
#[allow(dead_code)]
#[debug_handler]
pub async fn update_user(
} else {
return Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
- "Action not authorized",
+ NOT_AUTHORIZED_MESSAGE,
)));
}
Ok(StatusCode::OK)
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
- "Action not authorized",
+ NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
- "Action not authorized",
+ NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
- "Action not authorized",
+ NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
{
Err(ErrorResponse::from(ron_error(
StatusCode::UNAUTHORIZED,
- "Action not authorized",
+ NOT_AUTHORIZED_MESSAGE,
)))
} else {
Ok(())
Ok(StatusCode::OK)
}
+#[debug_handler]
+pub async fn set_servings(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::SetRecipeServings>,
+) -> Result<StatusCode> {
+ check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
+ connection
+ .set_recipe_servings(ron.recipe_id, ron.servings)
+ .await?;
+ Ok(StatusCode::OK)
+}
+
#[debug_handler]
pub async fn set_estimated_time(
State(connection): State<db::Connection>,
Ok(StatusCode::OK)
}
+#[debug_handler]
+pub async fn rm(
+ State(connection): State<db::Connection>,
+ Extension(user): Extension<Option<model::User>>,
+ ExtractRon(ron): ExtractRon<common::ron_api::Remove>,
+) -> Result<impl IntoResponse> {
+ check_user_rights_recipe(&connection, &user, ron.recipe_id).await?;
+ connection.rm_recipe(ron.recipe_id).await?;
+ Ok(StatusCode::OK)
+}
+
impl From<model::Group> for common::ron_api::Group {
fn from(group: model::Group) -> Self {
Self {
{
let description: HtmlTextAreaElement = by_id("text-area-description");
let mut current_description = description.value();
- let on_input_description_blur =
- EventListener::new(&description.clone(), "blur", move |_event| {
- if description.value() != current_description {
- current_description = description.value();
- let body = ron_api::SetRecipeDescription {
- recipe_id,
- description: description.value(),
- };
- spawn_local(async move {
- let _ = request::put::<(), _>("recipe/set_description", body).await;
- });
- }
- });
- on_input_description_blur.forget();
+
+ EventListener::new(&description.clone(), "blur", move |_event| {
+ if description.value() != current_description {
+ current_description = description.value();
+ let body = ron_api::SetRecipeDescription {
+ recipe_id,
+ description: description.value(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_description", body).await;
+ });
+ }
+ })
+ .forget();
+ }
+
+ // Servings.
+ {
+ let servings: HtmlInputElement = by_id("input-servings");
+ let mut current_servings = servings.value_as_number();
+ EventListener::new(&servings.clone(), "input", move |_event| {
+ let n = servings.value_as_number();
+ if n.is_nan() {
+ servings.set_value("");
+ }
+ if n != current_servings {
+ let servings = if n.is_nan() {
+ None
+ } else {
+ // TODO: Find a better way to validate integer numbers.
+ let n = n as u32;
+ servings.set_value_as_number(n as f64);
+ Some(n)
+ };
+ current_servings = n;
+ let body = ron_api::SetRecipeServings {
+ recipe_id,
+ servings,
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_servings", body).await;
+ });
+ }
+ })
+ .forget();
}
// Estimated time.
{
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(&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 {
- // 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 = n;
- let body = ron_api::SetRecipeEstimatedTime {
- recipe_id,
- estimated_time: time,
- };
- spawn_local(async move {
- let _ = request::put::<(), _>("recipe/set_estimated_time", body).await;
- });
- }
- });
- on_input_estimated_time_blur.forget();
+
+ EventListener::new(&estimated_time.clone(), "input", 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 {
+ // 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 = n;
+ let body = ron_api::SetRecipeEstimatedTime {
+ recipe_id,
+ estimated_time: time,
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_estimated_time", body).await;
+ });
+ }
+ })
+ .forget();
}
// Difficulty.
{
let difficulty: HtmlSelectElement = by_id("select-difficulty");
let mut current_difficulty = difficulty.value();
- let on_select_difficulty_blur =
- EventListener::new(&difficulty.clone(), "blur", move |_event| {
- if difficulty.value() != current_difficulty {
- current_difficulty = difficulty.value();
-
- let body = ron_api::SetRecipeDifficulty {
- recipe_id,
- difficulty: ron_api::Difficulty::try_from(
- current_difficulty.parse::<u32>().unwrap(),
- )
- .unwrap(),
- };
- spawn_local(async move {
- let _ = request::put::<(), _>("recipe/set_difficulty", body).await;
- });
- }
- });
- on_select_difficulty_blur.forget();
+
+ EventListener::new(&difficulty.clone(), "blur", move |_event| {
+ if difficulty.value() != current_difficulty {
+ current_difficulty = difficulty.value();
+
+ let body = ron_api::SetRecipeDifficulty {
+ recipe_id,
+ difficulty: ron_api::Difficulty::try_from(
+ current_difficulty.parse::<u32>().unwrap(),
+ )
+ .unwrap(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_difficulty", body).await;
+ });
+ }
+ })
+ .forget();
}
// Language.
{
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();
- }
+ EventListener::new(&language.clone(), "blur", move |_event| {
+ if language.value() != current_language {
+ current_language = language.value();
- // Is published.
- {
- let is_published: HtmlInputElement = by_id("input-is-published");
- let on_input_is_published_blur =
- EventListener::new(&is_published.clone(), "input", move |_event| {
- let body = ron_api::SetIsPublished {
+ let body = ron_api::SetRecipeLanguage {
recipe_id,
- is_published: is_published.checked(),
+ lang: language.value(),
};
spawn_local(async move {
- let _ = request::put::<(), _>("recipe/set_is_published", body).await;
- reload_recipes_list(recipe_id).await;
+ let _ = request::put::<(), _>("recipe/set_language", body).await;
});
+ }
+ })
+ .forget();
+ }
+
+ // Is published.
+ {
+ let is_published: HtmlInputElement = by_id("input-is-published");
+ EventListener::new(&is_published.clone(), "input", move |_event| {
+ let body = ron_api::SetIsPublished {
+ recipe_id,
+ is_published: is_published.checked(),
+ };
+ spawn_local(async move {
+ let _ = request::put::<(), _>("recipe/set_is_published", body).await;
+ reload_recipes_list(recipe_id).await;
});
- on_input_is_published_blur.forget();
+ })
+ .forget();
}
+ // Delete recipe button.
+ let delete_button: HtmlInputElement = by_id("input-delete");
+ EventListener::new(&delete_button, "click", move |_event| {
+ let title: HtmlInputElement = by_id("input-title");
+ spawn_local(async move {
+ if modal_dialog::show(&format!(
+ "Are you sure to delete the recipe '{}'",
+ title.value()
+ ))
+ .await
+ {
+ let body = ron_api::Remove { recipe_id };
+ let _ = request::delete::<(), _>("recipe/remove", body).await;
+
+ // by_id::<Element>(&format!("group-{}", group_id)).remove();
+ }
+ });
+ })
+ .forget();
+
fn create_group_element(group: &ron_api::Group) -> Element {
let group_id = group.id;
let group_element: Element = select_and_clone("#hidden-templates .group");
.map_or("".to_string(), |q| q.to_string()),
);
let mut current_quantity = quantity.value_as_number();
- EventListener::new(&quantity.clone(), "blur", move |_event| {
+ EventListener::new(&quantity.clone(), "input", move |_event| {
let n = quantity.value_as_number();
if n.is_nan() {
quantity.set_value("");
Ok(())
}
-
-// pub fn user_edit(doc: Document) -> Result<(), JsValue> {
-// log!("user_edit");
-
-// let button = doc
-// .query_selector("#user-edit input[type='button']")?
-// .unwrap();
-
-// let on_click_submit = EventListener::new(&button, "click", move |_event| {
-// log!("Click!");
-
-// let input_name = doc.get_element_by_id("input-name").unwrap();
-// let name = input_name.dyn_ref::<HtmlInputElement>().unwrap().value();
-
-// let update_data = common::ron_api::UpdateProfile {
-// name: Some(name),
-// email: None,
-// password: None,
-// };
-
-// let body = common::ron_api::to_string(update_data);
-
-// let doc = doc.clone();
-// spawn_local(async move {
-// match Request::put("/ron-api/user/update")
-// .header("Content-Type", "application/ron")
-// .body(body)
-// .unwrap()
-// .send()
-// .await
-// {
-// Ok(resp) => {
-// log!("Status code: {}", resp.status());
-// if resp.status() == 200 {
-// toast::show(Level::Info, "Profile saved");
-// } else {
-// toast::show(
-// Level::Error,
-// &format!(
-// "Status code: {} {}",
-// resp.status(),
-// resp.text().await.unwrap()
-// ),
-// );
-// }
-// }
-// Err(error) => {
-// toast::show(Level::Info, &format!("Internal server error: {}", error));
-// }
-// }
-// });
-// });
-
-// on_click_submit.forget();
-
-// Ok(())
-// }