From 67be18f2280bb78cc7b2fc50e01611f262203fc2 Mon Sep 17 00:00:00 2001 From: Zynh Ludwig Date: Thu, 14 Nov 2024 06:50:40 -0800 Subject: [PATCH] askama: link view --- src/router/link.rs | 12 +++++------ src/router/upload.rs | 9 +++------ src/views/askama.rs | 30 ++++++++++++++++++++++++++++ src/views/links.rs | 47 -------------------------------------------- src/views/mod.rs | 2 -- templates/link.html | 24 ++++++++++++++++++++++ 6 files changed, 63 insertions(+), 61 deletions(-) delete mode 100644 src/views/links.rs create mode 100644 templates/link.html diff --git a/src/router/link.rs b/src/router/link.rs index 64b3494..85ff8b3 100644 --- a/src/router/link.rs +++ b/src/router/link.rs @@ -6,7 +6,7 @@ use axum::{ }; use reqwest::StatusCode; -use crate::{util::ssr, AppState, AsyncRemoveRecord, DownloadLinkPage}; +use crate::{askama::DownloadLinkTemplate, AppState, AsyncRemoveRecord}; pub fn get_link_router() -> Router { // Link pages @@ -18,7 +18,7 @@ pub fn get_link_router() -> Router { async fn link( axum::extract::Path(id): axum::extract::Path, State(mut state): State, -) -> Result, Redirect> { +) -> Result { { let mut records = state.records.lock().await; @@ -26,10 +26,10 @@ async fn link( .get_mut(&id) .filter(|record| record.can_be_downloaded()) { - return Ok(Html(ssr::render({ - let record = record.clone(); - || leptos::view! { } - }))); + return Ok(DownloadLinkTemplate { + id, + record: record.clone(), + }); } } diff --git a/src/router/upload.rs b/src/router/upload.rs index 337bc01..410b20c 100644 --- a/src/router/upload.rs +++ b/src/router/upload.rs @@ -1,5 +1,6 @@ use std::path::Path; +use askama::Template; use async_zip::{base::write::ZipFileWriter, Compression, ZipEntryBuilder}; use axum::{ extract::{DefaultBodyLimit, Multipart, State}, @@ -14,11 +15,7 @@ use tokio::io; use tokio_util::{compat::FuturesAsyncWriteCompatExt, io::StreamReader}; use tower_http::limit::RequestBodyLimitLayer; -use crate::{ - cache, - util::{self, ssr}, - AppState, LinkView, UploadRecord, -}; +use crate::{askama::DownloadLinkFragment, cache, util, AppState, UploadRecord}; pub fn get_upload_router() -> Router { // Upload needs a subrouter to increase the body limit @@ -92,7 +89,7 @@ async fn upload_to_zip( .status(200) .header("Content-Type", "text/html") .header("HX-Push-Url", format!("/link/{}", &id)) - .body(ssr::render(|| leptos::view! { })) + .body(DownloadLinkFragment { id, record }.render().unwrap()) .unwrap(); Ok(response) diff --git a/src/views/askama.rs b/src/views/askama.rs index 6ba4069..46322a8 100644 --- a/src/views/askama.rs +++ b/src/views/askama.rs @@ -1,5 +1,7 @@ use askama_axum::Template; +use crate::{link::get_remaining_text, UploadRecord}; + #[derive(Template)] #[template(path = "welcome.html")] pub struct WelcomeTemplate { @@ -11,3 +13,31 @@ impl WelcomeTemplate { 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) + } +} diff --git a/src/views/links.rs b/src/views/links.rs deleted file mode 100644 index b26178e..0000000 --- a/src/views/links.rs +++ /dev/null @@ -1,47 +0,0 @@ -use leptos::{component, view, IntoView}; - -use crate::link::get_remaining_text; -use crate::state::UploadRecord; -use crate::HtmxPage; - -// -// #TODO: Handle pushing cleaner -#[component] -pub fn DownloadLinkPage(id: String, record: UploadRecord) -> impl IntoView { - view! { - -
- -
-
- } -} - -#[component] -pub fn LinkView(id: String, record: UploadRecord) -> impl IntoView { - let downloads_remaining = record.max_downloads - record.downloads; - view! { -
- - - - - - - "Return to home" - -
- } -} diff --git a/src/views/mod.rs b/src/views/mod.rs index 62533d7..de9b140 100644 --- a/src/views/mod.rs +++ b/src/views/mod.rs @@ -3,10 +3,8 @@ use serde::Deserialize; pub mod askama; pub mod base_page; -pub mod links; pub use base_page::*; -pub use links::*; #[derive(Debug, Deserialize)] pub struct CatFact { diff --git a/templates/link.html b/templates/link.html new file mode 100644 index 0000000..209364a --- /dev/null +++ b/templates/link.html @@ -0,0 +1,24 @@ +{% extends "base.html" %} + +{% block content %} +
+
+ + + + + + "Return to home" +
+
+{% endblock content %}