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
use crate::class::Class;
use dioxus::prelude::*;
use std::time::Duration;
use zino_core::SharedString;
/// Global messages as feedback in response to user operations.
pub fn OperationResult<T, E>(props: OperationResultProps<T, E>) -> Element
where
T: Clone + PartialEq + 'static,
E: Clone + PartialEq + 'static,
{
let mut visible = props.visible;
if !visible() {
return rsx! {};
}
let duration = Duration::from_millis(props.duration);
match (props.future)() {
Some(Ok(data)) => {
spawn(async move {
tokio::time::sleep(duration).await;
visible.set(false);
if let Some(handler) = props.on_success.as_ref() {
handler.call(data);
}
});
rsx! {
div {
class: "{props.class} is-success",
position: "fixed",
top: "4rem",
right: "0.75rem",
z_index: 9999,
if !props.title.is_empty() {
div {
class: "message-header",
span { { props.title.into_owned() } }
button {
r#type: "button",
class: props.close_class,
onclick: move |_event| {
visible.set(false);
if let Some(handler) = props.on_close.as_ref() {
handler.call(false);
}
}
}
}
}
div {
class: "message-body",
{ props.success.into_owned() }
}
}
}
}
Some(Err(err)) => {
spawn(async move {
tokio::time::sleep(duration).await;
visible.set(false);
if let Some(handler) = props.on_error.as_ref() {
handler.call(err);
}
});
rsx! {
div {
class: "{props.class} is-danger",
position: "fixed",
top: "4rem",
right: "0.75rem",
z_index: 9999,
if !props.title.is_empty() {
div {
class: "message-header",
span { { props.title.into_owned() } }
button {
r#type: "button",
class: props.close_class,
onclick: move |_event| {
visible.set(false);
if let Some(handler) = props.on_close.as_ref() {
handler.call(false);
}
}
}
}
}
div {
class: "message-body",
span { { props.error.into_owned() } }
}
}
}
}
None => {
if let Some(handler) = props.on_loading.as_ref() {
handler.call(());
}
if props.loading.is_empty() {
rsx! {}
} else {
rsx! {
div {
class: "{props.class} is-warning",
position: "fixed",
top: "4rem",
right: "0.75rem",
z_index: 99,
if !props.title.is_empty() {
div {
class: "message-header",
span { { props.title.into_owned() } }
button {
r#type: "button",
class: props.close_class,
onclick: move |_event| {
visible.set(false);
if let Some(handler) = props.on_close.as_ref() {
handler.call(false);
}
}
}
}
}
div {
class: "message-body",
span { { props.loading.into_owned() } }
}
}
}
}
}
}
}
/// The [`OperationResult`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct OperationResultProps<T: Clone + PartialEq + 'static, E: Clone + PartialEq + 'static> {
/// The class attribute for the component.
#[props(into, default = "message")]
pub class: Class,
/// A class to apply to the `close` button element.
#[props(into, default = "delete")]
pub close_class: Class,
/// A flag to determine whether the message is visible or not.
pub visible: Signal<bool>,
/// A future value which represents the result of user operations.
pub future: Resource<Result<T, E>>,
/// A duration in milliseconds.
#[props(default = 1500)]
pub duration: u64,
/// The title in the message header.
#[props(into, default)]
pub title: SharedString,
/// A message to render when the future value is not ready.
#[props(into, default)]
pub loading: SharedString,
/// A message to render when the future value is resolved.
#[props(into, default)]
pub success: SharedString,
/// A message to render when the future value is rejected.
#[props(into, default)]
pub error: SharedString,
/// An event handler to be called when the future value is not ready.
pub on_loading: Option<EventHandler>,
/// An event handler to be called when the future value is resolved.
pub on_success: Option<EventHandler<T>>,
/// An event handler to be called when the future value is rejected.
pub on_error: Option<EventHandler<E>>,
/// An event handler to be called when the `close` button is clicked.
pub on_close: Option<EventHandler<bool>>,
}