From 5fb16fb5b71fc5833d5ff3f9b5cc44626101a8b2 Mon Sep 17 00:00:00 2001 From: Bart Massey Date: Wed, 29 May 2024 14:30:52 -0700 Subject: [PATCH] broke code into modules --- src/cookie.rs | 42 ++++++++++++++++++++++++++ src/joke.rs | 38 +++++++++++++++++++++++ src/main.rs | 84 +++++---------------------------------------------- 3 files changed, 87 insertions(+), 77 deletions(-) create mode 100644 src/cookie.rs create mode 100644 src/joke.rs diff --git a/src/cookie.rs b/src/cookie.rs new file mode 100644 index 0000000..0557e11 --- /dev/null +++ b/src/cookie.rs @@ -0,0 +1,42 @@ +use crate::*; + +#[derive(Properties, Clone, PartialEq)] +pub struct CookieProps { + pub cookie: String, +} + +impl CookieProps { + pub fn acquire_cookie() -> Self { + let cookie_options = cookies::CookieOptions::default() + .expires_after(core::time::Duration::from_secs( + 52 * 7 * 24 * 60 * 60 + )); + match cookies::get("test") { + Some(Ok(cookie)) => { + log!("got cookie"); + return Self { cookie: cookie }; + } + Some(Err(e)) => { + log!(format!("cookie error: {}", e)); + } + None => { + log!("did not find cookie"); + } + } + log!("setting cookie"); + cookies::set("test", "123", &cookie_options); + let cookie = "123".to_string(); + // XXX Don't do this!! No secrets in logs! + // log!(&cookie); + Self { cookie } + } +} + +#[function_component(Cookie)] +pub fn cookie(CookieProps { cookie }: &CookieProps) -> Html { + html! { +
+

{cookie.clone()}

+
+ } +} diff --git a/src/joke.rs b/src/joke.rs new file mode 100644 index 0000000..78272f5 --- /dev/null +++ b/src/joke.rs @@ -0,0 +1,38 @@ +use crate::*; + +#[derive(Properties, Clone, PartialEq, serde::Deserialize)] +pub struct JokeStruct { + pub id: String, + pub whos_there: String, + pub answer_who: String, + pub tags: Option>, + pub source: Option, +} + +impl JokeStruct { + pub async fn get_joke() -> Result { + http::Request::get("http://localhost:3000/api/v1/joke") + .send() + .await? + .json() + .await + } +} + +#[derive(Properties, Clone, PartialEq, serde::Deserialize)] +pub struct JokeProps { + pub joke: JokeStruct, +} + +#[function_component(Joke)] +pub 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()}
+
+ } +} diff --git a/src/main.rs b/src/main.rs index a908f73..a3d6fe7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,9 @@ +mod cookie; +mod joke; + +use cookie::*; +use joke::*; + use std::collections::HashSet; extern crate serde; @@ -7,85 +13,9 @@ extern crate wasm_bindgen_futures; use wasm_cookies as cookies; use yew::prelude::*; -#[derive(Properties, Clone, PartialEq)] -struct CookieProps { - cookie: String, -} - -impl CookieProps { - fn setup_cookie() -> Self { - let cookie_options = cookies::CookieOptions::default() - .expires_after(core::time::Duration::from_secs(52 * 7 * 24 * 60 * 60)); - match cookies::get("test") { - Some(Ok(cookie)) => { - log!("got cookie"); - return Self { cookie: cookie }; - } - Some(Err(e)) => { - log!(format!("cookie error: {}", e)); - } - None => { - log!("did not find cookie"); - } - } - log!("setting cookie"); - cookies::set("test", "123", &cookie_options); - let cookie = "123".to_string(); - // XXX Don't do this!! No secrets in logs! - // log!(&cookie); - Self { cookie } - } -} - -#[function_component(Cookie)] -fn cookie(CookieProps { cookie }: &CookieProps) -> Html { - html! { -
-

{cookie.clone()}

-
- } -} - -#[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 cookie = use_state(|| CookieProps::acquire_cookie()); let joke = use_state(|| Err(gloo_net::Error::GlooError("uninit".to_string()))); let get_joke = joke.clone();