Skip to content

Instantly share code, notes, and snippets.

@mininaim
Created August 2, 2023 12:04
Show Gist options
  • Save mininaim/52e61fdb52455406afafb67d0a6a2f09 to your computer and use it in GitHub Desktop.
Save mininaim/52e61fdb52455406afafb67d0a6a2f09 to your computer and use it in GitHub Desktop.
<script defer>
const hmtlEntitiesDecode = (encodedStr) => $("<div/>").html(encodedStr).text();
const sortProductsResult = (event) => {
try {
event.preventDefault();
const sort = event.target.value;
const urlMap = {
oldest: "sort_by=created_at&order=asc",
latest: "sort_by=created_at&order=desc",
popularity_low: "sort_by=popularity_order&order=asc",
popularity_high: "sort_by=popularity_order&order=desc",
price_low: "sort_by=price&order=asc",
price_high: "sort_by=price&order=desc",
};
const currentUrl = new URL(window.location.href);
const existingParams = currentUrl.search;
const sortingParams = urlMap[sort] || "";
let url = currentUrl.origin + currentUrl.pathname;
if (existingParams) {
const params = new URLSearchParams(existingParams);
params.delete("sort_by");
params.delete("order");
url += "?" + params.toString() + "&" + sortingParams;
} else {
url += "?" + sortingParams;
}
history.pushState({}, "", url);
location.reload();
} catch (err) {
console.error(err);
}
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment