Added a random poem button.

This commit is contained in:
Ada Werefox 2023-04-18 04:50:24 +00:00
parent 5490393678
commit db2d86c5ee
4 changed files with 24 additions and 5 deletions

File diff suppressed because one or more lines are too long

View File

@ -17,6 +17,7 @@ console_error_panic_hook = "0.1.7"
log = "0.4.17"
dioxus-helmet = "0.2.4"
dioxus-use-storage = "0.3.0"
rand = "0.8.5"
[dependencies.serde]
version = "1.0.160"

View File

@ -74,6 +74,7 @@ pub mod void_app {
ButtonGroup {
NavigationButton { title: "See Latest Entry".to_string(), slug: helpers::get_latest_entry("".to_string()), user_prefs: user_prefs.clone() }
NavigationButton { title: "See Oldest Entry".to_string(), slug: helpers::get_oldest_entry("".to_string()), user_prefs: user_prefs.clone() }
NavigationButton { title: "See A Random Entry".to_string(), slug: helpers::get_random_entry(), user_prefs: user_prefs.clone() }
NavigationButton { title: "See All Entries".to_string(), slug: "/poems".to_string(), user_prefs: user_prefs.clone() }
}
Footer { theme: user_prefs.clone().get_theme(), font: user_prefs.get_font() }
@ -118,6 +119,7 @@ pub mod void_app {
ButtonGroup {
NavigationButton { title: "Oldest".to_string(), slug: helpers::get_oldest_entry(slug.clone()), user_prefs: user_prefs.clone() }
NavigationButton { title: "Previous".to_string(), slug: helpers::get_previous_entry(slug.clone()), user_prefs: user_prefs.clone() }
NavigationButton { title: "Random".to_string(), slug: helpers::get_random_entry(), user_prefs: user_prefs.clone() }
NavigationButton { title: "Next".to_string(), slug: helpers::get_next_entry(slug.clone()), user_prefs: user_prefs.clone() }
NavigationButton { title: "Latest".to_string(), slug: helpers::get_latest_entry(slug.clone()), user_prefs: user_prefs.clone() }
}

View File

@ -1,5 +1,6 @@
use rust_embed::RustEmbed;
use markdown::Options;
use rand::seq::SliceRandom;
use rust_embed::RustEmbed;
use std::collections::VecDeque;
#[derive(RustEmbed)]
@ -11,9 +12,13 @@ struct OtherData;
struct Poems;
pub fn get_homepage_paragraph() -> String {
let homepage_paragraph_content = OtherData::get("homepage.md").expect("Found homepage paragraph.");
let homepage_paragraph_to_string = std::str::from_utf8(homepage_paragraph_content.data.as_ref()).expect("Homepage file is valid UTF-8");
let test = markdown::to_html_with_options(homepage_paragraph_to_string, &Options::gfm()).unwrap();
let homepage_paragraph_content =
OtherData::get("homepage.md").expect("Found homepage paragraph.");
let homepage_paragraph_to_string =
std::str::from_utf8(homepage_paragraph_content.data.as_ref())
.expect("Homepage file is valid UTF-8");
let test =
markdown::to_html_with_options(homepage_paragraph_to_string, &Options::gfm()).unwrap();
test
}
@ -114,3 +119,14 @@ pub fn get_next_entry(current: String) -> String {
None => format!("/poems/{current}"),
}
}
pub fn get_random_entry() -> String {
let poem_list = get_poem_list();
let mut rng = rand::thread_rng();
let random_entry = poem_list
.choose(&mut rng)
.expect("Got a valid entry")
.1
.clone();
format!("/poems/{random_entry}")
}