Recipe edit (WIP): add API to set some recipe values

This commit is contained in:
Greg Burri 2024-12-23 01:37:01 +01:00
parent c6dfff065c
commit dd05a673d9
20 changed files with 690 additions and 2189 deletions

View file

@ -1,73 +1,254 @@
use gloo::{console::log, events::EventListener, net::http::Request};
use gloo::{console::log, events::EventListener, net::http::Request, utils::document};
use wasm_bindgen::prelude::*;
use wasm_bindgen_futures::spawn_local;
use web_sys::{Document, HtmlInputElement};
use web_sys::{Document, HtmlInputElement, HtmlSelectElement};
use crate::toast::{self, Level};
pub fn recipe_edit(doc: Document) -> Result<(), JsValue> {
let title_input = doc.get_element_by_id("title_field").unwrap();
Ok(())
async fn api_request(body: String, api_name: &str) {
if let Err(error) = Request::put(&format!("/ron-api/recipe/{}", api_name))
.header("Content-Type", "application/ron")
.body(body)
.unwrap()
.send()
.await
{
toast::show(Level::Info, &format!("Internal server error: {}", error));
}
}
pub fn user_edit(doc: Document) -> Result<(), JsValue> {
log!("user_edit");
async fn reload_recipes_list() {
match Request::get("/fragments/recipes_list").send().await {
Err(error) => {
toast::show(Level::Info, &format!("Internal server error: {}", error));
}
Ok(response) => {
let list = document().get_element_by_id("recipes-list").unwrap();
list.set_outer_html(&response.text().await.unwrap());
}
}
}
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", doc);
} else {
toast::show(
Level::Error,
&format!(
"Status code: {} {}",
resp.status(),
resp.text().await.unwrap()
),
doc,
);
}
}
Err(error) => {
toast::show(
Level::Info,
&format!("Internal server error: {}", error),
doc,
);
}
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 input_title = document().get_element_by_id("input-title").unwrap();
let title = input_title.dyn_ref::<HtmlInputElement>().unwrap();
if title.value() != current_title {
current_title = title.value();
let body = common::ron_api::to_string(common::ron_api::SetRecipeTitle {
recipe_id,
title: title.value(),
});
spawn_local(async move {
api_request(body, "set_title").await;
reload_recipes_list().await;
});
}
});
});
on_input_title_blur.forget();
}
on_click_submit.forget();
// Description.
{
let input_description = document().get_element_by_id("input-description").unwrap();
let mut current_description = input_description
.dyn_ref::<HtmlInputElement>()
.unwrap()
.value();
let on_input_description_blur =
EventListener::new(&input_description, "blur", move |_event| {
let input_description = document().get_element_by_id("input-description").unwrap();
let description = input_description.dyn_ref::<HtmlInputElement>().unwrap();
if description.value() != current_description {
current_description = description.value();
let body = common::ron_api::to_string(common::ron_api::SetRecipeDescription {
recipe_id,
description: description.value(),
});
spawn_local(async move {
api_request(body, "set_description").await;
});
}
});
on_input_description_blur.forget();
}
// 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 on_input_estimated_time_blur =
EventListener::new(&input_estimated_time, "blur", move |_event| {
let input_estimated_time = document()
.get_element_by_id("input-estimated-time")
.unwrap();
let estimated_time = input_estimated_time.dyn_ref::<HtmlInputElement>().unwrap();
if estimated_time.value() != current_time {
let time = if estimated_time.value().is_empty() {
None
} else {
if let Ok(t) = estimated_time.value().parse::<u32>() {
Some(t)
} else {
estimated_time.set_value(&current_time);
return;
}
};
current_time = estimated_time.value();
let body =
common::ron_api::to_string(common::ron_api::SetRecipeEstimatedTime {
recipe_id,
estimated_time: time,
});
spawn_local(async move {
api_request(body, "set_estimated_time").await;
});
}
});
on_input_estimated_time_blur.forget();
}
// Difficulty.
{
let select_difficulty = document().get_element_by_id("select-difficulty").unwrap();
let mut current_difficulty = select_difficulty
.dyn_ref::<HtmlSelectElement>()
.unwrap()
.value();
let on_select_difficulty_blur =
EventListener::new(&select_difficulty, "blur", move |_event| {
let select_difficulty = document().get_element_by_id("select-difficulty").unwrap();
let difficulty = select_difficulty.dyn_ref::<HtmlSelectElement>().unwrap();
if difficulty.value() != current_difficulty {
current_difficulty = difficulty.value();
let body = common::ron_api::to_string(common::ron_api::SetRecipeDifficulty {
recipe_id,
difficulty: common::ron_api::Difficulty::try_from(
current_difficulty.parse::<u32>().unwrap(),
)
.unwrap(),
});
spawn_local(async move {
api_request(body, "set_difficulty").await;
});
}
});
on_select_difficulty_blur.forget();
}
// 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 select_language = document().get_element_by_id("select-language").unwrap();
let difficulty = select_language.dyn_ref::<HtmlSelectElement>().unwrap();
if difficulty.value() != current_language {
current_language = difficulty.value();
let body = common::ron_api::to_string(common::ron_api::SetRecipeLanguage {
recipe_id,
lang: difficulty.value(),
});
spawn_local(async move {
api_request(body, "set_language").await;
});
}
});
on_select_language_blur.forget();
}
// Is published.
{
let input_is_published = document().get_element_by_id("input-is-published").unwrap();
let on_input_is_published_blur =
EventListener::new(&input_is_published, "input", move |_event| {
let input_is_published =
document().get_element_by_id("input-is-published").unwrap();
let is_published = input_is_published.dyn_ref::<HtmlInputElement>().unwrap();
let body = common::ron_api::to_string(common::ron_api::SetIsPublished {
recipe_id,
is_published: is_published.checked(),
});
spawn_local(async move {
api_request(body, "set_is_published").await;
reload_recipes_list().await;
});
});
on_input_is_published_blur.forget();
}
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(())
// }