use graphql_client::reqwest::post_graphql; use yew::prelude::*; use yew_agent::{Dispatched, Dispatcher}; use yew_router::prelude::*; use crate::agents; use crate::components; use crate::graphql; use crate::routes; pub enum Msg { LogoutClicked, Logout(graphql::Errors), BurgerClicked, } #[derive(Properties, PartialEq)] pub struct NavbarProps { pub token: Option, pub logged_in: bool, #[prop_or(true)] pub default_theme: bool, } pub struct Navbar { logged_in: bool, logged_in_event_bus: Dispatcher, errors: Option>, burger_active: bool, } impl Component for Navbar { type Message = Msg; type Properties = NavbarProps; fn create(ctx: &Context) -> Self { Self { logged_in: ctx.props().logged_in, logged_in_event_bus: agents::logged_in::EventBus::dispatcher(), errors: None, burger_active: false, } } fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { Msg::LogoutClicked => { 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::mutations::logout::logout::Variables, ) .await .unwrap(); Msg::Logout(graphql::convert(response.errors)) }); false } Msg::Logout(errors) => { if errors.is_none() { self.logged_in_event_bus .send(agents::logged_in::Request::LoggedIn(false)); false } else { self.errors = errors; true } } Msg::BurgerClicked => { self.burger_active = !self.burger_active; true } } } fn view(&self, ctx: &Context) -> Html { html! {
} } }