Skip to content

Commit

Permalink
Make admin a little more bearable
Browse files Browse the repository at this point in the history
  • Loading branch information
ackwell committed Jan 11, 2025
1 parent 31bcc23 commit cc9850e
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 50 deletions.
18 changes: 16 additions & 2 deletions src/http/admin/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@ impl Render for BaseTemplate {
html {
head {
title { "admin | " (self.title) }
link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@picocss/pico@2/css/pico.min.css";
}
body {
h1 { (self.title) }
(self.content)
header.container {
nav {
ul {
li { strong { "boilmaster" } }
li { (self.title) }
}
ul {
li { a href="/admin" { "versions" } }
}
}
}

main.container {
(self.content)
}
}
}
}
Expand Down
51 changes: 28 additions & 23 deletions src/http/admin/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,32 @@ async fn get_version(
Ok((BaseTemplate {
title: format!("version {}", version_key),
content: html! {
h2 { "status" }
form action=(uri) method="post" {
ul {
li {
@if banned { "banned" }
@else { "ready" }
button type="submit" name="ban" value=(!banned) { "toggle ban" }
fieldset {
label {
"names"
input type="text" id="names" name="names" value={
@for (index, name) in names.into_iter().enumerate() {
@if index > 0 { ", " }
(name)
}
};
small { "comma separated" }
}
}
}

h2 { "names" }
form action=(uri) method="post" {
input type="text" name="names" value={
@for (index, name) in names.into_iter().enumerate() {
@if index > 0 { ", " }
(name)
label {
input
type="checkbox"
role="switch"
name="ban"
value="true"
checked[banned]
style="--pico-switch-checked-background-color: #AF291D"; // pico-color-red-600

"banned"
}
};
}

button type="submit" { "save" };
}

Expand Down Expand Up @@ -114,7 +121,8 @@ async fn get_version(

#[derive(Debug, Deserialize)]
struct VersionPostRequest {
names: Option<String>,
names: String,
// Will only be set if checked.
ban: Option<bool>,
}

Expand All @@ -125,14 +133,11 @@ async fn post_version(
State(Service { version, .. }): State<Service>,
Form(request): Form<VersionPostRequest>,
) -> Result<impl IntoResponse> {
if let Some(names) = request.names {
let names = names.split(',').map(str::trim);
version.set_names(version_key, names).await?;
}
let names = request.names.split(',').map(str::trim);
version.set_names(version_key, names).await?;

if let Some(ban) = request.ban {
version.set_banned(version_key, ban).await?;
}
let banned = request.ban.is_some();
version.set_banned(version_key, banned).await?;

Ok(Redirect::to(&uri.to_string()))
}
Expand Down
66 changes: 41 additions & 25 deletions src/http/admin/versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub fn router(state: HttpState) -> Router {

struct VersionInfo {
key: VersionKey,
patches: Vec<(String, String)>,
patch: String,
names: Vec<String>,
banned: bool,
}
Expand All @@ -37,51 +37,67 @@ async fn versions(
let version_info = |key: VersionKey| -> Result<_> {
let version = version_service.version(key).context("missing version")?;

let latest = version
let patch = version
.repositories
.into_iter()
.map(|repository| (repository.name, repository.patches.last().name.clone()))
.collect();
.first()
.map(|repository| repository.patches.last().name.clone())
.unwrap_or_else(|| "(NONE)".into());

Ok(VersionInfo {
key,
patches: latest,
patch,
names: version_service.names(key).context("missing version")?,
banned: version.ban_time.is_some(),
})
};

let versions = version_service
let mut versions = version_service
.keys()
.into_iter()
.map(version_info)
.collect::<Result<Vec<_>>>()?;

versions.sort_unstable_by(|a, b| a.patch.cmp(&b.patch).reverse());

Ok((BaseTemplate {
title: "versions".to_string(),
content: html! {
@for version in versions {
h2 {
a href={ (uri) "/" (version.key) } {
(version.key)
table.striped {
thead {
tr {
th { "key" }
th { "names" }
th { "banned" }
th { "patch" }
}
}

" ("
@for (index, name) in version.names.iter().enumerate() {
@if index > 0 { ", " }
(name)
}
")"
tbody {
@for version in versions {
tr {
th {
code {
a href={ (uri) "/" (version.key) } {
(version.key)
}
}
}

@if version.banned {
" (banned)"
}
}
td {
@for (index, name) in version.names.iter().enumerate() {
@if index > 0 { ", " }
(name)
}
}

td {
@if version.banned {
"❌"
}
}

dl {
@for (repository, patch) in &version.patches {
dt { (repository) }
dd { (patch) }
td { (version.patch) }
}
}
}
}
Expand Down

0 comments on commit cc9850e

Please sign in to comment.