diff --git a/Cargo.toml b/Cargo.toml index 00dc21a..b9d73d4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ gloo-net = "0.2" serde = { version = "1.0", features = ["derive"] } wasm-bindgen-futures = "0.4" wasm-cookies = "0.2.1" +web-sys = { version = "0.3.69", features = ["HtmlTextAreaElement"] } yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } diff --git a/src/cookie.rs b/src/cookie.rs index e1dce20..e068dfc 100644 --- a/src/cookie.rs +++ b/src/cookie.rs @@ -2,22 +2,20 @@ use crate::*; pub fn acquire_cookie() -> String { let cookie_options = cookies::CookieOptions::default() - .expires_after(core::time::Duration::from_secs( - 52 * 7 * 24 * 60 * 60 - )); + .expires_after(core::time::Duration::from_secs(52 * 7 * 24 * 60 * 60)); match cookies::get("test") { Some(Ok(cookie)) => { - log!("got cookie"); + // log!("got cookie"); return cookie; } - Some(Err(e)) => { - log!(format!("cookie error: {}", e)); + Some(Err(_)) => { + // log!(format!("cookie error: {}", e)); } None => { - log!("did not find cookie"); + // log!("did not find cookie"); } } - log!("setting cookie"); + // log!("setting cookie"); cookies::set("test", "123", &cookie_options); "123".to_string() } diff --git a/src/finder.rs b/src/finder.rs new file mode 100644 index 0000000..0bd5457 --- /dev/null +++ b/src/finder.rs @@ -0,0 +1,36 @@ +use crate::*; + +#[derive(Properties, Clone, PartialEq)] +pub struct FinderProps { + pub on_find: Callback>, +} + +#[function_component] +pub fn Finder(props: &FinderProps) -> Html { + let key = use_state(|| >::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! { <> + + + + {"Find this joke"} + + + > } +} diff --git a/src/joke.rs b/src/joke.rs index 4f64950..645d7e5 100644 --- a/src/joke.rs +++ b/src/joke.rs @@ -13,14 +13,9 @@ impl JokeStruct { pub async fn get_joke(key: Option) -> Msg { 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, - ), + Some(ref key) => format!("http://localhost:3000/api/v1/joke/{}", key,), }; - let response = http::Request::get(&request) - .send() - .await; + let response = http::Request::get(&request).send().await; match response { Err(e) => Msg::GotJoke(Err(e)), Ok(data) => Msg::GotJoke(data.json().await), diff --git a/src/main.rs b/src/main.rs index 1eb8f53..204e0da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,19 @@ mod cookie; +mod finder; mod joke; use cookie::*; +use finder::*; use joke::*; use std::collections::HashSet; extern crate serde; -use gloo_console::log; +// use gloo_console::log; use gloo_net::http; extern crate wasm_bindgen_futures; use wasm_cookies as cookies; +use web_sys::HtmlTextAreaElement; use yew::prelude::*; pub type JokeResult = Result; @@ -45,10 +48,16 @@ impl Component for App { fn update(&mut self, ctx: &Context, msg: Self::Message) -> bool { match msg { - Msg::GotJoke(joke) => self.joke = joke, - Msg::GetJoke(key) => App::refresh_joke(ctx, key), + Msg::GotJoke(joke) => { + self.joke = joke; + true + } + Msg::GetJoke(key) => { + // log!(format!("GetJoke: {:?}", key)); + App::refresh_joke(ctx, key); + false + } } - true } fn view(&self, ctx: &Context) -> Html { @@ -71,6 +80,7 @@ impl Component for App { {"Tell me another!"} + > } }