use crate::infra::{ api::HostService, common_component::{CommonComponent, CommonComponentParts}, cookies::delete_cookie, }; use anyhow::Result; use yew::prelude::*; pub struct LogoutButton { common: CommonComponentParts, } #[derive(Clone, PartialEq, Properties)] pub struct Props { pub on_logged_out: Callback<()>, } pub enum Msg { LogoutRequested, LogoutCompleted(Result<()>), } impl CommonComponent for LogoutButton { fn handle_msg(&mut self, msg: ::Message) -> Result { match msg { Msg::LogoutRequested => { self.common .call_backend(HostService::logout, (), Msg::LogoutCompleted)?; } Msg::LogoutCompleted(res) => { res?; delete_cookie("user_id")?; self.common.on_logged_out.emit(()); } } Ok(false) } fn mut_common(&mut self) -> &mut CommonComponentParts { &mut self.common } } impl Component for LogoutButton { type Message = Msg; type Properties = Props; fn create(props: Self::Properties, link: ComponentLink) -> Self { LogoutButton { common: CommonComponentParts::::create(props, link), } } fn update(&mut self, msg: Self::Message) -> ShouldRender { CommonComponentParts::::update(self, msg) } fn change(&mut self, _: Self::Properties) -> ShouldRender { false } fn view(&self) -> Html { html! { } } }