added "tell me another" button
This commit is contained in:
parent
eb45baf455
commit
d69cc7d019
@ -1,6 +1,7 @@
|
|||||||
<!doctype html>
|
<!doctype html>
|
||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
|
<title>Knock Knock!</title>
|
||||||
<link data-trunk rel="css" href="index.css" />
|
<link data-trunk rel="css" href="index.css" />
|
||||||
</head>
|
</head>
|
||||||
<body></body>
|
<body></body>
|
||||||
|
11
src/joke.rs
11
src/joke.rs
@ -10,8 +10,15 @@ pub struct JokeStruct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl JokeStruct {
|
impl JokeStruct {
|
||||||
pub async fn get_joke() -> Msg {
|
pub async fn get_joke(key: Option<String>) -> Msg {
|
||||||
let response = http::Request::get("http://localhost:3000/api/v1/joke")
|
let request = match &key {
|
||||||
|
None => "http://localhost:3000/api/v1/joke".to_string(),
|
||||||
|
Some(ref key) => format!(
|
||||||
|
"http://localhost:3000/api/v1/joke?id={}",
|
||||||
|
key,
|
||||||
|
),
|
||||||
|
};
|
||||||
|
let response = http::Request::get(&request)
|
||||||
.send()
|
.send()
|
||||||
.await;
|
.await;
|
||||||
match response {
|
match response {
|
||||||
|
19
src/main.rs
19
src/main.rs
@ -22,6 +22,14 @@ struct App {
|
|||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
GotJoke(JokeResult),
|
GotJoke(JokeResult),
|
||||||
|
GetJoke(Option<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl App {
|
||||||
|
fn refresh_joke(ctx: &Context<Self>, key: Option<String>) {
|
||||||
|
let got_joke = JokeStruct::get_joke(key);
|
||||||
|
ctx.link().send_future(got_joke);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Component for App {
|
impl Component for App {
|
||||||
@ -30,20 +38,20 @@ impl Component for App {
|
|||||||
|
|
||||||
fn create(ctx: &Context<Self>) -> Self {
|
fn create(ctx: &Context<Self>) -> Self {
|
||||||
let cookie = acquire_cookie();
|
let cookie = acquire_cookie();
|
||||||
let got_joke = JokeStruct::get_joke();
|
App::refresh_joke(ctx, None);
|
||||||
ctx.link().send_future(got_joke);
|
|
||||||
let joke = Err(gloo_net::Error::GlooError("Loading Joke…".to_string()));
|
let joke = Err(gloo_net::Error::GlooError("Loading Joke…".to_string()));
|
||||||
Self { cookie, joke }
|
Self { cookie, joke }
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, _ctx: &Context<Self>, msg: Self::Message) -> bool {
|
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::GotJoke(joke) => self.joke = joke,
|
Msg::GotJoke(joke) => self.joke = joke,
|
||||||
|
Msg::GetJoke(key) => App::refresh_joke(ctx, key),
|
||||||
}
|
}
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self, _ctx: &Context<Self>) -> Html {
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||||
let cookie = &self.cookie;
|
let cookie = &self.cookie;
|
||||||
let joke = &self.joke;
|
let joke = &self.joke;
|
||||||
html! {
|
html! {
|
||||||
@ -60,6 +68,9 @@ impl Component for App {
|
|||||||
<span class="error">{format!("Server Error: {error}")}</span>
|
<span class="error">{format!("Server Error: {error}")}</span>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
<div>
|
||||||
|
<button onclick={ctx.link().callback(|_| Msg::GetJoke(None))}>{"Tell me another!"}</button>
|
||||||
|
</div>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user