render refactor

main
Zynh Ludwig 2024-08-28 18:45:12 -07:00
parent 3bb1d11f8c
commit 391f9cfa01
2 changed files with 43 additions and 45 deletions

View File

@ -30,6 +30,7 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod cache; mod cache;
mod nyazoom_headers; mod nyazoom_headers;
pub mod ssr;
mod state; mod state;
mod util; mod util;
mod views; mod views;
@ -135,12 +136,11 @@ async fn remaining(
async fn welcome() -> impl IntoResponse { async fn welcome() -> impl IntoResponse {
let cat_fact = views::get_cat_fact().await; let cat_fact = views::get_cat_fact().await;
Html( Html(ssr::render(move || {
leptos::ssr::render_to_string(move || { leptos::view! {
leptos::view! { <Welcome fact=cat_fact /> } <Welcome fact=cat_fact />
}) }
.to_string(), }))
)
} }
async fn records(State(state): State<AppState>) -> impl IntoResponse { async fn records(State(state): State<AppState>) -> impl IntoResponse {
@ -151,31 +151,28 @@ async fn records(State(state): State<AppState>) -> impl IntoResponse {
// this behind some kind of authentication // this behind some kind of authentication
async fn records_links(State(state): State<AppState>) -> impl IntoResponse { async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
let records = state.records.lock().await.clone(); let records = state.records.lock().await.clone();
Html( Html(ssr::render(move || {
leptos::ssr::render_to_string(move || { leptos::view! {
leptos::view! { <HtmxPage>
<HtmxPage> <div class="form-wrapper">
<div class="form-wrapper"> <div class="column-container">
<div class="column-container"> <ul>
<ul> {records.keys().map(|key| leptos::view! {
{records.keys().map(|key| leptos::view! { <li class="link-wrapper">
<li class="link-wrapper"> <a href="/link/{key}">{key}</a>
<a href="/link/{key}">{key}</a> <button style="margin-left: 1em;"
<button style="margin-left: 1em;" hx-target="closest .link-wrapper"
hx-target="closest .link-wrapper" hx-swap="outerHTML"
hx-swap="outerHTML" hx-delete="/link/{key}">X</button>
hx-delete="/link/{key}">X</button> </li>
</li> })
}) .collect::<Vec<_>>()}
.collect::<Vec<_>>()} </ul>
</ul>
</div>
</div> </div>
</HtmxPage> </div>
} </HtmxPage>
}) }
.to_string(), }))
)
} }
async fn link( async fn link(
@ -190,15 +187,12 @@ async fn link(
.filter(|record| record.can_be_downloaded()) .filter(|record| record.can_be_downloaded())
{ {
if record.can_be_downloaded() { if record.can_be_downloaded() {
return Ok(Html( return Ok(Html(ssr::render({
leptos::ssr::render_to_string({ let record = record.clone();
let record = record.clone(); || {
|| { leptos::view! { <DownloadLinkPage id=id record=record /> }
leptos::view! { <DownloadLinkPage id=id record=record /> } }
} })));
})
.into(),
));
} }
} }
} }
@ -294,12 +288,9 @@ 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( .body(ssr::render(|| {
leptos::ssr::render_to_string(|| { leptos::view! { <LinkView id record /> }
leptos::view! { <LinkView id record /> } }))
})
.into(),
)
.unwrap(); .unwrap();
Ok(response) Ok(response)

7
src/ssr.rs Normal file
View File

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