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"] }
|
||||
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"] }
|
||||
|
@ -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()
|
||||
}
|
||||
|
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 {
|
||||
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),
|
||||
|
18
src/main.rs
18
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<JokeStruct, gloo_net::Error>;
|
||||
@ -45,11 +48,17 @@ impl Component for App {
|
||||
|
||||
fn update(&mut self, ctx: &Context<Self>, 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn view(&self, ctx: &Context<Self>) -> Html {
|
||||
let cookie = &self.cookie;
|
||||
@ -71,6 +80,7 @@ impl Component for App {
|
||||
<div>
|
||||
<button onclick={ctx.link().callback(|_| Msg::GetJoke(None))}>{"Tell me another!"}</button>
|
||||
</div>
|
||||
<Finder on_find={ctx.link().callback(Msg::GetJoke)}/>
|
||||
</>
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user