use graphql_client::reqwest::post_graphql; use web_sys::HtmlInputElement; use yew::prelude::*; use yew_agent::{Dispatched, Dispatcher}; use crate::agents; use crate::components; use crate::graphql; pub enum Msg { Submit, Registration(Option>), } #[derive(Properties, PartialEq)] pub struct TeacherRegistrationProps { pub token: String, } pub struct TeacherRegistration { max_students: NodeRef, errors: Option>, teacher_registered_event_bus: Dispatcher, } impl Component for TeacherRegistration { type Message = Msg; type Properties = TeacherRegistrationProps; fn create(_ctx: &Context) -> Self { Self { max_students: NodeRef::default(), errors: None, teacher_registered_event_bus: agents::teacher_registered::EventBus::dispatcher(), } } 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(); log::debug!("{:?}", response.data.unwrap().register_teacher); Msg::Registration(components::graphql_errors::convert(response.errors)) }); } false } Msg::Registration(errors) => { self.errors = errors; if self.errors.is_none() { self.teacher_registered_event_bus .send(agents::teacher_registered::Request::Registered); false } else { true } } } } fn view(&self, ctx: &Context) -> Html { let onsubmit = ctx.link().callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit }); html! { <>


} } }