Simplefied dark mode toggle.

This commit is contained in:
Ada Werefox 2023-04-12 05:42:59 +00:00
parent 09bb699e0c
commit d84457f038
3 changed files with 11 additions and 31 deletions

View File

@ -1,6 +1,5 @@
[default] [default]
port = 8345 port = 8345
address = "100.64.0.2"
# workers = 16 # workers = 16
# max_blocking = 512 # max_blocking = 512
# keep_alive = 5 # keep_alive = 5

View File

@ -32,7 +32,7 @@ pub mod web_app_backend {
let mut vdom = VirtualDom::new_with_props( let mut vdom = VirtualDom::new_with_props(
void_app::HomePage, void_app::HomePage,
DarkModeProps { DarkModeProps {
slug: Some(String::new()), slug: Some("/".to_string()),
dark_mode, dark_mode,
}, },
); );
@ -52,13 +52,14 @@ pub mod web_app_backend {
) )
} }
#[get("/toggle-dark-mode")] #[get("/?dark_mode&<callback>")]
async fn dark_mode_root(cookies: &CookieJar<'_>) -> Redirect { async fn dark_mode(cookies: &CookieJar<'_>, callback: &str) -> Redirect {
match cookies.get("dark-mode") { match cookies.get("dark-mode") {
Some(_) => cookies.remove(Cookie::named("dark-mode")), Some(_) => cookies.remove(Cookie::named("dark-mode")),
None => cookies.add(Cookie::new("dark-mode", "true")), None => cookies.add(Cookie::new("dark-mode", "true")),
}; };
Redirect::to("/") let callback_uri = format!("{callback}");
Redirect::to(callback_uri)
} }
#[get("/")] #[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("/<entry>/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("/")] #[get("/")]
async fn latest_entry() -> Redirect { async fn latest_entry() -> Redirect {
let slug = void_app::get_latest_entry(); let slug = void_app::get_latest_entry();
@ -162,7 +145,7 @@ pub mod web_app_backend {
let mut vdom = VirtualDom::new_with_props( let mut vdom = VirtualDom::new_with_props(
void_app::PoemPage, void_app::PoemPage,
PoemRequest { PoemRequest {
slug: entry.to_string(), slug: format!("{entry}"),
dark_mode: Some(dark_mode), dark_mode: Some(dark_mode),
}, },
); );
@ -192,11 +175,8 @@ pub mod web_app_backend {
.mount("/poems/previous", routes![previous_entry]) .mount("/poems/previous", routes![previous_entry])
.mount("/poems/next", routes![next_entry]) .mount("/poems/next", routes![next_entry])
.mount("/poems/latest", routes![latest_entry]) .mount("/poems/latest", routes![latest_entry])
.mount( .mount("/poems", routes![poem_list, poem])
"/poems", .mount("/", routes![dark_mode, index])
routes![poems_dark_mode, poem_list, entry_dark_mode, poem],
)
.mount("/", routes![dark_mode_root, index])
.attach(Template::fairing()) .attach(Template::fairing())
} }
} }

View File

@ -146,7 +146,7 @@ pub mod void_app {
let slug_ref = slug.as_str(); let slug_ref = slug.as_str();
let dark_mode = cx.props.dark_mode; let dark_mode = cx.props.dark_mode;
cx.render(rsx! { cx.render(rsx! {
a { href: "{slug_ref}/toggle-dark-mode", a { href: "/?dark_mode&callback={slug_ref}",
match dark_mode { match dark_mode {
true => { true => {
rsx! { rsx! {
@ -490,6 +490,7 @@ pub mod void_app {
// #[cfg(any(target_family = "unix", target_family = "windows"))] // #[cfg(any(target_family = "unix", target_family = "windows"))]
fn RenderPoemTitle(cx: Scope<PoemData>) -> Element { fn RenderPoemTitle(cx: Scope<PoemData>) -> Element {
let slug = cx.props.slug.clone().expect("Slug prop was not passed."); let slug = cx.props.slug.clone().expect("Slug prop was not passed.");
let callback = format!("/poems/{slug}");
let dark_mode = cx let dark_mode = cx
.props .props
.dark_mode .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: "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", span { class: "flex flex-row align-middle mx-auto max-w-full justify-center",
"{title}&nbsp;" "{title}&nbsp;"
DarkModeButton { slug: slug, dark_mode: dark_mode } DarkModeButton { slug: callback, dark_mode: dark_mode }
} }
} }
}) })