create enum for sort_order

This commit is contained in:
manuel 2023-01-08 16:06:49 +01:00
parent 8de5366a35
commit ba580a7342
3 changed files with 48 additions and 36 deletions

View File

@ -1,4 +1,5 @@
use actix_web::{error, web, Error, HttpRequest, HttpResponse}; use actix_web::{error, web, Error, HttpRequest, HttpResponse};
use serde::Serialize;
use serde::{Deserialize}; use serde::{Deserialize};
use tera::{Context}; use tera::{Context};
use crate::prelude::*; use crate::prelude::*;
@ -13,6 +14,12 @@ use super::{ add_auth_context, user_can_access_page, render_unauthorized};
const DEFAULT_ENTITIES_PER_PAGE: u64 = 10; const DEFAULT_ENTITIES_PER_PAGE: u64 = 10;
#[derive(Debug, Serialize, Deserialize)]
pub enum SortOrder {
Asc,
Desc,
}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct Params { pub struct Params {
page: Option<u64>, page: Option<u64>,
@ -20,7 +27,7 @@ pub struct Params {
render_partial: Option<bool>, render_partial: Option<bool>,
search: Option<String>, search: Option<String>,
sort_by: Option<String>, sort_by: Option<String>,
sort_order: Option<String> sort_order: Option<SortOrder>
} }
pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>( pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
@ -53,7 +60,7 @@ pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
let db = data.get_db(); let db = data.get_db();
let sort_by = params.sort_by.clone().unwrap_or(view_model.primary_key.to_string()); let sort_by = params.sort_by.clone().unwrap_or(view_model.primary_key.to_string());
let sort_order = params.sort_order.clone().unwrap_or(String::new()); let sort_order = params.sort_order.as_ref().unwrap_or(&SortOrder::Asc);
let result = E::list(db, page, entities_per_page, &search).await; let result = E::list(db, page, entities_per_page, &search).await;
match result { match result {
Ok(res) => { Ok(res) => {
@ -94,6 +101,8 @@ pub async fn list<T: ActixAdminAppDataTrait, E: ActixAdminViewModelTrait>(
ctx.insert("search", &search); ctx.insert("search", &search);
ctx.insert("sort_by", &sort_by); ctx.insert("sort_by", &sort_by);
ctx.insert("sort_order", &sort_order); ctx.insert("sort_order", &sort_order);
ctx.insert("sort_order_asc", &SortOrder::Asc);
ctx.insert("sort_order_desc", &SortOrder::Desc);
let body = TERA let body = TERA
.render("list.html", &ctx) .render("list.html", &ctx)

View File

@ -42,10 +42,10 @@
function sort_by(column) { function sort_by(column) {
document.getElementById("sort_by").value = column; document.getElementById("sort_by").value = column;
current_sort_order = document.getElementById("sort_order").value; current_sort_order = document.getElementById("sort_order").value;
if (current_sort_order == "asc") { if (current_sort_order == "{{ sort_order_asc }}") {
document.getElementById("sort_order").value = "desc"; document.getElementById("sort_order").value = "{{ sort_order_desc }}";
} else { } else {
document.getElementById("sort_order").value = "asc"; document.getElementById("sort_order").value = "{{ sort_order_asc }}";
} }
document.getElementById('search_form').submit(); document.getElementById('search_form').submit();
} }

View File

@ -33,7 +33,6 @@
<p class="control has-icons-left has-icons-right"> <p class="control has-icons-left has-icons-right">
<input class="input is-rounded" type="search" id="search" value="{{ search }}" name="search" <input class="input is-rounded" type="search" id="search" value="{{ search }}" name="search"
placeholder="Search" placeholder="Search"
hx-push-url="true"
hx-get="/admin/{{ entity_name }}/list?render_partial=true&entities_per_page={{ entities_per_page }}&page={{ page }}&sort_by={{ sort_by }}&sort_order={{ sort_order }}" hx-get="/admin/{{ entity_name }}/list?render_partial=true&entities_per_page={{ entities_per_page }}&page={{ page }}&sort_by={{ sort_by }}&sort_order={{ sort_order }}"
hx-trigger="keyup changed delay:500ms, search" hx-target="#{{ entity_name }}table" hx-trigger="keyup changed delay:500ms, search" hx-target="#{{ entity_name }}table"
hx-indicator="#loading"> hx-indicator="#loading">
@ -80,9 +79,9 @@
<th onclick="sort_by('{{ view_model.primary_key }}');" class="is-clickable">{{ <th onclick="sort_by('{{ view_model.primary_key }}');" class="is-clickable">{{
view_model.primary_key | title }} view_model.primary_key | title }}
{% if sort_by == view_model.primary_key %} {% if sort_by == view_model.primary_key %}
{% if sort_order == "asc" %} {% if sort_order == "{{ sort_order_asc }}" %}
<i class="ml-1 fa-solid fa-caret-up"></i> <i class="ml-1 fa-solid fa-caret-up"></i>
{% else %} {% elif sort_order == "{{ sort_order_desc }}" %}
<i class="ml-1 fa-solid fa-caret-down"></i> <i class="ml-1 fa-solid fa-caret-down"></i>
{% endif %} {% endif %}
{% endif %} {% endif %}
@ -91,9 +90,9 @@
<th onclick="sort_by('{{ model_field.field_name }}');" class="is-clickable">{{ <th onclick="sort_by('{{ model_field.field_name }}');" class="is-clickable">{{
model_field.field_name | split(pat="_") | join(sep=" ") | title }} model_field.field_name | split(pat="_") | join(sep=" ") | title }}
{% if sort_by == model_field.field_name %} {% if sort_by == model_field.field_name %}
{% if sort_order == "asc" %} {% if sort_order == "{{ sort_order_asc }}" %}
<i class="ml-1 fa-solid fa-caret-up"></i> <i class="ml-1 fa-solid fa-caret-up"></i>
{% else %} {% elif sort_order == "{{ sort_order_desc }}" %}
<i class="ml-1 fa-solid fa-caret-down"></i> <i class="ml-1 fa-solid fa-caret-down"></i>
{% endif %} {% endif %}
{% endif %} {% endif %}
@ -141,38 +140,42 @@
</table> </table>
</div> </div>
</form> </form>
<nav hx-boost="true" hx-indicator="#loading" class="pagination is-rounded is-centered" role="pagination" aria-label="pagination"> <nav hx-boost="true" hx-indicator="#loading" class="pagination is-rounded is-centered" role="pagination"
aria-label="pagination">
{% if page > 1 %} {% if page > 1 %}
<a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}" <a href="?page={{ page - 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
class="pagination-previous left-arrow-click"><i class="fa-solid fa-arrow-left"></i> class="pagination-previous left-arrow-click"><i class="fa-solid fa-arrow-left"></i>
</a> </a>
{% endif %} {% endif %}
{% if page < num_pages %} {% if page < num_pages %} <a
<a href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}" href="?page={{ page + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
class="pagination-next right-arrow-click"><i class="fa-solid fa-arrow-right"></i> class="pagination-next right-arrow-click"><i class="fa-solid fa-arrow-right"></i>
</a> </a>
{% endif %} {% endif %}
<ul class="pagination-list"> <ul class="pagination-list">
<li> <li>
<a class="pagination-link {% if page == 1 %}is-current{% endif %}" <a class="pagination-link {% if page == 1 %}is-current{% endif %}"
href="?page={{ 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}" href="?page={{ 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
aria-label="Goto page 1">1</a> aria-label="Goto page 1">1</a>
</li> </li>
<li> <li>
<span class="pagination-ellipsis">&hellip;</span> <span class="pagination-ellipsis">&hellip;</span>
</li> </li>
{% for i in range(start=min_show_page,end=max_show_page) %} {% for i in range(start=min_show_page,end=max_show_page) %}
<li><a class="pagination-link {% if page == i+1 %}is-current{% endif %}" aria-label="Goto page {{ i + 1 }}" <li><a class="pagination-link {% if page == i+1 %}is-current{% endif %}"
href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">{{ aria-label="Goto page {{ i + 1 }}"
i + 1 }}</a></li> href="?page={{ i + 1 }}&entities_per_page={{ entities_per_page }}&search={{ search }}">{{
{%- endfor %} i + 1 }}</a></li>
<li> {%- endfor %}
<span class="pagination-ellipsis">&hellip;</span> <li>
</li> <span class="pagination-ellipsis">&hellip;</span>
<li> </li>
<a href="?page={{ num_pages }}&entities_per_page={{ entities_per_page }}&search={{ search }}" class="pagination-link is-rounded {% if page == num_pages %}is-current{% endif %}" aria-label="Goto page {{ num_pages }}">{{ num_pages }} </a> <li>
</li> <a href="?page={{ num_pages }}&entities_per_page={{ entities_per_page }}&search={{ search }}"
</ul> class="pagination-link is-rounded {% if page == num_pages %}is-current{% endif %}"
aria-label="Goto page {{ num_pages }}">{{ num_pages }} </a>
</li>
</ul>
</nav> </nav>
</div> </div>