forked from seed-rs/seed
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_a.rs
86 lines (73 loc) · 2.16 KB
/
example_a.rs
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
use seed::prelude::*;
use seed::fetch;
use futures::Future;
use shared;
fn get_request_url() -> String {
"/api/send-message".into()
}
// Model
#[derive(Default)]
pub struct Model {
pub new_message: String,
pub response: Option<fetch::ResponseResult<shared::ResponseExampleA>>,
}
// Update
#[derive(Clone)]
pub enum Msg {
NewMessageChanged(String),
SendRequest,
Fetched(fetch::FetchObject<shared::ResponseExampleA>),
}
pub fn update(msg: Msg, model: &mut Model, orders: &mut Orders<Msg>) {
match msg {
Msg::NewMessageChanged(message) => {
model.new_message = message;
}
Msg::SendRequest => {
orders
.skip()
.perform_cmd(send_request(model.new_message.clone()));
}
Msg::Fetched(fetch_object) => {
model.response = Some(fetch_object.response());
}
}
}
fn send_request(new_message: String) -> impl Future<Item=Msg, Error=Msg> {
fetch::Request::new(get_request_url())
.method(fetch::Method::Post)
.send_json(&shared::RequestExampleA { text: new_message })
.fetch_json(Msg::Fetched)
}
// View
pub fn view(model: &Model) -> impl ElContainer<Msg> {
let message = match &model.response {
Some(response) => {
match response {
Ok(response) => {
div![
format!(r#"{}. message: "{}""#, response.data.ordinal_number, response.data.text)
]
}
Err(fail_reason) => {
log!("Example_A error:", fail_reason);
//@TODO: [BUG] it cannot be `seed::empty()` because of order bug (rewrite after fix)
div![]
}
}
}
//@TODO: [BUG] div![] cannot be `seed::empty()` because of order bug (rewrite after fix)
None => div![]
};
vec![
message,
input![
input_ev(Ev::Input, Msg::NewMessageChanged),
attrs!{At::Value => model.new_message}
],
button![
simple_ev(Ev::Click, Msg::SendRequest),
"Send message"
],
]
}