webcomment/webui/src/components/admin_login_form.rs
2023-07-13 11:36:31 +02:00

70 lines
1.6 KiB
Rust

use crate::api::Api;
use gloo::console;
use web_sys::HtmlFormElement;
use yew::{html, Component, Context, Html, Properties, SubmitEvent, html::TargetCast};
pub struct AdminLoginFormComponent {}
#[derive(Properties, PartialEq)]
pub struct AdminLoginFormProps {
pub root_id: String, // TODO maybe opti
pub api: Api,
}
pub enum Msg {
Login(HtmlFormElement)
}
impl Component for AdminLoginFormComponent {
type Message = Msg;
type Properties = AdminLoginFormProps;
fn create(_ctx: &Context<Self>) -> Self {
Self {}
}
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::Login(form) => {
console::log!("{:?}", &form);
let formdata = web_sys::FormData::new_with_form(&form).unwrap();
console::log!("{:?}", &formdata);
let password = formdata.get("password").as_string().unwrap();
let mut api = ctx.props().api.inner.write();
api.admin_psw = if password.is_empty() {
None
} else {
Some(password)
};
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
let props = ctx.props();
let elem_id = format!("{}-admin_login_form", props.root_id);
html! {
<form
id={ elem_id }
method="post"
action="#"
onsubmit={ctx.link().callback(|event: SubmitEvent| {
event.prevent_default();
Msg::Login(event.target_unchecked_into())
})}
>
<fieldset>
<legend>{ "Admin Login" }</legend>
<label>
{ "Password:" }
<input type="password" name="password"/>
</label><br/>
<input type="submit" value="Login"/>
</fieldset>
</form>
}
}
}