render refactor
This commit is contained in:
parent
3bb1d11f8c
commit
391f9cfa01
2 changed files with 43 additions and 45 deletions
81
src/main.rs
81
src/main.rs
|
@ -30,6 +30,7 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|||
|
||||
mod cache;
|
||||
mod nyazoom_headers;
|
||||
pub mod ssr;
|
||||
mod state;
|
||||
mod util;
|
||||
mod views;
|
||||
|
@ -135,12 +136,11 @@ async fn remaining(
|
|||
|
||||
async fn welcome() -> impl IntoResponse {
|
||||
let cat_fact = views::get_cat_fact().await;
|
||||
Html(
|
||||
leptos::ssr::render_to_string(move || {
|
||||
leptos::view! { <Welcome fact=cat_fact /> }
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
Html(ssr::render(move || {
|
||||
leptos::view! {
|
||||
<Welcome fact=cat_fact />
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
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
|
||||
async fn records_links(State(state): State<AppState>) -> impl IntoResponse {
|
||||
let records = state.records.lock().await.clone();
|
||||
Html(
|
||||
leptos::ssr::render_to_string(move || {
|
||||
leptos::view! {
|
||||
<HtmxPage>
|
||||
<div class="form-wrapper">
|
||||
<div class="column-container">
|
||||
<ul>
|
||||
{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::<Vec<_>>()}
|
||||
</ul>
|
||||
</div>
|
||||
Html(ssr::render(move || {
|
||||
leptos::view! {
|
||||
<HtmxPage>
|
||||
<div class="form-wrapper">
|
||||
<div class="column-container">
|
||||
<ul>
|
||||
{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::<Vec<_>>()}
|
||||
</ul>
|
||||
</div>
|
||||
</HtmxPage>
|
||||
}
|
||||
})
|
||||
.to_string(),
|
||||
)
|
||||
</div>
|
||||
</HtmxPage>
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
||||
async fn link(
|
||||
|
@ -190,15 +187,12 @@ async fn link(
|
|||
.filter(|record| record.can_be_downloaded())
|
||||
{
|
||||
if record.can_be_downloaded() {
|
||||
return Ok(Html(
|
||||
leptos::ssr::render_to_string({
|
||||
let record = record.clone();
|
||||
|| {
|
||||
leptos::view! { <DownloadLinkPage id=id record=record /> }
|
||||
}
|
||||
})
|
||||
.into(),
|
||||
));
|
||||
return Ok(Html(ssr::render({
|
||||
let record = record.clone();
|
||||
|| {
|
||||
leptos::view! { <DownloadLinkPage id=id record=record /> }
|
||||
}
|
||||
})));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -294,12 +288,9 @@ async fn upload_to_zip(
|
|||
.status(200)
|
||||
.header("Content-Type", "text/html")
|
||||
.header("HX-Push-Url", format!("/link/{}", &id))
|
||||
.body(
|
||||
leptos::ssr::render_to_string(|| {
|
||||
leptos::view! { <LinkView id record /> }
|
||||
})
|
||||
.into(),
|
||||
)
|
||||
.body(ssr::render(|| {
|
||||
leptos::view! { <LinkView id record /> }
|
||||
}))
|
||||
.unwrap();
|
||||
|
||||
Ok(response)
|
||||
|
|
7
src/ssr.rs
Normal file
7
src/ssr.rs
Normal 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()
|
||||
}
|
Loading…
Reference in a new issue