mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
app: Migrate Logout to CommonComponent
This commit is contained in:
parent
d31ca426f7
commit
640126f39a
@ -1,13 +1,13 @@
|
|||||||
use crate::infra::{api::HostService, cookies::delete_cookie};
|
use crate::infra::{
|
||||||
|
api::HostService,
|
||||||
|
common_component::{CommonComponent, CommonComponentParts},
|
||||||
|
cookies::delete_cookie,
|
||||||
|
};
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
use yew::services::{fetch::FetchTask, ConsoleService};
|
|
||||||
|
|
||||||
pub struct LogoutButton {
|
pub struct LogoutButton {
|
||||||
link: ComponentLink<Self>,
|
common: CommonComponentParts<Self>,
|
||||||
on_logged_out: Callback<()>,
|
|
||||||
// Used to keep the request alive long enough.
|
|
||||||
_task: Option<FetchTask>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Properties)]
|
#[derive(Clone, PartialEq, Properties)]
|
||||||
@ -20,43 +20,39 @@ pub enum Msg {
|
|||||||
LogoutCompleted(Result<()>),
|
LogoutCompleted(Result<()>),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Component for LogoutButton {
|
impl Component for LogoutButton {
|
||||||
type Message = Msg;
|
type Message = Msg;
|
||||||
type Properties = Props;
|
type Properties = Props;
|
||||||
|
|
||||||
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
LogoutButton {
|
LogoutButton {
|
||||||
link,
|
common: CommonComponentParts::<Self>::create(props, link),
|
||||||
on_logged_out: props.on_logged_out,
|
|
||||||
_task: None,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
match msg {
|
CommonComponentParts::<Self>::update(self, msg)
|
||||||
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());
|
|
||||||
}
|
|
||||||
match delete_cookie("user_id") {
|
|
||||||
Err(e) => {
|
|
||||||
ConsoleService::error(&e.to_string());
|
|
||||||
false
|
|
||||||
}
|
|
||||||
Ok(()) => {
|
|
||||||
self.on_logged_out.emit(());
|
|
||||||
true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
||||||
@ -67,7 +63,7 @@ impl Component for LogoutButton {
|
|||||||
html! {
|
html! {
|
||||||
<button
|
<button
|
||||||
class="dropdown-item"
|
class="dropdown-item"
|
||||||
onclick=self.link.callback(|_| Msg::LogoutRequested)>
|
onclick=self.common.callback(|_| Msg::LogoutRequested)>
|
||||||
{"Logout"}
|
{"Logout"}
|
||||||
</button>
|
</button>
|
||||||
}
|
}
|
||||||
|
@ -223,7 +223,8 @@ impl HostService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn logout(callback: Callback<Result<()>>) -> Result<FetchTask> {
|
// The `_request` parameter is to make it the same shape as the other functions.
|
||||||
|
pub fn logout(_request: (), callback: Callback<Result<()>>) -> Result<FetchTask> {
|
||||||
call_server_empty_response_with_error_message(
|
call_server_empty_response_with_error_message(
|
||||||
"/auth/logout",
|
"/auth/logout",
|
||||||
yew::format::Nothing,
|
yew::format::Nothing,
|
||||||
|
Loading…
Reference in New Issue
Block a user