Skip to content

Commit

Permalink
feat: impl game queue as enum instead of hard-coded
Browse files Browse the repository at this point in the history
  • Loading branch information
JacksonVirgo committed Jul 28, 2024
1 parent 0019023 commit 7637538
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 2 additions & 2 deletions src/components/forms/input/select_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl SelectMenuBuilder {
self
}

pub fn options(mut self, options: Vec<&str>) -> Self {
self.options = options.iter().map(|s| s.to_string()).collect();
pub fn options(mut self, options: Vec<String>) -> Self {
self.options = options;
self
}

Expand Down
10 changes: 2 additions & 8 deletions src/routes/api/dashboard/player_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
forms::input::select_menu::SelectMenuBuilder,
},
models::players::get_players,
routes::api::dashboard::setup_data::GameQueue,
AppState,
};
use actix_web::{
Expand Down Expand Up @@ -71,14 +72,7 @@ async fn player_data(state: Data<AppState>, raw_thread_id: web::Path<String>) ->
let game_queue = SelectMenuBuilder::new()
.name("game_queue")
.placeholder("Select the game queue")
.options(vec![
"Open",
"Newbie",
"Normal",
"Mini/Micro Theme",
"Large Theme",
"Other/Unknown",
])
.options(GameQueue::to_vec())
.is_required(true)
.default_value("Other/Unknown")
.build_html();
Expand Down
33 changes: 25 additions & 8 deletions src/routes/api/dashboard/setup_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@ use actix_web::{
HttpResponse, Responder,
};
use maud::html;
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter};

#[derive(EnumIter, Display, Debug)]
pub enum GameQueue {
#[strum(serialize = "Open")]
Open,
#[strum(serialize = "Newbie")]
Newbie,
#[strum(serialize = "Normal")]
Normal,
#[strum(serialize = "Mini/Micro Theme")]
MiniOrMicroTheme,
#[strum(serialize = "Large Theme")]
LargeTheme,
#[strum(serialize = "Other/Unknown")]
OtherOrUnknown,
}

impl GameQueue {
pub fn to_vec() -> Vec<String> {
GameQueue::iter().map(|q| q.to_string()).collect()
}
}

#[derive(serde::Deserialize, Debug)]
struct FormData {
Expand Down Expand Up @@ -53,14 +77,7 @@ async fn setup_data(state: Data<AppState>, raw_thread_id: web::Path<String>) ->
let game_queue = SelectMenuBuilder::new()
.name("game_queue")
.placeholder("Select the game queue")
.options(vec![
"Open",
"Newbie",
"Normal",
"Mini/Micro Theme",
"Large Theme",
"Other/Unknown",
])
.options(GameQueue::to_vec())
.is_required(true)
.default_value_option(game_queue)
.build_html();
Expand Down

0 comments on commit 7637538

Please sign in to comment.