Skip to content

Commit

Permalink
lazy loading of table using htmx
Browse files Browse the repository at this point in the history
  • Loading branch information
Joeyh021 committed Jul 5, 2023
1 parent dab3ef0 commit 0f87ad1
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 35 deletions.
27 changes: 21 additions & 6 deletions src/routes/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ pub async fn home() -> impl IntoResponse {
#[template(path = "app.html")]
struct PanelTemplate {
username: String,
redirects: Vec<crate::types::Redirect>,
}

async fn panel(
Expand All @@ -32,16 +31,30 @@ async fn panel(
if session.get::<String>("username").is_none() {
return Err(Redirect::to("/auth/login").into());
}

let username = session.get::<String>("username").unwrap();
Ok(PanelTemplate { username })
}

#[derive(Template)]
#[template(path = "table.html")]
struct TableTemplate {
redirects: Vec<crate::types::Redirect>,
}
async fn table(
session: ReadableSession,
State(state): State<AppState>,
) -> Result<impl IntoResponse> {
if session.get::<String>("username").is_none() {
return Err(Redirect::to("/auth/login").into());
}

let redirects = crate::db::get_all(&state.pool)
.await
.context("Could not get redirects from database")
.map_err(handle_error)?;

Ok(PanelTemplate {
username,
redirects,
})
Ok(TableTemplate { redirects })
}

async fn handle_form(
Expand Down Expand Up @@ -71,5 +84,7 @@ async fn handle_form(
}

pub fn app_routes() -> Router<AppState> {
Router::new().route("/panel", get(panel).post(handle_form))
Router::new()
.route("/panel", get(panel).post(handle_form))
.route("/panel/table", get(table))
}
36 changes: 7 additions & 29 deletions templates/app.html
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,13 @@ <h2 class="block font-bold py-2 px-4 mt-5 text-lg">Add new go link</h2>
>
</tr>
</thead>
<tbody class="divide-y divide-gray-200 bg-white">
{% for r in redirects %}
<tr>
<td
class="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-gray-900 sm:pl-6"
>{{ r.source }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{{ r.usages }}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.created %} {% when Some with (val) %} {{
val.format("%d/%m/%Y %H:%M") }} {% when None %} {% endmatch
%}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.author %} {% when Some with (val) %} {{ val }} {%
when None %} {% endmatch %}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.last_used %} {% when Some with (val) %} {{
val.format("%d/%m/%Y %H:%M") }} {% when None %} {% endmatch
%}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{{ r.sink }}</td
>
</tr>
{% endfor %}
<tbody
class="divide-y divide-gray-200 bg-white"
hx-get="panel/table"
hx-trigger="load"
hx-swap="outerHTML"
>
<tr> <td> Links loading...</td> </tr>
</tbody>
</table>
</div>
Expand Down
5 changes: 5 additions & 0 deletions templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
type="image/x-icon"
href="https://uwcs.co.uk/static/favicon-192.png"
/>
<script
src="https://unpkg.com/htmx.org@1.9.2"
integrity="sha384-L6OqL9pRWyyFU3+/bjdSri+iIphTN/bvYyM37tICVyOJkWZLpP2vGn6VUEXgzg6h"
crossorigin="anonymous"
></script>
</head>
<body>
<ul
Expand Down
27 changes: 27 additions & 0 deletions templates/table.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<tbody class="divide-y divide-gray-200 bg-white">
{% for r in redirects %}
<tr>
<td class="whitespace-nowrap py-4 pl-4 pr-3 text-sm text-gray-900 sm:pl-6"
>{{ r.source }}
</td>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{{ r.usages }}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.created %} {% when Some with (val) %} {{ val.format("%d/%m/%Y
%H:%M") }} {% when None %} {% endmatch %}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.author %} {% when Some with (val) %} {{ val }} {% when None %}
{% endmatch %}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{% match r.last_used %} {% when Some with (val) %} {{
val.format("%d/%m/%Y %H:%M") }} {% when None %} {% endmatch %}</td
>
<td class="whitespace-nowrap px-3 py-4 text-sm text-gray-500"
>{{ r.sink }}</td
>
</tr>
{% endfor %}
</tbody>

0 comments on commit 0f87ad1

Please sign in to comment.