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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
pub use embedded_svc::utils::role::Role as RoleValue;
use yew::prelude::*;
use yew_router::prelude::*;
use crate::auth::*;
use crate::frame::*;
use crate::loading::*;
use crate::redust::{use_projection, Projection, Reducible2, ValueAction, ValueState};
#[derive(Clone, Default, Debug, PartialEq, Eq)]
pub struct Credentials {
pub username: String,
pub password: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RoleStateValue {
Unknown,
Authenticating(Credentials),
AuthenticationFailed(Credentials),
Role(embedded_svc::utils::role::Role),
LoggingOut(embedded_svc::utils::role::Role),
LoggedOut,
}
impl Default for RoleStateValue {
fn default() -> Self {
Self::Unknown
}
}
pub type RoleAction = ValueAction<RoleStateValue>;
pub type RoleState = ValueState<RoleStateValue>;
#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleProps<R: Reducible2> {
pub role: RoleValue,
pub projection: Projection<R, RoleState, RoleAction>,
#[prop_or_default]
pub auth: bool,
#[prop_or_default]
pub children: Children,
}
#[function_component(Role)]
pub fn role<R: Reducible2>(props: &RoleProps<R>) -> Html {
let role = use_projection(props.projection.clone());
match (&**role, &Default::default()) {
(RoleStateValue::Role(role), _) if *role >= props.role => {
html! {
{ for props.children.iter() }
}
}
(RoleStateValue::Unknown, _) if props.auth => {
html! {
<Loading/>
}
}
(RoleStateValue::AuthenticationFailed(credentials), _)
| (RoleStateValue::Authenticating(credentials), _)
| (RoleStateValue::Role(RoleValue::None), credentials)
if props.auth =>
{
let submit = {
let role = role.clone();
Callback::from(move |(username, password)| {
role.dispatch(RoleAction::Update(RoleStateValue::Authenticating(
Credentials { username, password },
)));
})
};
html! {
<Auth
username={credentials.username.clone()}
password={credentials.password.clone()}
authenticating={matches!(&**role, RoleStateValue::Authenticating(_))}
auth_failed={matches!(&**role, RoleStateValue::AuthenticationFailed(_))}
{submit}
/>
}
}
_ if props.auth => {
html! {
<NoPerm/>
}
}
_ => {
html! {}
}
}
}
#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleLogoutStatusItemProps<R: Routable + PartialEq + Clone + 'static, S: Reducible2> {
pub auth_status_route: R,
pub projection: Projection<S, RoleState, RoleAction>,
}
#[function_component(RoleLogoutStatusItem)]
pub fn role_logout_status_item<R: Routable + PartialEq + Clone + 'static, S: Reducible2>(
props: &RoleLogoutStatusItemProps<R, S>,
) -> Html {
let role = use_projection(props.projection.clone());
let history = use_history();
match &**role {
RoleStateValue::Role(role_value)
if *role_value >= embedded_svc::utils::role::Role::None =>
{
let selected = {
let auth_status_route = props.auth_status_route.clone();
let role_value = *role_value;
let role = role.clone();
Callback::from(move |_| {
role.dispatch(RoleAction::Update(RoleStateValue::LoggingOut(role_value)));
if let Some(history) = history.as_ref() {
history.push(auth_status_route.clone());
}
})
};
html! {
<StatusItem
icon="fa-lg fa-solid fa-right-from-bracket"
{selected}/>
}
}
_ => {
html! {}
}
}
}
#[derive(Properties, Clone, Debug, PartialEq)]
pub struct RoleAuthStateProps<R: Routable + PartialEq + Clone + 'static, S: Reducible2> {
#[prop_or_default]
pub home: Option<R>,
pub projection: Projection<S, RoleState, RoleAction>,
}
#[function_component(RoleAuthState)]
pub fn role_auth_state<R: Routable + PartialEq + Clone + 'static, S: Reducible2>(
props: &RoleAuthStateProps<R, S>,
) -> Html {
let role = use_projection(props.projection.clone());
let role = match &**role {
RoleStateValue::Role(role_value) => Some(*role_value),
_ => None,
};
let login = if let Some(home) = props.home.clone() {
let history = use_history();
Some(Callback::from(move |_| {
if let Some(history) = history.as_ref() {
history.push(home.clone());
}
}))
} else {
None
};
html! {
<AuthState {login} {role}/>
}
}