mentorenwahl/frontend/src/routes/home/mod.rs

117 lines
4.0 KiB
Rust

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<Vec<String>>,
me: graphql::queries::me::me::MeMe,
},
}
#[derive(Properties, PartialEq)]
pub struct HomeProps {
pub token: Option<String>,
}
pub struct Home {
fetching: bool,
errors: Option<Vec<String>>,
me: Option<graphql::queries::me::me::MeMe>,
}
impl Component for Home {
type Message = Msg;
type Properties = HomeProps;
fn create(ctx: &Context<Self>) -> Self {
let client = graphql::client(ctx.props().token.as_ref()).unwrap();
ctx.link().send_future(async move {
let response = post_graphql::<graphql::queries::me::Me, _>(
&client,
graphql::URL.as_str(),
graphql::queries::me::me::Variables,
)
.await
.unwrap();
Msg::DoneFetching {
errors: graphql::convert(response.errors),
me: response.data.unwrap().me.unwrap(),
}
});
Self {
fetching: true,
errors: None,
me: None,
}
}
fn update(&mut self, _ctx: &Context<Self>, 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<Self>) -> Html {
html! {
<>
<Title value="Home" />
if self.fetching {
<components::fetching::Fetching />
} else {
if let Some(errors) = &self.errors {
<components::graphql_errors::GraphQLErrors errors={errors.clone()} />
} else {
{{
let me = self.me.as_ref().unwrap();
html! {
<>
<section class={classes!("hero", "is-primary")}>
<div class={classes!("hero-body")}>
<p class={classes!("title")}>{ format!("Hey, {}!", me.first_name) }</p>
</div>
</section>
<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! {}
}
}
</>
}
}}
}
}
</>
}
}
}