mirror of
https://github.com/ae-utbm/sith.git
synced 2025-07-10 03:49:24 +00:00
Improve update_query_string with enum action
This commit is contained in:
@ -69,20 +69,35 @@ function getCSRFToken() {
|
||||
|
||||
const initialUrlParams = new URLSearchParams(window.location.search);
|
||||
|
||||
function update_query_string(key, value, push = true) {
|
||||
/**
|
||||
* @readonly
|
||||
* @enum {number}
|
||||
*/
|
||||
const History = {
|
||||
PUSH: 1,
|
||||
REPLACE: 2,
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {string} key
|
||||
* @param {string | string[] | null} value
|
||||
* @param {History} action
|
||||
*/
|
||||
function update_query_string(key, value, action = History.REPLACE) {
|
||||
const url = new URL(window.location.href);
|
||||
if (!value) {
|
||||
// If the value is null, undefined or empty => delete it
|
||||
url.searchParams.delete(key)
|
||||
} else if (Array.isArray(value)) {
|
||||
|
||||
url.searchParams.delete(key)
|
||||
value.forEach((v) => url.searchParams.append(key, v))
|
||||
} else {
|
||||
url.searchParams.set(key, value);
|
||||
}
|
||||
|
||||
if (push){
|
||||
if (action === History.PUSH) {
|
||||
history.pushState(null, document.title, url.toString());
|
||||
} else if (action === History.REPLACE) {
|
||||
history.replaceState(null, document.title, url.toString());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user