commit 94a5b2a6224bf9fdb7c5d87797287e10789fdb1c Author: Bart Massey Date: Wed May 29 09:31:19 2024 -0700 knock-knock client in yew diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3acfcae --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "knock-knock-yew" +version = "0.1.0" +edition = "2021" + +[dependencies] +yew = { git = "https://github.com/yewstack/yew/", features = ["csr"] } +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" diff --git a/index.html b/index.html new file mode 100644 index 0000000..e3dc762 --- /dev/null +++ b/index.html @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..d1459fc --- /dev/null +++ b/src/main.rs @@ -0,0 +1,55 @@ +extern crate serde; +use gloo_console::log; +extern crate wasm_bindgen_futures; +extern crate wasm_cookies; +use yew::prelude::*; +use std::sync::Arc; + +#[derive(Properties, Clone, PartialEq)] +struct CookieProps { + cookie: Arc, +} + +impl CookieProps { + fn setup_cookie() -> Self { + let cookie_options = wasm_cookies::CookieOptions::default() + .expires_after(core::time::Duration::from_secs(52 * 7 * 24 * 60 * 60)); + let cookie = wasm_cookies::get("test"); + let cookie = if let Some(cookie) = cookie { + cookie.unwrap() + } else { + log!("setting cookie"); + wasm_cookies::set("test", "123", &cookie_options); + "123".to_string() + }; + log!(&cookie); + Self { cookie: Arc::new(cookie) } + } +} + +#[function_component(Cookie)] +fn cookie(CookieProps { cookie }: &CookieProps) -> Html { + html! { +
+

{cookie.clone()}

+
+ } +} + +#[function_component(App)] +fn app() -> Html { + let cookie = use_state(|| CookieProps::setup_cookie()); + + html! { + <> +

{ "Knock-Knock" }

+
+ +
+ + } +} + +fn main() { + yew::Renderer::::new().render(); +}