Skip to content

Commit

Permalink
remove unnecessary query params
Browse files Browse the repository at this point in the history
  • Loading branch information
SuperStormer committed Oct 18, 2023
1 parent 9f8a3cf commit 8933f05
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 25 deletions.
8 changes: 8 additions & 0 deletions src/scripts/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,11 @@ export const SORT_FIELDS = [
"Total Downloads",
].map((v, i) => [v, i + 1]);
export const SORT_ORDERS = ["asc", "desc"].map((v) => [v, v]);

export const DEFAULT_PARAMS = {
classId: "6", // "Mods"
modLoaderType: "0", // "Any"
sortField: "6", // "Total Downloads"
sortOrder: "desc",
pageSize: "50",
};
4 changes: 2 additions & 2 deletions src/scripts/dropdowns.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export function populate_dropdown(dropdown, values, defaultValue) {
dropdown.prepend(defaultOption);
} else {
// otherwise, select the corresponding <option>
let option = Array.from(dropdown.options).find((x) => x.textContent === defaultValue);
dropdown.dataset.default = option.value;
let option = Array.from(dropdown.options).find((x) => x.value === defaultValue);
option.selected = true;
dropdown.dataset.default = defaultValue;
dropdown.dispatchEvent(new Event("change"));
}
}
Expand Down
13 changes: 0 additions & 13 deletions src/scripts/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,3 @@ export function filter_results(results, filters) {

return results;
}

export function update_query_params(params, filters) {
let params2 = new URLSearchParams(params);
// don't overwrite filters query params
let [include, exclude, max_ver, min_ver] = filters;
params2.set("filtersInclude", include.join(" "));
params2.set("filtersExclude", exclude.join(" "));
params2.set("maxVer", max_ver);
params2.set("minVer", min_ver);
if (window.location.search !== "?" + params2.toString()) {
history.pushState({}, "", "?" + params2.toString());
}
}
17 changes: 7 additions & 10 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MODLOADERS, SORT_FIELDS, SORT_ORDERS } from "./consts";
import { MODLOADERS, SORT_FIELDS, SORT_ORDERS, DEFAULT_PARAMS } from "./consts";
import { sort_subvers } from "./utils";
import { populate_dropdown, fetch_dropdown_values } from "./dropdowns";
import {
Expand All @@ -7,12 +7,9 @@ import {
fetch_results,
populate_results,
} from "./results";
import {
update_query_params,
get_active_filters,
populate_filters as _populate_filters,
} from "./filters";
import { get_active_filters, populate_filters as _populate_filters } from "./filters";

import { update_query_params } from "./params";
// eslint-disable-next-line sonarjs/cognitive-complexity
(async function () {
const by_id = document.getElementById.bind(document);
Expand Down Expand Up @@ -105,11 +102,11 @@ import {
populate_dropdown(
classes_el,
classes.map((x) => [x.name, x.id]),
"Mods"
DEFAULT_PARAMS.classId
);

// modloaders
populate_dropdown(modloader_el, MODLOADERS, "Any");
populate_dropdown(modloader_el, MODLOADERS, DEFAULT_PARAMS.modLoaderType);

sub_version_el.addEventListener("change", function () {
update_modloader();
Expand Down Expand Up @@ -164,8 +161,8 @@ import {
});

// sort field and order
populate_dropdown(sort_field_el, SORT_FIELDS, "Total Downloads");
populate_dropdown(sort_order_el, SORT_ORDERS, "desc");
populate_dropdown(sort_field_el, SORT_FIELDS, DEFAULT_PARAMS.sortField);
populate_dropdown(sort_order_el, SORT_ORDERS, DEFAULT_PARAMS.sortOrder);

/* prefill forms based on query params*/
function prefill_forms() {
Expand Down
21 changes: 21 additions & 0 deletions src/scripts/params.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { DEFAULT_PARAMS } from "./consts";
export function update_query_params(params, filters) {
let params2 = new URLSearchParams(params);
// don't overwrite filters query params
let [include, exclude, max_ver, min_ver] = filters;
params2.set("filtersInclude", include.join(" "));
params2.set("filtersExclude", exclude.join(" "));
params2.set("maxVer", max_ver);
params2.set("minVer", min_ver);

// remove unnecessary query params
// use Array.from to clone the entries to avoid deleting values from what we're iterating over
for (let [key, value] of Array.from(params2.entries())) {
if ((key in DEFAULT_PARAMS && value === DEFAULT_PARAMS[key]) || value === "") {
params2.delete(key);
}
}
if (window.location.search !== "?" + params2.toString()) {
history.pushState({}, "", "?" + params2.toString());
}
}

0 comments on commit 8933f05

Please sign in to comment.