use graphql_client::reqwest::post_graphql; use yew::prelude::*; use yew_side_effect::title::Title; use crate::components; use crate::graphql; pub mod student_home; pub mod student_vote; pub mod teacher_home; pub mod teacher_registration; pub enum Msg { DoneFetching { errors: Option>, me: graphql::queries::me::me::MeMe, }, } #[derive(Properties, PartialEq)] pub struct HomeProps { pub token: Option, } pub struct Home { fetching: bool, errors: Option>, me: Option, } impl Component for Home { type Message = Msg; type Properties = HomeProps; fn create(ctx: &Context) -> Self { let client = graphql::client(ctx.props().token.as_ref()).unwrap(); ctx.link().send_future(async move { let response = post_graphql::( &client, graphql::URL.as_str(), graphql::queries::me::me::Variables, ) .await .unwrap(); Msg::DoneFetching { errors: components::graphql_errors::convert(response.errors), me: response.data.unwrap().me.unwrap(), } }); Self { fetching: true, errors: None, me: None, } } fn update(&mut self, _ctx: &Context, msg: Self::Message) -> bool { match msg { Msg::DoneFetching { errors, me } => { self.fetching = false; self.errors = errors; self.me = Some(me); true } } } fn view(&self, ctx: &Context) -> Html { html! { <> if self.fetching { <p>{ "Fetching..." }</p> } else { if let Some(errors) = &self.errors { <components::graphql_errors::GraphQLErrors errors={errors.clone()} /> } else { {{ let me = self.me.as_ref().unwrap(); html! { <> <h1>{ format!("Hey, {}!", me.first_name) }</h1> <hr /> { match me.role { graphql::queries::me::me::UserRole::Student => html! { <student_home::StudentHome token={ctx.props().token.as_ref().unwrap().to_owned()} voted={me.student.as_ref().unwrap().vote.is_some()} /> }, graphql::queries::me::me::UserRole::Teacher => html! { <teacher_home::TeacherHome token={ctx.props().token.as_ref().unwrap().to_owned()} registered={me.teacher.is_some()} /> }, graphql::queries::me::me::UserRole::Other(_) => html! {} } } </> } }} } } </> } } }