askama: link view

This commit is contained in:
Zynh Ludwig 2024-11-14 06:50:40 -08:00
parent e693115f10
commit ac1699ab39
6 changed files with 63 additions and 61 deletions

View file

@ -6,7 +6,7 @@ use axum::{
}; };
use reqwest::StatusCode; use reqwest::StatusCode;
use crate::{util::ssr, AppState, AsyncRemoveRecord, DownloadLinkPage}; use crate::{askama::DownloadLinkTemplate, AppState, AsyncRemoveRecord};
pub fn get_link_router() -> Router<AppState> { pub fn get_link_router() -> Router<AppState> {
// Link pages // Link pages
@ -18,7 +18,7 @@ pub fn get_link_router() -> Router<AppState> {
async fn link( async fn link(
axum::extract::Path(id): axum::extract::Path<String>, axum::extract::Path(id): axum::extract::Path<String>,
State(mut state): State<AppState>, State(mut state): State<AppState>,
) -> Result<Html<String>, Redirect> { ) -> Result<impl IntoResponse, Redirect> {
{ {
let mut records = state.records.lock().await; let mut records = state.records.lock().await;
@ -26,10 +26,10 @@ async fn link(
.get_mut(&id) .get_mut(&id)
.filter(|record| record.can_be_downloaded()) .filter(|record| record.can_be_downloaded())
{ {
return Ok(Html(ssr::render({ return Ok(DownloadLinkTemplate {
let record = record.clone(); id,
|| leptos::view! { <DownloadLinkPage id record /> } record: record.clone(),
}))); });
} }
} }

View file

@ -1,5 +1,6 @@
use std::path::Path; use std::path::Path;
use askama::Template;
use async_zip::{base::write::ZipFileWriter, Compression, ZipEntryBuilder}; use async_zip::{base::write::ZipFileWriter, Compression, ZipEntryBuilder};
use axum::{ use axum::{
extract::{DefaultBodyLimit, Multipart, State}, extract::{DefaultBodyLimit, Multipart, State},
@ -14,11 +15,7 @@ use tokio::io;
use tokio_util::{compat::FuturesAsyncWriteCompatExt, io::StreamReader}; use tokio_util::{compat::FuturesAsyncWriteCompatExt, io::StreamReader};
use tower_http::limit::RequestBodyLimitLayer; use tower_http::limit::RequestBodyLimitLayer;
use crate::{ use crate::{askama::DownloadLinkFragment, cache, util, AppState, UploadRecord};
cache,
util::{self, ssr},
AppState, LinkView, UploadRecord,
};
pub fn get_upload_router() -> Router<AppState> { pub fn get_upload_router() -> Router<AppState> {
// Upload needs a subrouter to increase the body limit // Upload needs a subrouter to increase the body limit
@ -92,7 +89,7 @@ async fn upload_to_zip(
.status(200) .status(200)
.header("Content-Type", "text/html") .header("Content-Type", "text/html")
.header("HX-Push-Url", format!("/link/{}", &id)) .header("HX-Push-Url", format!("/link/{}", &id))
.body(ssr::render(|| leptos::view! { <LinkView id record /> })) .body(DownloadLinkFragment { id, record }.render().unwrap())
.unwrap(); .unwrap();
Ok(response) Ok(response)

View file

@ -1,5 +1,7 @@
use askama_axum::Template; use askama_axum::Template;
use crate::{link::get_remaining_text, UploadRecord};
#[derive(Template)] #[derive(Template)]
#[template(path = "welcome.html")] #[template(path = "welcome.html")]
pub struct WelcomeTemplate { pub struct WelcomeTemplate {
@ -11,3 +13,31 @@ impl WelcomeTemplate {
WelcomeTemplate { fact } WelcomeTemplate { fact }
} }
} }
#[derive(Template)]
#[template(path = "link.html")]
pub struct DownloadLinkTemplate {
pub id: String,
pub record: UploadRecord,
}
impl DownloadLinkTemplate {
fn get_downloads_remaining_text(record: &UploadRecord) -> String {
let downloads_remaining = record.max_downloads - record.downloads;
get_remaining_text(downloads_remaining)
}
}
#[derive(Template)]
#[template(path = "link.html", block = "content")]
pub struct DownloadLinkFragment {
pub id: String,
pub record: UploadRecord,
}
impl DownloadLinkFragment {
fn get_downloads_remaining_text(record: &UploadRecord) -> String {
let downloads_remaining = record.max_downloads - record.downloads;
get_remaining_text(downloads_remaining)
}
}

View file

@ -1,47 +0,0 @@
use leptos::{component, view, IntoView};
use crate::link::get_remaining_text;
use crate::state::UploadRecord;
use crate::HtmxPage;
// <link href="../dist/css/link.css" rel="stylesheet" />
// #TODO: Handle pushing cleaner
#[component]
pub fn DownloadLinkPage(id: String, record: UploadRecord) -> impl IntoView {
view! {
<HtmxPage>
<div class="form-wrapper">
<LinkView id record />
</div>
</HtmxPage>
}
}
#[component]
pub fn LinkView(id: String, record: UploadRecord) -> impl IntoView {
let downloads_remaining = record.max_downloads - record.downloads;
view! {
<div class="column-container">
<div class="link-wrapper">
<a id="link" href="/download/{id}">
"Download Now!"
</a>
</div>
<div
class="link-wrapper"
hx-get="/link/{id}/remaining"
hx-trigger="click from:#link delay:0.2s, every 10s"
>
{get_remaining_text(downloads_remaining)}
</div>
<button class="return-button" onclick="clipboard()">
Copy to Clipboard
</button>
<a href="/" class="return-button">
"Return to home"
</a>
</div>
}
}

View file

@ -3,10 +3,8 @@ use serde::Deserialize;
pub mod askama; pub mod askama;
pub mod base_page; pub mod base_page;
pub mod links;
pub use base_page::*; pub use base_page::*;
pub use links::*;
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct CatFact { pub struct CatFact {

24
templates/link.html Normal file
View file

@ -0,0 +1,24 @@
{% extends "base.html" %}
{% block content %}
<div class="form-wrapper">
<div class="column-container">
<div class="link-wrapper">
<a id="link" href="/download/{{id}}"> "Download Now!" </a>
</div>
<div
class="link-wrapper"
hx-get="/link/{{id}}/remaining"
hx-trigger="click from:#link delay:0.2s, every 10s"
>
{{Self::get_downloads_remaining_text(record)}}
</div>
<button class="return-button" onclick="clipboard()">
Copy to Clipboard
</button>
<a href="/" class="return-button"> "Return to home" </a>
</div>
</div>
{% endblock content %}