mirror of
https://github.com/ae-utbm/sith.git
synced 2024-11-10 00:03:24 +00:00
Fix back function in album pagination
This commit is contained in:
parent
8174bce720
commit
b35e1a476e
@ -69,7 +69,7 @@ function getCSRFToken() {
|
||||
|
||||
const initialUrlParams = new URLSearchParams(window.location.search);
|
||||
|
||||
function update_query_string(key, value) {
|
||||
function update_query_string(key, value, push = true) {
|
||||
const url = new URL(window.location.href);
|
||||
if (!value) {
|
||||
// If the value is null, undefined or empty => delete it
|
||||
@ -81,5 +81,8 @@ function update_query_string(key, value) {
|
||||
} else {
|
||||
url.searchParams.set(key, value);
|
||||
}
|
||||
|
||||
if (push){
|
||||
history.pushState(null, document.title, url.toString());
|
||||
}
|
||||
}
|
||||
|
@ -129,13 +129,25 @@
|
||||
Alpine.data("pictures", () => ({
|
||||
pictures: {},
|
||||
page: parseInt(initialUrlParams.get("page")) || 1,
|
||||
pushstate: true, /* Used to avoid pushing a state on a back action */
|
||||
loading: false,
|
||||
|
||||
async init() {
|
||||
await this.fetch_pictures();
|
||||
this.$watch("page", () => {
|
||||
update_query_string("page", this.page === 1 ? null : this.page);
|
||||
this.fetch_pictures()
|
||||
update_query_string("page",
|
||||
this.page === 1 ? null : this.page,
|
||||
this.pushstate
|
||||
);
|
||||
this.pushstate = true;
|
||||
this.fetch_pictures();
|
||||
});
|
||||
|
||||
window.addEventListener("popstate", () => {
|
||||
this.pushstate = false;
|
||||
this.page = parseInt(
|
||||
new URLSearchParams(window.location.search).get("page")
|
||||
) || 1;
|
||||
});
|
||||
},
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user