125 lines
4.4 KiB
Rust
125 lines
4.4 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>,
|
|
}
|
|
|
|
enum State {
|
|
Fetching,
|
|
Done,
|
|
}
|
|
|
|
pub struct Home {
|
|
state: State,
|
|
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 {
|
|
Self {
|
|
state: State::Fetching,
|
|
errors: None,
|
|
me: None,
|
|
}
|
|
}
|
|
|
|
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
|
match msg {
|
|
Msg::DoneFetching { errors, me } => {
|
|
self.state = State::Done;
|
|
self.errors = errors;
|
|
self.me = Some(me);
|
|
true
|
|
}
|
|
}
|
|
}
|
|
|
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
|
html! {
|
|
<>
|
|
<Title value="Home" />
|
|
{
|
|
match self.state {
|
|
State::Fetching => {
|
|
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(),
|
|
}
|
|
});
|
|
|
|
html! {
|
|
<p>{ "Fetching..." }</p>
|
|
}
|
|
}
|
|
State::Done => {
|
|
if let Some(errors) = &self.errors {
|
|
html! {
|
|
<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! {}
|
|
}
|
|
}
|
|
</>
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
</>
|
|
}
|
|
}
|
|
}
|