From d84457f0387078af75012aa608a5cb7836bbcda0 Mon Sep 17 00:00:00 2001 From: Ada Werefox Date: Wed, 12 Apr 2023 05:42:59 +0000 Subject: [PATCH] Simplefied dark mode toggle. --- Rocket.toml | 1 - void-be/src/lib.rs | 36 ++++++++---------------------------- void-fe/src/lib.rs | 5 +++-- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/Rocket.toml b/Rocket.toml index 8ba6ea1..55e9da0 100644 --- a/Rocket.toml +++ b/Rocket.toml @@ -1,6 +1,5 @@ [default] port = 8345 -address = "100.64.0.2" # workers = 16 # max_blocking = 512 # keep_alive = 5 diff --git a/void-be/src/lib.rs b/void-be/src/lib.rs index d9908a4..196501a 100644 --- a/void-be/src/lib.rs +++ b/void-be/src/lib.rs @@ -32,7 +32,7 @@ pub mod web_app_backend { let mut vdom = VirtualDom::new_with_props( void_app::HomePage, DarkModeProps { - slug: Some(String::new()), + slug: Some("/".to_string()), dark_mode, }, ); @@ -52,13 +52,14 @@ pub mod web_app_backend { ) } - #[get("/toggle-dark-mode")] - async fn dark_mode_root(cookies: &CookieJar<'_>) -> Redirect { + #[get("/?dark_mode&")] + async fn dark_mode(cookies: &CookieJar<'_>, callback: &str) -> Redirect { match cookies.get("dark-mode") { Some(_) => cookies.remove(Cookie::named("dark-mode")), None => cookies.add(Cookie::new("dark-mode", "true")), }; - Redirect::to("/") + let callback_uri = format!("{callback}"); + Redirect::to(callback_uri) } #[get("/")] @@ -98,24 +99,6 @@ pub mod web_app_backend { ) } - #[get("/toggle-dark-mode")] - async fn poems_dark_mode(cookies: &CookieJar<'_>) -> Redirect { - match cookies.get("dark-mode") { - Some(_) => cookies.remove(Cookie::named("dark-mode")), - None => cookies.add(Cookie::new("dark-mode", "true")), - }; - Redirect::to("/poems/") - } - - #[get("//toggle-dark-mode")] - async fn entry_dark_mode(cookies: &CookieJar<'_>, entry: &str) -> Redirect { - match cookies.get("dark-mode") { - Some(_) => cookies.remove(Cookie::named("dark-mode")), - None => cookies.add(Cookie::new("dark-mode", "true")), - }; - Redirect::to(format!("/poems/{entry}")) - } - #[get("/")] async fn latest_entry() -> Redirect { let slug = void_app::get_latest_entry(); @@ -162,7 +145,7 @@ pub mod web_app_backend { let mut vdom = VirtualDom::new_with_props( void_app::PoemPage, PoemRequest { - slug: entry.to_string(), + slug: format!("{entry}"), dark_mode: Some(dark_mode), }, ); @@ -192,11 +175,8 @@ pub mod web_app_backend { .mount("/poems/previous", routes![previous_entry]) .mount("/poems/next", routes![next_entry]) .mount("/poems/latest", routes![latest_entry]) - .mount( - "/poems", - routes![poems_dark_mode, poem_list, entry_dark_mode, poem], - ) - .mount("/", routes![dark_mode_root, index]) + .mount("/poems", routes![poem_list, poem]) + .mount("/", routes![dark_mode, index]) .attach(Template::fairing()) } } diff --git a/void-fe/src/lib.rs b/void-fe/src/lib.rs index 84a821b..f072001 100644 --- a/void-fe/src/lib.rs +++ b/void-fe/src/lib.rs @@ -146,7 +146,7 @@ pub mod void_app { let slug_ref = slug.as_str(); let dark_mode = cx.props.dark_mode; cx.render(rsx! { - a { href: "{slug_ref}/toggle-dark-mode", + a { href: "/?dark_mode&callback={slug_ref}", match dark_mode { true => { rsx! { @@ -490,6 +490,7 @@ pub mod void_app { // #[cfg(any(target_family = "unix", target_family = "windows"))] fn RenderPoemTitle(cx: Scope) -> Element { let slug = cx.props.slug.clone().expect("Slug prop was not passed."); + let callback = format!("/poems/{slug}"); let dark_mode = cx .props .dark_mode @@ -500,7 +501,7 @@ pub mod void_app { span { class: "p-4 ml-2 mr-2 flex text-xl text-center ring-4 bg-alice-werefox-grey-lightest dark:bg-alice-werefox-grey-dark ring-alice-werefox-red-dark dark:ring-alice-werefox-red text-alice-werefox-grey-dark dark:text-alice-werefox-grey-light", span { class: "flex flex-row align-middle mx-auto max-w-full justify-center", "{title} " - DarkModeButton { slug: slug, dark_mode: dark_mode } + DarkModeButton { slug: callback, dark_mode: dark_mode } } } })