added joke finder
This commit is contained in:
parent
d69cc7d019
commit
7b80847e65
@ -9,4 +9,5 @@ gloo-net = "0.2"
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
wasm-bindgen-futures = "0.4"
|
wasm-bindgen-futures = "0.4"
|
||||||
wasm-cookies = "0.2.1"
|
wasm-cookies = "0.2.1"
|
||||||
|
web-sys = { version = "0.3.69", features = ["HtmlTextAreaElement"] }
|
||||||
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
|
yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] }
|
||||||
|
@ -2,22 +2,20 @@ use crate::*;
|
|||||||
|
|
||||||
pub fn acquire_cookie() -> String {
|
pub fn acquire_cookie() -> String {
|
||||||
let cookie_options = cookies::CookieOptions::default()
|
let cookie_options = cookies::CookieOptions::default()
|
||||||
.expires_after(core::time::Duration::from_secs(
|
.expires_after(core::time::Duration::from_secs(52 * 7 * 24 * 60 * 60));
|
||||||
52 * 7 * 24 * 60 * 60
|
|
||||||
));
|
|
||||||
match cookies::get("test") {
|
match cookies::get("test") {
|
||||||
Some(Ok(cookie)) => {
|
Some(Ok(cookie)) => {
|
||||||
log!("got cookie");
|
// log!("got cookie");
|
||||||
return cookie;
|
return cookie;
|
||||||
}
|
}
|
||||||
Some(Err(e)) => {
|
Some(Err(_)) => {
|
||||||
log!(format!("cookie error: {}", e));
|
// log!(format!("cookie error: {}", e));
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
log!("did not find cookie");
|
// log!("did not find cookie");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log!("setting cookie");
|
// log!("setting cookie");
|
||||||
cookies::set("test", "123", &cookie_options);
|
cookies::set("test", "123", &cookie_options);
|
||||||
"123".to_string()
|
"123".to_string()
|
||||||
}
|
}
|
||||||
|
36
src/finder.rs
Normal file
36
src/finder.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use crate::*;
|
||||||
|
|
||||||
|
#[derive(Properties, Clone, PartialEq)]
|
||||||
|
pub struct FinderProps {
|
||||||
|
pub on_find: Callback<Option<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[function_component]
|
||||||
|
pub fn Finder(props: &FinderProps) -> Html {
|
||||||
|
let key = use_state(|| <Option<String>>::None);
|
||||||
|
let change_key = {
|
||||||
|
let key = key.clone();
|
||||||
|
Callback::from(move |e: InputEvent| {
|
||||||
|
let input: HtmlTextAreaElement = e.target_unchecked_into();
|
||||||
|
let value = input.value();
|
||||||
|
// log!(format!("key change: {:?}", value));
|
||||||
|
let value = value.trim();
|
||||||
|
let value = if value.is_empty() {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(value.to_string())
|
||||||
|
};
|
||||||
|
// log!(format!("key change final: {:?}", value));
|
||||||
|
key.set(value);
|
||||||
|
})
|
||||||
|
};
|
||||||
|
let props = props.clone();
|
||||||
|
html! { <>
|
||||||
|
<div>
|
||||||
|
<input type="text" placeholder="joke id" oninput={change_key}/>
|
||||||
|
<button onclick={move |_| props.on_find.emit((*key).clone())}>
|
||||||
|
{"Find this joke"}
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</> }
|
||||||
|
}
|
@ -13,14 +13,9 @@ impl JokeStruct {
|
|||||||
pub async fn get_joke(key: Option<String>) -> Msg {
|
pub async fn get_joke(key: Option<String>) -> Msg {
|
||||||
let request = match &key {
|
let request = match &key {
|
||||||
None => "http://localhost:3000/api/v1/joke".to_string(),
|
None => "http://localhost:3000/api/v1/joke".to_string(),
|
||||||
Some(ref key) => format!(
|
Some(ref key) => format!("http://localhost:3000/api/v1/joke/{}", key,),
|
||||||
"http://localhost:3000/api/v1/joke?id={}",
|
|
||||||
key,
|
|
||||||
),
|
|
||||||
};
|
};
|
||||||
let response = http::Request::get(&request)
|
let response = http::Request::get(&request).send().await;
|
||||||
.send()
|
|
||||||
.await;
|
|
||||||
match response {
|
match response {
|
||||||
Err(e) => Msg::GotJoke(Err(e)),
|
Err(e) => Msg::GotJoke(Err(e)),
|
||||||
Ok(data) => Msg::GotJoke(data.json().await),
|
Ok(data) => Msg::GotJoke(data.json().await),
|
||||||
|
18
src/main.rs
18
src/main.rs
@ -1,16 +1,19 @@
|
|||||||
mod cookie;
|
mod cookie;
|
||||||
|
mod finder;
|
||||||
mod joke;
|
mod joke;
|
||||||
|
|
||||||
use cookie::*;
|
use cookie::*;
|
||||||
|
use finder::*;
|
||||||
use joke::*;
|
use joke::*;
|
||||||
|
|
||||||
use std::collections::HashSet;
|
use std::collections::HashSet;
|
||||||
|
|
||||||
extern crate serde;
|
extern crate serde;
|
||||||
use gloo_console::log;
|
// use gloo_console::log;
|
||||||
use gloo_net::http;
|
use gloo_net::http;
|
||||||
extern crate wasm_bindgen_futures;
|
extern crate wasm_bindgen_futures;
|
||||||
use wasm_cookies as cookies;
|
use wasm_cookies as cookies;
|
||||||
|
use web_sys::HtmlTextAreaElement;
|
||||||
use yew::prelude::*;
|
use yew::prelude::*;
|
||||||
|
|
||||||
pub type JokeResult = Result<JokeStruct, gloo_net::Error>;
|
pub type JokeResult = Result<JokeStruct, gloo_net::Error>;
|
||||||
@ -45,10 +48,16 @@ impl Component for App {
|
|||||||
|
|
||||||
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) => {
|
||||||
Msg::GetJoke(key) => App::refresh_joke(ctx, key),
|
self.joke = joke;
|
||||||
|
true
|
||||||
|
}
|
||||||
|
Msg::GetJoke(key) => {
|
||||||
|
// log!(format!("GetJoke: {:?}", key));
|
||||||
|
App::refresh_joke(ctx, key);
|
||||||
|
false
|
||||||
|
}
|
||||||
}
|
}
|
||||||
true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&self, ctx: &Context<Self>) -> Html {
|
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||||
@ -71,6 +80,7 @@ impl Component for App {
|
|||||||
<div>
|
<div>
|
||||||
<button onclick={ctx.link().callback(|_| Msg::GetJoke(None))}>{"Tell me another!"}</button>
|
<button onclick={ctx.link().callback(|_| Msg::GetJoke(None))}>{"Tell me another!"}</button>
|
||||||
</div>
|
</div>
|
||||||
|
<Finder on_find={ctx.link().callback(Msg::GetJoke)}/>
|
||||||
</>
|
</>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user