lldap/app/src/components/logout.rs

76 lines
2.0 KiB
Rust
Raw Normal View History

use crate::infra::{api::HostService, cookies::delete_cookie};
2021-05-23 15:07:02 +00:00
use anyhow::Result;
use yew::prelude::*;
2021-05-23 15:07:02 +00:00
use yew::services::{fetch::FetchTask, ConsoleService};
pub struct LogoutButton {
link: ComponentLink<Self>,
on_logged_out: Callback<()>,
2021-05-23 15:07:02 +00:00
// Used to keep the request alive long enough.
_task: Option<FetchTask>,
}
#[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<()>),
}
impl Component for LogoutButton {
type Message = Msg;
type Properties = Props;
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
LogoutButton {
2021-05-30 15:07:34 +00:00
link,
on_logged_out: props.on_logged_out,
2021-05-23 15:07:02 +00:00
_task: None,
}
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
2021-05-23 15:07:02 +00:00
Msg::LogoutRequested => {
match HostService::logout(self.link.callback(Msg::LogoutCompleted)) {
Ok(task) => self._task = Some(task),
Err(e) => ConsoleService::error(&e.to_string()),
};
false
}
Msg::LogoutCompleted(res) => {
if let Err(e) = res {
ConsoleService::error(&e.to_string());
}
2021-05-23 15:07:02 +00:00
match delete_cookie("user_id") {
Err(e) => {
ConsoleService::error(&e.to_string());
false
}
Ok(()) => {
self.on_logged_out.emit(());
true
}
}
2021-05-23 15:07:02 +00:00
}
}
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
false
}
fn view(&self) -> Html {
html! {
2021-09-19 17:44:53 +00:00
<button
class="dropdown-item"
2021-09-19 17:44:53 +00:00
onclick=self.link.callback(|_| Msg::LogoutRequested)>
{"Logout"}
</button>
}
}
}