2021-10-31 13:41:28 +00:00
|
|
|
use crate::infra::{
|
|
|
|
api::HostService,
|
|
|
|
common_component::{CommonComponent, CommonComponentParts},
|
|
|
|
cookies::delete_cookie,
|
|
|
|
};
|
2021-05-23 15:07:02 +00:00
|
|
|
use anyhow::Result;
|
2021-05-18 17:04:06 +00:00
|
|
|
use yew::prelude::*;
|
|
|
|
|
|
|
|
pub struct LogoutButton {
|
2021-10-31 13:41:28 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, PartialEq, Properties)]
|
|
|
|
pub struct Props {
|
|
|
|
pub on_logged_out: Callback<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
2021-05-23 15:07:02 +00:00
|
|
|
LogoutRequested,
|
|
|
|
LogoutCompleted(Result<()>),
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
2021-10-31 13:41:28 +00:00
|
|
|
impl CommonComponent<LogoutButton> for LogoutButton {
|
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
|
|
|
match msg {
|
|
|
|
Msg::LogoutRequested => {
|
|
|
|
self.common
|
|
|
|
.call_backend(HostService::logout, (), Msg::LogoutCompleted)?;
|
|
|
|
}
|
|
|
|
Msg::LogoutCompleted(res) => {
|
|
|
|
res?;
|
|
|
|
delete_cookie("user_id")?;
|
|
|
|
self.common.props.on_logged_out.emit(());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-18 17:04:06 +00:00
|
|
|
impl Component for LogoutButton {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = Props;
|
|
|
|
|
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
|
|
|
LogoutButton {
|
2021-10-31 13:41:28 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-31 13:41:28 +00:00
|
|
|
CommonComponentParts::<Self>::update(self, msg)
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
|
|
|
html! {
|
2021-09-19 17:44:53 +00:00
|
|
|
<button
|
2021-10-15 08:20:37 +00:00
|
|
|
class="dropdown-item"
|
2021-10-31 13:41:28 +00:00
|
|
|
onclick=self.common.callback(|_| Msg::LogoutRequested)>
|
2021-09-19 17:44:53 +00:00
|
|
|
{"Logout"}
|
|
|
|
</button>
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|