1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::{
api::Response,
route::RouterTarget,
service::{
cookie::CookieService,
session_timer::{self, SessionTimerAgent},
uikit::{NotificationStatus, UIkitService},
},
string::{REQUEST_ERROR, RESPONSE_ERROR, TEXT_CONTENT, TEXT_LOGOUT},
SESSION_COOKIE,
};
use log::{error, info, warn};
use webapp::{
protocol::{model::Session, request, response},
API_URL_LOGOUT,
};
use yew::{agent::Bridged, format::Json, html, prelude::*, services::fetch::FetchTask};
use yew_router::agent::{RouteAgent, RouteRequest::ChangeRoute};
pub struct ContentComponent {
component_link: ComponentLink<ContentComponent>,
cookie_service: CookieService,
fetch_task: Option<FetchTask>,
logout_button_disabled: bool,
router_agent: Box<dyn Bridge<RouteAgent<()>>>,
session_timer_agent: Box<dyn Bridge<SessionTimerAgent>>,
uikit_service: UIkitService,
}
pub enum Message {
Fetch(Response<response::Logout>),
Ignore,
LogoutRequest,
}
impl Component for ContentComponent {
type Message = Message;
type Properties = ();
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
let mut router_agent = RouteAgent::bridge(link.callback(|_| Message::Ignore));
let cookie_service = CookieService::new();
let mut session_timer_agent = SessionTimerAgent::bridge(link.callback(|_| Message::Ignore));
if cookie_service.get(SESSION_COOKIE).is_err() {
info!("No session token found, routing back to login");
router_agent.send(ChangeRoute(RouterTarget::Login.into()));
} else {
session_timer_agent.send(session_timer::Request::Start);
}
Self {
component_link: link,
cookie_service,
fetch_task: None,
logout_button_disabled: false,
router_agent,
session_timer_agent,
uikit_service: UIkitService::new(),
}
}
fn change(&mut self, _: Self::Properties) -> ShouldRender {
true
}
fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg {
Message::LogoutRequest => {
if let Ok(token) = self.cookie_service.get(SESSION_COOKIE) {
self.fetch_task = fetch! {
request::Logout(Session::new(token)) => API_URL_LOGOUT,
self.component_link, Message::Fetch,
|| {
self.logout_button_disabled = true;
},
|| {
error!("Unable to create logout request");
self.uikit_service
.notify(REQUEST_ERROR, &NotificationStatus::Danger);
}
};
} else {
error!("No session cookie found");
self.router_agent
.send(ChangeRoute(RouterTarget::Login.into()));
}
}
Message::Fetch(response) => {
let (meta, Json(body)) = response.into_parts();
if meta.status.is_success() {
match body {
Ok(response::Logout) => info!("Got valid logout response"),
_ => {
warn!("Got wrong logout response");
self.uikit_service
.notify(RESPONSE_ERROR, &NotificationStatus::Danger);
}
}
} else {
warn!("Logout failed with status: {}", meta.status);
}
self.cookie_service.remove(SESSION_COOKIE);
self.session_timer_agent.send(session_timer::Request::Stop);
self.router_agent
.send(ChangeRoute(RouterTarget::Login.into()));
self.logout_button_disabled = true;
self.fetch_task = None;
}
Message::Ignore => {}
}
true
}
fn view(&self) -> Html {
let onclick = self.component_link.callback(|_| Message::LogoutRequest);
html! {
<div class="uk-card uk-card-default uk-card-body uk-width-1-3@s uk-position-center",>
<h1 class="uk-card-title",>{TEXT_CONTENT}</h1>
<button disabled=self.logout_button_disabled,
class="uk-button uk-button-default",
onclick=onclick>{TEXT_LOGOUT}</button>
</div>
}
}
}