askama: link list view

This commit is contained in:
Zynh Ludwig 2024-11-14 07:33:36 -08:00
parent ac1699ab39
commit a91e66fd70
8 changed files with 38 additions and 77 deletions

View file

@ -1,7 +1,7 @@
use axum::{
extract::{ConnectInfo, Request},
middleware::{self, Next},
response::{Html, IntoResponse},
response::IntoResponse,
routing::get,
Router,
};
@ -12,7 +12,7 @@ use std::{io, net::SocketAddr};
use nyazoom::*;
use util::{headers::ForwardedFor, logging, ssr, sweeper};
use util::{headers::ForwardedFor, logging, sweeper};
#[tokio::main]
async fn main() -> io::Result<()> {

View file

@ -1,12 +1,6 @@
use axum::{
extract::State,
response::{Html, IntoResponse},
routing::get,
Json, Router,
};
use leptos::CollectView;
use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
use crate::{util::ssr, AppState, HtmxPage};
use crate::{askama::LinkListTemplate, AppState};
pub fn get_records_router() -> Router<AppState> {
// Records views
@ -24,34 +18,6 @@ pub(crate) async fn records(State(state): State<AppState>) -> impl IntoResponse
pub async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone();
let records_list_view = records
.keys()
.map(|key| {
leptos::view! {
<li class="link-wrapper">
<a href="/link/{key}">{key}</a>
<button
style="margin-left: 1em;"
hx-target="closest .link-wrapper"
hx-swap="outerHTML"
hx-delete="/link/{key}"
>
"X"
</button>
</li>
}
})
.collect_view();
Html(ssr::render(move || {
leptos::view! {
<HtmxPage>
<div class="form-wrapper">
<div class="column-container">
<ul>{records_list_view}</ul>
</div>
</div>
</HtmxPage>
}
}))
let record_keys: Vec<String> = records.keys().cloned().collect();
LinkListTemplate { record_keys }
}

View file

@ -1,6 +1,5 @@
pub mod headers;
pub mod logging;
pub mod ssr;
pub mod sweeper;
use rand::{

View file

@ -1,7 +0,0 @@
pub fn render<F, N>(f: F) -> String
where
F: FnOnce() -> N + 'static,
N: leptos::IntoView,
{
leptos::ssr::render_to_string(f).to_string()
}

View file

@ -41,3 +41,10 @@ impl DownloadLinkFragment {
get_remaining_text(downloads_remaining)
}
}
#[derive(Template)]
#[template(path = "linklist.html")]
pub struct LinkListTemplate {
pub record_keys: Vec<String>,
}

View file

@ -1,26 +0,0 @@
use leptos::{component, view, Children, IntoView};
#[component]
pub fn HtmxPage(children: Children) -> impl IntoView {
view! {
<head>
<title>"Nyazoom"</title>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="/css/main.css" rel="stylesheet" />
<link href="/css/link.css" rel="stylesheet" />
<script src="/scripts/file_label.js" />
<script src="/scripts/link.js" />
<script
src="https://unpkg.com/htmx.org@2.0.2"
integrity="sha384-Y7hw+L/jvKeWIRRkqWYfPcvVxHzVzn5REgzbawhxAuQGwX1XWe70vji+VSeHOThJ"
crossorigin="anonymous"
></script>
</head>
<body>
<h1>"NyaZoom"<sup>"2"</sup></h1>
{children()}
</body>
}
}

View file

@ -2,9 +2,6 @@ use futures::TryFutureExt;
use serde::Deserialize;
pub mod askama;
pub mod base_page;
pub use base_page::*;
#[derive(Debug, Deserialize)]
pub struct CatFact {

25
templates/linklist.html Normal file
View file

@ -0,0 +1,25 @@
{% extends "base.html" %}
{% block content %}
<HtmxPage>
<div class="form-wrapper">
<div class="column-container">
<ul>
{% for key in record_keys %}
<li class="link-wrapper">
<a href="/link/{{ key }}">{{ key }}</a>
<button
style="margin-left: 1em;"
hx-target="closest .link-wrapper"
hx-swap="outerHTML"
hx-delete="/link/{{ key }}"
>
"X"
</button>
</li>
{% endfor %}
</ul>
</div>
</div>
</HtmxPage>
{% endblock content %}