Created
August 2, 2023 12:04
-
-
Save mininaim/52e61fdb52455406afafb67d0a6a2f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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