Skip to content

Commit

Permalink
Basic frontend impl
Browse files Browse the repository at this point in the history
  • Loading branch information
cyqsimon committed Jan 26, 2024
1 parent 8fef88b commit ec3c60b
Showing 1 changed file with 43 additions and 6 deletions.
49 changes: 43 additions & 6 deletions src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ pub fn page(
}

let upload_route = format!("{}/upload", &conf.route_prefix);
let rm_route = format!("{}/rm", &conf.route_prefix);
let (sort_method, sort_order) = (query_params.sort, query_params.order);

let upload_action = build_upload_action(&upload_route, encoded_dir, sort_method, sort_order);
Expand All @@ -49,6 +50,17 @@ pub fn page(
.allowed_upload_dir
.iter()
.any(|x| encoded_dir.starts_with(&format!("/{x}")));
let rm_allowed = conf.allowed_rm_dir.is_empty()
|| conf
.allowed_rm_dir
.iter()
.any(|x| encoded_dir.starts_with(&format!("/{x}")));

// OR with other conditions in the future if more actions are added
let show_actions = conf.rm_enabled && rm_allowed;
let actions_conf = show_actions.then(|| ActionsConf {
rm_route: &rm_route,
});

html! {
(DOCTYPE)
Expand Down Expand Up @@ -133,9 +145,12 @@ pub fn page(
}
table {
thead {
th.name { (build_link("name", "Name", sort_method, sort_order)) }
th.size { (build_link("size", "Size", sort_method, sort_order)) }
th.date { (build_link("date", "Last modification", sort_method, sort_order)) }
th.name { (sortable_title("name", "Name", sort_method, sort_order)) }
th.size { (sortable_title("size", "Size", sort_method, sort_order)) }
th.date { (sortable_title("date", "Last modification", sort_method, sort_order)) }
@if show_actions {
th.actions { span { "Actions" } }
}
}
tbody {
@if !is_root {
Expand All @@ -151,7 +166,7 @@ pub fn page(
}
}
@for entry in entries {
(entry_row(entry, sort_method, sort_order, false))
(entry_row(entry, sort_method, sort_order, false, actions_conf))
}
}
}
Expand Down Expand Up @@ -205,7 +220,7 @@ pub fn raw(entries: Vec<Entry>, is_root: bool) -> Markup {
}
}
@for entry in entries {
(entry_row(entry, None, None, true))
(entry_row(entry, None, None, true, None))
}
}
}
Expand Down Expand Up @@ -451,7 +466,7 @@ fn parametrized_link(
}

/// Partial: table header link
fn build_link(
fn sortable_title(
name: &str,
title: &str,
sort_method: Option<SortingMethod>,
Expand Down Expand Up @@ -483,12 +498,29 @@ fn build_link(
}
}

/// Partial: rm form
fn rm_form(rm_route: &str, encoded_path: &str) -> Markup {
let rm_action = format!("{rm_route}?path={encoded_path}");
html! {
form class="rm_form" action=(rm_action) method="POST" {
button type="submit" { "X" }
}
}
}

#[derive(Copy, Clone, Debug)]
struct ActionsConf<'a> {
/// Route prefix for file removal POST requests.
rm_route: &'a str,
}

/// Partial: row for an entry
fn entry_row(
entry: Entry,
sort_method: Option<SortingMethod>,
sort_order: Option<SortingOrder>,
raw: bool,
actions_conf: Option<ActionsConf>,
) -> Markup {
html! {
tr {
Expand Down Expand Up @@ -546,6 +578,11 @@ fn entry_row(
}
}
}
@if let Some(conf) = actions_conf {
td.actions-cell {
(rm_form(conf.rm_route, &entry.link))
}
}
}
}
}
Expand Down

0 comments on commit ec3c60b

Please sign in to comment.