use graphql_client::reqwest::post_graphql; use web_sys::HtmlInputElement; use yew::prelude::*; use crate::components; use crate::graphql; pub enum Msg { Submit, Registration(Option>), } #[derive(Properties, PartialEq)] pub struct TeacherRegistrationProps { pub token: String, pub onregistered: Callback<()>, } pub struct TeacherRegistration { max_students: NodeRef, errors: Option>, } impl Component for TeacherRegistration { type Message = Msg; type Properties = TeacherRegistrationProps; fn create(_ctx: &Context) -> Self { Self { max_students: NodeRef::default(), errors: None, } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Msg::Submit => { if let Some(max_students) = self .max_students .cast::() .map(|x| x.value_as_number() as usize) { let client = graphql::client(Some(&ctx.props().token)).unwrap(); ctx.link().send_future(async move { let response = post_graphql::< graphql::mutations::register_teacher::RegisterTeacher, _, >( &client, graphql::URL.as_str(), graphql::mutations::register_teacher::register_teacher::Variables { max_students: max_students as i64, }, ) .await .unwrap(); Msg::Registration(components::graphql_errors::convert(response.errors)) }); } false } Msg::Registration(errors) => { self.errors = errors; if self.errors.is_none() { ctx.props().onregistered.emit(()); false } else { true } } } } fn view(&self, ctx: &Context) -> Html { let onsubmit = ctx.link().callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit }); html! { <>
} } }