* Create a minimalistic toast

* Profile editing (WIP)
This commit is contained in:
Greg Burri 2024-12-04 17:39:56 +01:00
parent 327b2d0a5b
commit 1c79cc890d
25 changed files with 1133 additions and 575 deletions

View file

@ -1,10 +1,73 @@
use gloo::{console::log, events::EventListener, net::http::Request};
use wasm_bindgen::prelude::*;
use web_sys::{Document, HtmlLabelElement};
use wasm_bindgen_futures::spawn_local;
use web_sys::{Document, HtmlInputElement};
pub fn recipe_edit(doc: &Document) {
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(())
}
pub fn user_edit(doc: &Document) {
// let name_input = doc.get_element_by_id("name_field").unwrap().dyn_ref::<>()
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", 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,
);
}
}
});
});
on_click_submit.forget();
Ok(())
}