From 2ec1f8cdc00323ebaccfac529f178d76754ab342 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 11 Aug 2024 14:58:05 +0200 Subject: [PATCH 1/5] Fix back action in uv guide --- pedagogy/templates/pedagogy/guide.jinja | 77 +++++++++++++++++++------ 1 file changed, 59 insertions(+), 18 deletions(-) diff --git a/pedagogy/templates/pedagogy/guide.jinja b/pedagogy/templates/pedagogy/guide.jinja index 1aa713e4..639ee0ca 100644 --- a/pedagogy/templates/pedagogy/guide.jinja +++ b/pedagogy/templates/pedagogy/guide.jinja @@ -114,13 +114,25 @@ @@ -141,34 +153,63 @@ Alpine.data("uv_search", () => ({ uvs: [], loading: false, - page: parseInt(initialUrlParams.get("page")) || page_default, - page_size: parseInt(initialUrlParams.get("page_size")) || page_size_default, - search: initialUrlParams.get("search") || "", - department: initialUrlParams.getAll("department"), - credit_type: initialUrlParams.getAll("credit_type"), - {# The semester is easier to use on the backend as an enum (spring/autumn/both/none) - and easier to use on the frontend as an array ([spring, autumn]). - Thus there is some conversion involved when both communicate together #} - semester: initialUrlParams.has("semester") ? - initialUrlParams.get("semester").split("_AND_") : [], + page: page_default, + page_size: page_size_default, + search: "", + department: [], + credit_type: [], + semester: [], + pushstate: History.PUSH, + + async initialize_args() { + let url = new URLSearchParams(window.location.search); + this.pushstate = History.REPLACE; + + this.page = parseInt(url.get("page")) || page_default;; + this.page_size = parseInt(url.get("page_size")) || page_size_default; + this.search = url.get("search") || ""; + this.department = url.getAll("department"); + this.credit_type = url.getAll("credit_type"); + {# The semester is easier to use on the backend as an enum (spring/autumn/both/none) + and easier to use on the frontend as an array ([spring, autumn]). + Thus there is some conversion involved when both communicate together #} + this.semester = url.has("semester") ? + url.get("semester").split("_AND_") : []; + + {# Wait for all watch callbacks to be called #} + await (new Promise(resolve => setTimeout(resolve, 100))); + + this.pushstate = History.PUSH; + }, async init() { + await this.initialize_args(); let search_params = ["search", "department", "credit_type", "semester"]; - let pagination_params = ["semester", "page"]; + let pagination_params = ["page", "page_size"]; + + this.fetch_data(); {# load initial data #} + search_params.forEach((param) => { - this.$watch(param, async () => { - {# Reset pagination on search #} + this.$watch(param, async (value) => { + if (this.pushstate != History.PUSH){ + {# This means that we are doing a mass param edit #} + return; + } + {# Reset pagination on search #} this.page = page_default; this.page_size = page_size_default; }); }); search_params.concat(pagination_params).forEach((param) => { this.$watch(param, async (value) => { - update_query_string(param, value); + console.log(param + " " + value) + update_query_string(param, value, this.pushstate); await this.fetch_data(); {# reload data on form change #} }); }); - await this.fetch_data(); {# load initial data #} + window.addEventListener("popstate", () => { + this.initialize_args(); + }); }, async fetch_data() { From 2a6c1f050d6f7281a4250e8e127e86408a2a3369 Mon Sep 17 00:00:00 2001 From: Sli Date: Sun, 11 Aug 2024 15:11:51 +0200 Subject: [PATCH 2/5] Create a paginate_alpine macro --- core/templates/core/macros.jinja | 40 +++++++++++++++++++++++++ pedagogy/templates/pedagogy/guide.jinja | 25 ++-------------- sas/templates/sas/album.jinja | 24 ++------------- 3 files changed, 44 insertions(+), 45 deletions(-) diff --git a/core/templates/core/macros.jinja b/core/templates/core/macros.jinja index 2344de37..11014a64 100644 --- a/core/templates/core/macros.jinja +++ b/core/templates/core/macros.jinja @@ -116,6 +116,46 @@ {% endif %} {% endmacro %} +{% macro paginate_alpine(page, nb_pages) %} + {# Add pagination buttons for ajax based content with alpine + + Notes: + This can only be used in the scope of your alpine datastore + + Notes: + You might need to listen to the "popstate" event in your code + to update the current page you are on when the user goes back in + it's browser history with the back arrow + + Parameters: + page (str): name of the alpine page variable in your datastore + nb_page (str): call to a javascript function or variable returning + the maximum number of pages to paginate + #} + +{% endmacro %} + {% macro paginate(page_obj, paginator, js_action) %} {% set js = js_action|default('') %} {% if page_obj.has_previous() or page_obj.has_next() %} diff --git a/pedagogy/templates/pedagogy/guide.jinja b/pedagogy/templates/pedagogy/guide.jinja index 639ee0ca..e2ab4d22 100644 --- a/pedagogy/templates/pedagogy/guide.jinja +++ b/pedagogy/templates/pedagogy/guide.jinja @@ -1,4 +1,5 @@ {% extends "core/base.jinja" %} +{% from 'core/macros.jinja' import paginate_alpine %} {% block title %} {% trans %}UV Guide{% endtrans %} @@ -113,29 +114,7 @@ - + {{ paginate_alpine("page", "max_page()") }}