Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pub mod categories_list;
pub mod manual_start_number_input;
pub mod racers;
pub mod races_list;
pub mod th;
pub mod time_input;
pub mod track_start;
pub mod upload_results;
7 changes: 4 additions & 3 deletions src/components/racers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use chrono::{DateTime, Local, TimeDelta, Utc};
use dioxus::prelude::*;

use crate::{
components::categories_list::CategoriesList,
components::{
categories_list::CategoriesList,
th::{Sorter, Th},
},
race::{Category, Race, Racer, RacerField},
sort_table::Th,
sorter::Sorter,
};

fn format_time(datetime: Option<DateTime<Utc>>) -> String {
Expand Down
76 changes: 60 additions & 16 deletions src/sort_table.rs → src/components/th.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,74 @@
use crate::sorter::{Direction, Sorter};
use std::cmp::Ordering;
use std::hash::Hash;

use dioxus::prelude::*;

/// Table header component that toggles sorting when clicked
#[derive(Props, PartialEq, Clone)]
pub struct ThProps<F: Copy + Eq + std::hash::Hash + 'static> {
pub sorter: Signal<Sorter<F>>,
pub field: F,
#[props(optional)]
pub filters: Option<Signal<std::collections::HashMap<F, String>>>,
pub children: Element,
/// Direction of sorting
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Direction {
Asc,
Desc,
}

/// Custom <Th> component that toggles sorting on click
#[allow(non_snake_case)]
pub fn Th<F: Copy + Eq + std::hash::Hash + 'static>(props: ThProps<F>) -> Element {
let mut sorter = props.sorter;
let filters = props.filters;
let field = props.field;
#[derive(Clone)]
pub struct Sorter<F: Copy + Eq + Hash> {
pub active: F,
pub direction: Direction,
}

impl<F: Copy + Eq + Hash> Sorter<F> {
pub fn new(field: F) -> Self {
Self {
active: field,
direction: Direction::Asc,
}
}

/// Toggle sorting for a given field
pub fn toggle(&mut self, field: F) {
if field == self.active {
self.direction = match self.direction {
Direction::Asc => Direction::Desc,
Direction::Desc => Direction::Asc,
};
} else {
self.active = field;
self.direction = Direction::Asc;
}
}

/// Compare two values using a custom comparator
pub fn cmp_by<T>(
&self,
a: &T,
b: &T,
field: F,
cmp_fn: impl Fn(&T, &T, F) -> Ordering,
) -> Ordering {
match (self.active, self.direction) {
(active_field, Direction::Desc) if active_field == field => {
cmp_fn(a, b, field).reverse()
}
_ => cmp_fn(a, b, field),
}
}
}

/// Custom <Th> component that toggles sorting on click
#[component]
pub fn Th<F: Copy + Eq + Hash + 'static>(
sorter: Signal<Sorter<F>>,
field: F,
filters: Option<Signal<std::collections::HashMap<F, String>>>,
children: Element,
) -> Element {
rsx! {
th { role: "button",
span {
onclick: move |_| {
sorter.write().toggle(field);
},
{props.children}
{children}
}

{
Expand Down
2 changes: 0 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ mod race;
mod race_events;
mod restclient;
mod rfid_reader;
mod sort_table;
mod sorter;

const MAIN_CSS: Asset = asset!("/assets/main.css");
const BOOTSTRAP_CSS: Asset = asset!("/assets/bootstrap.css");
Expand Down
53 changes: 0 additions & 53 deletions src/sorter.rs

This file was deleted.

Loading