askama: link list view

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

View file

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

View file

@ -1,12 +1,6 @@
use axum::{ use axum::{extract::State, response::IntoResponse, routing::get, Json, Router};
extract::State,
response::{Html, IntoResponse},
routing::get,
Json, Router,
};
use leptos::CollectView;
use crate::{util::ssr, AppState, HtmxPage}; use crate::{askama::LinkListTemplate, AppState};
pub fn get_records_router() -> Router<AppState> { pub fn get_records_router() -> Router<AppState> {
// Records views // 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 { pub async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone(); let records = state.records.lock().await.clone();
let records_list_view = records let record_keys: Vec<String> = records.keys().cloned().collect();
.keys() LinkListTemplate { record_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>
}
}))
} }

View file

@ -1,6 +1,5 @@
pub mod headers; pub mod headers;
pub mod logging; pub mod logging;
pub mod ssr;
pub mod sweeper; pub mod sweeper;
use rand::{ 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) 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; use serde::Deserialize;
pub mod askama; pub mod askama;
pub mod base_page;
pub use base_page::*;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CatFact { pub struct CatFact {

23
templates/linklist.html Normal file
View file

@ -0,0 +1,23 @@
{% extends "base.html" %}
{% block content %}
<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>
{% endblock content %}