diff --git a/.cargo/config.toml b/.cargo/config.toml index 60d260e..f4e8c00 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ -[profile] +[build] target = "wasm32-unknown-unknown" diff --git a/Cargo.toml b/Cargo.toml index 3acfcae..00dc21a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,9 +4,9 @@ version = "0.1.0" edition = "2021" [dependencies] -yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } +gloo-console = "0.3.0" gloo-net = "0.2" serde = { version = "1.0", features = ["derive"] } wasm-bindgen-futures = "0.4" wasm-cookies = "0.2.1" -gloo-console = "0.3.0" +yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } diff --git a/src/main.rs b/src/main.rs index 6d956e3..a908f73 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,10 @@ +use std::collections::HashSet; + extern crate serde; use gloo_console::log; +use gloo_net::http; extern crate wasm_bindgen_futures; -extern crate wasm_cookies; +use wasm_cookies as cookies; use yew::prelude::*; #[derive(Properties, Clone, PartialEq)] @@ -11,9 +14,9 @@ struct CookieProps { impl CookieProps { fn setup_cookie() -> Self { - let cookie_options = wasm_cookies::CookieOptions::default() + let cookie_options = cookies::CookieOptions::default() .expires_after(core::time::Duration::from_secs(52 * 7 * 24 * 60 * 60)); - match wasm_cookies::get("test") { + match cookies::get("test") { Some(Ok(cookie)) => { log!("got cookie"); return Self { cookie: cookie }; @@ -26,7 +29,7 @@ impl CookieProps { } } log!("setting cookie"); - wasm_cookies::set("test", "123", &cookie_options); + cookies::set("test", "123", &cookie_options); let cookie = "123".to_string(); // XXX Don't do this!! No secrets in logs! // log!(&cookie); @@ -43,16 +46,71 @@ fn cookie(CookieProps { cookie }: &CookieProps) -> Html { } } +#[derive(Properties, Clone, PartialEq, serde::Deserialize)] +struct JokeStruct { + id: String, + whos_there: String, + answer_who: String, + tags: Option>, + source: Option, +} + +impl JokeStruct { + async fn get_joke() -> Result { + http::Request::get("http://localhost:3000/api/v1/joke") + .send() + .await? + .json() + .await + } +} + +#[derive(Properties, Clone, PartialEq, serde::Deserialize)] +struct JokeProps { + joke: JokeStruct, +} + +#[function_component(Joke)] +fn joke(joke: &JokeProps) -> Html { + html! { +
+ {"Knock-Knock!"}
+ {"Who's there?"}
+ {joke.joke.whos_there.clone()}
+ {format!("{} who?", &joke.joke.whos_there)}
+ {joke.joke.answer_who.clone()}
+
+ } +} + #[function_component(App)] fn app() -> Html { let cookie = use_state(|| CookieProps::setup_cookie()); + let joke = use_state(|| Err(gloo_net::Error::GlooError("uninit".to_string()))); + let get_joke = joke.clone(); + use_effect_with((), move |()| { + wasm_bindgen_futures::spawn_local(async move { + let joke = JokeStruct::get_joke().await; + get_joke.set(joke); + }); + || () + }); + html! { <>

{ "Knock-Knock" }

+ if let Ok(ref joke) = *joke { + + } + if let Err(ref error) = *joke { +
+

{error.to_string().clone()}

+
+ } } }