mentorenwahl/frontend/src/layouts/main.rs

117 lines
3.7 KiB
Rust
Raw Normal View History

2022-11-21 18:48:53 +00:00
use graphql_client::reqwest::post_graphql;
2022-11-04 20:23:36 +00:00
use yew::prelude::*;
2022-11-05 20:27:49 +00:00
use yew_agent::{Dispatched, Dispatcher};
2022-11-04 20:23:36 +00:00
use yew_router::prelude::*;
2022-11-05 20:27:49 +00:00
use crate::agents;
use crate::components;
2022-11-21 18:48:53 +00:00
use crate::graphql;
2022-11-04 20:23:36 +00:00
use crate::routes;
2022-11-05 20:27:49 +00:00
pub enum Msg {
2022-11-21 18:48:53 +00:00
LogOutClicked,
LogOut(Option<Vec<String>>),
2022-11-05 20:27:49 +00:00
}
2022-11-04 20:23:36 +00:00
#[derive(Properties, PartialEq)]
pub struct MainProps {
2022-11-21 18:48:53 +00:00
pub token: Option<String>,
2022-11-05 20:27:49 +00:00
pub logged_in: bool,
2022-11-04 20:23:36 +00:00
#[prop_or_default]
pub children: Children,
}
2022-11-05 20:27:49 +00:00
pub struct Main {
logged_in: bool,
logged_in_event_bus: Dispatcher<agents::logged_in::EventBus>,
2022-11-21 18:48:53 +00:00
errors: Option<Vec<String>>,
2022-11-05 20:27:49 +00:00
}
impl Component for Main {
type Message = Msg;
type Properties = MainProps;
fn create(ctx: &Context<Self>) -> Self {
Self {
logged_in: ctx.props().logged_in,
logged_in_event_bus: agents::logged_in::EventBus::dispatcher(),
2022-11-21 18:48:53 +00:00
errors: None,
2022-11-05 20:27:49 +00:00
}
}
2022-11-21 18:48:53 +00:00
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
2022-11-05 20:27:49 +00:00
match msg {
2022-11-21 18:48:53 +00:00
Msg::LogOutClicked => {
let client = graphql::client(ctx.props().token.as_ref()).unwrap();
ctx.link().send_future(async move {
let response = post_graphql::<graphql::mutations::logout::Logout, _>(
&client,
graphql::URL.as_str(),
graphql::mutations::logout::logout::Variables,
)
.await
.unwrap();
Msg::LogOut(components::graphql_errors::convert(response.errors))
});
2022-11-05 20:27:49 +00:00
false
}
2022-11-21 18:48:53 +00:00
Msg::LogOut(errors) => {
if self.errors.is_none() {
self.logged_in_event_bus
.send(agents::logged_in::Request::LoggedIn(false));
false
} else {
self.errors = errors;
true
}
}
2022-11-05 20:27:49 +00:00
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
2022-11-21 18:48:53 +00:00
<>
<components::logged_in_handler::LoggedInHandler logged_in={self.logged_in} />
2022-11-25 22:21:21 +00:00
<div id="wrapper">
<nav>
<ul>
<li>
<Link<routes::Route> to={routes::Route::Home}>
<button>{ "Home" }</button>
</Link<routes::Route>>
</li>
<li>
<Link<routes::Route> to={routes::Route::Settings}>
<button>{ "Settings" }</button>
2022-11-21 18:48:53 +00:00
</Link<routes::Route>>
2022-11-25 22:21:21 +00:00
</li>
<li>
if self.logged_in {
<button onclick={ctx.link().callback(move |_| Msg::LogOutClicked)}>{ "Logout" }</button>
} else {
<Link<routes::Route> to={routes::Route::Login}>
<button>{ "Login" }</button>
</Link<routes::Route>>
}
</li>
</ul>
<components::graphql_errors::GraphQLErrors errors={self.errors.clone()} />
</nav>
<hr />
<main>
{ for ctx.props().children.iter() }
</main>
</div>
2022-11-05 20:27:49 +00:00
2022-11-25 22:21:21 +00:00
<components::footer::Footer />
2022-11-21 18:48:53 +00:00
</>
2022-11-05 20:27:49 +00:00
}
2022-11-04 20:23:36 +00:00
}
}