reuse demolist

This commit is contained in:
Robin Appelman 2023-04-16 17:59:25 +02:00
commit 7bbaa70481
3 changed files with 22 additions and 22 deletions

View file

@ -1,14 +1,14 @@
use crate::data::demo::ListDemo;
use maud::{html, Markup, Render};
pub struct DemoList {
pub demos: Vec<ListDemo>,
pub struct DemoList<'a> {
pub demos: &'a [ListDemo],
}
impl Render for DemoList {
impl Render for DemoList<'_> {
fn render(&self) -> Markup {
html! {
@for demo in &self.demos {
@for demo in self.demos {
tr {
td .title {
a href = (demo.url()) { (demo.server) " - " (demo.red) " vs " (demo.blu) }

View file

@ -147,11 +147,11 @@ async fn index(
) -> Result<Markup> {
let filter = filter.map(|filter| filter.0).unwrap_or_default();
let demos = ListDemo::list(&app.connection, filter).await?;
let maps = map_list(&app.connection).await?.collect();
let maps: Vec<_> = map_list(&app.connection).await?.collect();
Ok(render(
Index {
demos,
maps,
demos: &demos,
maps: &maps,
api: &app.api,
},
session,
@ -283,7 +283,7 @@ async fn upload(State(app): State<Arc<App>>, session: SessionData) -> impl IntoR
async fn demo_list(State(app): State<Arc<App>>, filter: Option<Query<Filter>>) -> Result<Markup> {
let filter = filter.map(|filter| filter.0).unwrap_or_default();
let demos = ListDemo::list(&app.connection, filter).await?;
Ok(DemoList { demos }.render())
Ok(DemoList { demos: &demos }.render())
}
async fn handler_404() -> impl IntoResponse {

View file

@ -1,12 +1,13 @@
use crate::data::demo::ListDemo;
use crate::fragments::demo_list::DemoList;
use crate::pages::Page;
use demostf_build::Asset;
use maud::{html, Markup, Render};
use std::borrow::Cow;
pub struct Index<'a> {
pub demos: Vec<ListDemo>,
pub maps: Vec<String>,
pub demos: &'a [ListDemo],
pub maps: &'a [String],
pub api: &'a str,
}
@ -14,6 +15,15 @@ pub struct Index<'a> {
#[asset(source = "script/demo_list.js", url = "/demo_list.js")]
pub struct DemoListScript;
impl<'a> Index<'a> {
fn map_list(&self) -> impl Render + 'a {
MapList(&self.maps)
}
fn demo_list(&self) -> impl Render + 'a {
DemoList { demos: self.demos }
}
}
impl Page for Index<'_> {
fn title(&self) -> Cow<'static, str> {
"Demos - demos.tf".into()
@ -23,7 +33,7 @@ impl Page for Index<'_> {
let script = DemoListScript::url();
html! {
h1 { "Demos" }
#filter-bar data-maps = (MapList(&self.maps)) data-api-base = (self.api) {}
#filter-bar data-maps = (self.map_list()) data-api-base = (self.api) {}
table.demolist {
thead {
tr {
@ -35,17 +45,7 @@ impl Page for Index<'_> {
}
}
tbody {
@for demo in &self.demos {
tr {
td .title {
a href = (demo.url()) { (demo.server) " - " (demo.red) " vs " (demo.blu) }
}
td .format { (demo.format()) }
td .map { (demo.map) }
td .duration { (demo.duration()) }
td .date title = (demo.date()) { (demo.relative_date()) }
}
}
(self.demo_list())
}
}
script defer src = (script) type = "text/javascript" {}