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

113 lines
3.7 KiB
Rust
Raw Normal View History

2022-11-12 21:50:06 +00:00
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 {
2022-11-21 18:48:53 +00:00
fetching: bool,
2022-11-12 21:50:06 +00:00
errors: Option<Vec<String>>,
me: Option<graphql::queries::me::me::MeMe>,
}
impl Component for Home {
type Message = Msg;
type Properties = HomeProps;
2022-11-21 18:48:53 +00:00
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: components::graphql_errors::convert(response.errors),
me: response.data.unwrap().me.unwrap(),
}
});
2022-11-12 21:50:06 +00:00
Self {
2022-11-21 18:48:53 +00:00
fetching: true,
2022-11-12 21:50:06 +00:00
errors: None,
me: None,
}
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::DoneFetching { errors, me } => {
2022-11-21 18:48:53 +00:00
self.fetching = false;
2022-11-12 21:50:06 +00:00
self.errors = errors;
self.me = Some(me);
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<>
<Title value="Home" />
2022-11-21 18:48:53 +00:00
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();
2022-11-12 21:50:06 +00:00
html! {
2022-11-21 18:48:53 +00:00
<>
<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! {}
2022-11-12 21:50:06 +00:00
}
2022-11-21 18:48:53 +00:00
}
</>
2022-11-12 21:50:06 +00:00
}
2022-11-21 18:48:53 +00:00
}}
2022-11-12 21:50:06 +00:00
}
}
</>
}
}
}