-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.rs
136 lines (119 loc) · 3.91 KB
/
app.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
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
use super::connector;
use std::time::Duration;
use wasm_bindgen_futures::spawn_local;
use wasm_timer::Delay;
use yew::{html, Component, Html};
use yew::Context;
pub struct Model {
tasks: Vec<Task>,
}
pub struct Task {
pub status: i32,
pub fav: bool,
pub info: Option<connector::Payload>,
}
pub enum TaskMsg {
Fetchit(connector::Payload),
ToggleFav,
}
impl Component for Task {
type Message = TaskMsg;
type Properties = ();
fn create(ctx: &Context<Self>) -> Self {
let inst = Task {
status: 0,
fav: false,
info: None::<connector::Payload>,
};
let link = ctx.link().clone();
spawn_local(async move {
loop {
let info = connector::fetchit().await.unwrap();
if let Some(info) = info {
//let task_msg = TaskMsg::Fetchit(info);
log::info!("quote.body: {:?}", info.quote.body.clone());
link.send_message(TaskMsg::Fetchit(info));
}
Delay::new(Duration::from_secs(5)).await.unwrap();
}
});
inst
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Self::Message::Fetchit(info) => {
self.info = Some(info);
self.fav = false;
true
}
Self::Message::ToggleFav => {
self.fav = !self.fav;
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<div class="card mt-3">
<div class="card-body">
<figure>
<blockquote class="blockquote">
if self.fav {
<span class="position-absolute top-0 start-100 translate-middle badge rounded-pill bg-danger">
{ "1+" }
<span class="visually-hidden">{ "Number of likes" }</span>
</span>
}
<p>
{ self.info.as_ref().map(|val| val.quote.body.clone() ).unwrap_or("".to_string())}
</p>
<a class="btn btn-success position-absolute bottom-0 end-0 me-2 mb-2" href="#" onclick={ctx.link().callback(|_| TaskMsg::ToggleFav)}>
{ "Like" }
</a>
</blockquote>
<figcaption class="blockquote-footer">
<cite title="Source Title">
{ self.info.as_ref().map(|val| val.clone().quote.author).unwrap_or("fetching --->".to_string())}
</cite>
</figcaption>
</figure>
</div>
</div>
}
}
}
pub enum Msg {
DoIt,
}
impl Component for Model {
type Message = Msg;
type Properties = ();
fn create(_ctx: &Context<Self>) -> Self {
Model { tasks: Vec::new() }
}
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
match msg {
Msg::DoIt => {
let task = Task {
fav: false,
info: None::<connector::Payload>,
status: 0,
};
self.tasks.push(task);
true
}
}
}
fn view(&self, ctx: &Context<Self>) -> Html {
html! {
<>
<a class="btn btn-warning" href="#" onclick={ctx.link().callback(|_| Msg::DoIt)}>
{ "fetch quotes of the day" }
</a>
{ for self.tasks.iter().map(|_tsk| html! {
<Task />
})}
</>
}
}
}