Skip to content

Commit

Permalink
fix: losing sort from the params while using input fields
Browse files Browse the repository at this point in the history
(cherry picked from commit 030d2c6)
  • Loading branch information
krantheman authored and mergify[bot] committed Dec 12, 2023
1 parent a1624e7 commit 535b42d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 26 deletions.
58 changes: 36 additions & 22 deletions hrms/www/jobs/index.js
Original file line number Diff line number Diff line change
@@ -1,56 +1,70 @@
$(() => {
const query_params = frappe.utils.get_query_params();
show_applied_filters(query_params);
show_applied_filters();

$("input:checkbox").change(function () {
const filters = $("input").serialize();
scroll_up_and_update_params(filters);
scroll_up_and_update_params(get_new_params());
});

$("#clear-filters").on("click", function () {
scroll_up_and_update_params();
});

$("#search-box").bind("search", function () {
const filters = $("input").serialize();
scroll_up_and_update_params(filters);
scroll_up_and_update_params(get_new_params());
});

$("#search-box").keyup(function (e) {
if (e.keyCode == 13) {
$(this).trigger("search");
}
});

$("[name=sort]").on("click", function () {
const filters = $("input").serialize();
const sort = $.param({ sort: $(this).text() });
scroll_up_and_update_params(filters + "&" + sort);
});
});

function show_applied_filters(query_params) {
for (const filter in query_params) {
if (filter === "query") {
$("#search-box").val(query_params["query"]);
} else if (filter === "query=") {
continue;
}
if (typeof query_params[filter] === "string") {
$("#" + $.escapeSelector(query_params[filter])).prop("checked", true);
} else {
for (const d of query_params[filter])
$("#" + $.escapeSelector(d)).prop("checked", true);
function show_applied_filters() {
for (const filter in query_params) {
if (filter === "query") {
$("#search-box").val(query_params["query"]);
} else if (
["company", "department", "location", "employment_type"].includes(
filter
)
) {
if (typeof query_params[filter] === "string") {
$("#" + $.escapeSelector(query_params[filter])).prop("checked", true);
} else {
for (const d of query_params[filter])
$("#" + $.escapeSelector(d)).prop("checked", true);
}
} else {
continue;
}
}
}
}

function scroll_up_and_update_params(filters = "") {
function get_new_params() {
return "sort" in query_params
? $("input").serialize() + "&" + $.param({ sort: query_params["sort"] })
: $("input").serialize();
}
});

function scroll_up_and_update_params(params = "") {
if (window.scrollY === 0) {
window.location.href = "/jobs?" + filters;
window.location.href = "/jobs?" + params;
} else {
window.scroll({
top: 0,
behavior: "smooth",
});
window.addEventListener("scroll", function () {
if (window.scrollY === 0) {
window.location.href = "/jobs?" + filters;
window.location.href = "/jobs?" + params;
}
});
}
Expand Down
10 changes: 6 additions & 4 deletions hrms/www/jobs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@

def get_context(context):
context.parents = [{"name": _("My Account"), "route": "/"}]
args = frappe.request.args.to_dict(flat=False)
filters, txt, context.order_by = get_filters_txt_and_order_by(args)
context.job_openings = get_job_openings(filters, txt, context.order_by)
filters, txt, order_by = get_filters_txt_and_order_by()
context.job_openings = get_job_openings(filters, txt, order_by)
context.all_filters = get_all_filters(filters)
context.order_by = order_by


def get_job_openings(filters=None, txt=None, order_by=None, limit_start=0, limit_page_length=20):
Expand Down Expand Up @@ -78,11 +78,13 @@ def get_all_filters(filters=None):
return all_filters


def get_filters_txt_and_order_by(args):
def get_filters_txt_and_order_by():
args = frappe.request.args.to_dict(flat=False)
filters = {}
txt = ""
order_by = "Newest Post"
allowed_filters = ["company", "department", "location", "employment_type"]

for d in args:
if d in allowed_filters:
filters[d] = args[d]
Expand Down

0 comments on commit 535b42d

Please sign in to comment.