94 lines
2.4 KiB
Rust
94 lines
2.4 KiB
Rust
use crate::{types::*, api::Api};
|
|
|
|
use gloo::console;
|
|
use wasm_bindgen::JsValue;
|
|
use web_sys::HtmlFormElement;
|
|
use yew::{html, Callback, Component, Context, Html, Properties, SubmitEvent, html::TargetCast};
|
|
|
|
pub struct NewCommentFormComponent {}
|
|
|
|
#[derive(Properties, PartialEq)]
|
|
pub struct NewCommentFormProps {
|
|
pub root_id: String, // TODO maybe opti
|
|
pub api: Api,
|
|
pub topic: String,
|
|
pub comment: NotSentComment,
|
|
}
|
|
|
|
pub enum Msg {
|
|
Submit(HtmlFormElement)
|
|
}
|
|
|
|
impl Component for NewCommentFormComponent {
|
|
type Message = Msg;
|
|
type Properties = NewCommentFormProps;
|
|
|
|
fn create(_ctx: &Context<Self>) -> Self {
|
|
Self {}
|
|
}
|
|
|
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::Submit(form) => {
|
|
console::log!("{:?}", &form);
|
|
let formdata = web_sys::FormData::new_with_form(&form).unwrap();
|
|
console::log!("{:?}", &formdata);
|
|
let email = formdata.get("email").as_string().unwrap();
|
|
let api = ctx.props().api.clone();
|
|
let topic = ctx.props().topic.clone();
|
|
yew::platform::spawn_local(async move {
|
|
let email_trimmed = email.trim();
|
|
api.new_comment(
|
|
StoredComment {
|
|
author: formdata.get("author").as_string().unwrap(),
|
|
email: if email_trimmed.is_empty() {None} else {Some(email_trimmed.to_string())},
|
|
last_edit_time: None,
|
|
post_time: 0,// TODO
|
|
text: formdata.get("text").as_string().unwrap()
|
|
},
|
|
topic,
|
|
).await;
|
|
});
|
|
// TODO move req to dedicated async part
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
let props = ctx.props();
|
|
let elem_id = format!("{}-new_comment_form", props.root_id);
|
|
html! {
|
|
<form
|
|
id={ elem_id }
|
|
method="post"
|
|
action="#"
|
|
onsubmit={ctx.link().callback(|event: SubmitEvent| {
|
|
event.prevent_default();
|
|
Msg::Submit(event.target_unchecked_into())
|
|
})}
|
|
>
|
|
<fieldset>
|
|
<legend>{ "New comment" }</legend>
|
|
<label>
|
|
{ "Your name:" }
|
|
<input type="text" name="author" class="comment-form-author"/>
|
|
</label><br/>
|
|
<label>
|
|
{ "Your email:" }
|
|
<input type="email" name="email" class="comment-form-email"/>
|
|
</label><br/>
|
|
<textarea class="comment-form-text" name="text"></textarea><br/>
|
|
<input type="submit" value="Post"/>
|
|
</fieldset>
|
|
</form>
|
|
}
|
|
}
|
|
}
|
|
|
|
/*impl NewCommentFormComponent {
|
|
fn submit(&mut self, element: HtmlElement) {
|
|
|
|
}
|
|
}*/
|