Skip to content

Commit

Permalink
feat: add sort by
Browse files Browse the repository at this point in the history
(cherry picked from commit 78981e3)
  • Loading branch information
krantheman authored and mergify[bot] committed Dec 12, 2023
1 parent a3fa7c5 commit a1624e7
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 56 deletions.
77 changes: 54 additions & 23 deletions hrms/www/jobs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ <h3 class="mt-0 mb-10">{{ _("Job Openings") }}</h3>
<p class="lg-text font-weight-bold">{{ _("Filters") }}</p>
<a id="clear-filters" class="ml-auto sm-text" role="button">{{ _("Clear All") }}</a>
</div>
{% for name, values in filters.items() %}
{% for name, values in all_filters.items() %}
<div class="mb-10">
<p class="font-weight-bold mb-4">{{ name.title() | replace('_', ' ') }}</p>
{% for value in values %}
Expand All @@ -30,28 +30,59 @@ <h3 class="mt-0 mb-10">{{ _("Job Openings") }}</h3>
{% endfor %}
</div>
<div class="col-9">
<div class="mb-12 col-8 pl-0">
<input type="search"
name="query"
id="search-box"
class="form-control border font-md"
placeholder="Search for Jobs"
aria-label="Jobs Search"
style="height: 36px">
<div class="search-icon">
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="feather feather-search">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
<div class="row px-4 mb-12 ">
<div class="col-8 pl-0">
<input type="search"
name="query"
id="search-box"
class="form-control border font-md"
placeholder="Search for Jobs"
aria-label="Jobs Search"
style="height: 40px">
<div class="search-icon">
<svg xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
class="feather feather-search">
<circle cx="11" cy="11" r="8"></circle>
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
</svg>
</div>
</div>
<div class="col-4 flex pr-0">
<div class="ml-auto flex align-items-center">
<div class="md-text text-secondary flex"
type="button"
data-toggle="dropdown"
aria-haspopup="true"
aria-expanded="false">
Sort by:
<div class="font-weight-bold ml-3">
{{ order_by }}
<svg xmlns="http://www.w3.org/2000/svg"
width="10"
height="10"
fill="currentColor"
stroke="currentColor"
stroke-width="3"
class="bi bi-chevron-down"
viewBox="0 0 16 16">
<path fill-rule="evenodd" d="M1.646 4.646a.5.5 0 0 1 .708 0L8 10.293l5.646-5.647a.5.5 0 0 1 .708.708l-6 6a.5.5 0 0 1-.708 0l-6-6a.5.5 0 0 1 0-.708z" />
</svg>
</div>
</div>
<div class="dropdown-menu border" aria-labelledby="dropdownMenuButton">
{% for sort in ["Newest Post", "Oldest Post", "Earliest Closing", "Latest Closing"] %}
<a name="sort" class="dropdown-item text-secondary">{{ sort }}</a>
{% endfor %}
</div>
</div>
</div>
</div>
<p class="text-secondary mb-4 md-text">Showing {{ job_openings|length }} results</p>
Expand Down
30 changes: 17 additions & 13 deletions hrms/www/jobs/index.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
$(() => {
show_applied_filters();
const query_params = frappe.utils.get_query_params();
show_applied_filters(query_params);
$("input:checkbox").change(function () {
const filters = $("input").serialize();
scroll_up_and_update_filters(filters);
scroll_up_and_update_params(filters);
});
$("#clear-filters").on("click", function () {
scroll_up_and_update_filters();
scroll_up_and_update_params();
});
$("#search-box").bind("search", function () {
const filters = $("input").serialize();
scroll_up_and_update_filters(filters);
scroll_up_and_update_params(filters);
});
$("#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() {
const query_params = frappe.utils.get_query_params();
if ("query" in query_params) {
$("#search-box").val(query_params["query"]);
delete query_params["query"];
} else if ("query=" in query_params) {
delete query_params["query="];
}
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 {
Expand All @@ -36,7 +40,7 @@ function show_applied_filters() {
}
}

function scroll_up_and_update_filters(filters = "") {
function scroll_up_and_update_params(filters = "") {
if (window.scrollY === 0) {
window.location.href = "/jobs?" + filters;
} else {
Expand Down
58 changes: 38 additions & 20 deletions hrms/www/jobs/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

def get_context(context):
context.parents = [{"name": _("My Account"), "route": "/"}]
applied_filters = frappe.request.args.to_dict(flat=False)
context.job_openings = get_job_openings(applied_filters)
context.filters = get_all_filters(applied_filters)
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)
context.all_filters = get_all_filters(filters)


def get_job_openings(applied_filters=None, limit_start=0, limit_page_length=20, order_by=None):
def get_job_openings(filters=None, txt=None, order_by=None, limit_start=0, limit_page_length=20):

jo = frappe.qb.DocType("Job Opening")
ja = frappe.qb.DocType("Job Applicant")
Expand Down Expand Up @@ -42,35 +43,52 @@ def get_job_openings(applied_filters=None, limit_start=0, limit_page_length=20,
.groupby(jo.name)
)

if "query" in applied_filters:
search = f"%{applied_filters['query'][0]}%"
query = query.where((jo.job_title.like(search)) | (jo.description.like(search)))
del applied_filters["query"]
for d in filters:
query = query.where(frappe.qb.Field(d).isin(filters[d]))

if txt:
query = query.where((jo.job_title.like(f"%{txt}%")) | (jo.description.like(f"%{txt}%")))

for d in applied_filters:
query = query.where(frappe.qb.Field(d).isin(applied_filters[d]))
if order_by:
print(order_by)

return query.run(as_dict=True)


def get_all_filters(applied_filters=None):
def get_all_filters(filters=None):
job_openings = frappe.get_all(
"Job Opening",
fields=["company", "department", "location", "employment_type"],
)

companies = applied_filters["company"] if "company" in applied_filters else None
companies = filters["company"] if "company" in filters else None

filters = {}
all_filters = {}
for d in job_openings:
for key, value in d.items():
if key == "company" or not companies or (companies and d["company"] in companies):
if key not in filters:
filters[key] = [value]
elif value and value not in filters[key]:
filters[key].append(value)
if key not in all_filters:
all_filters[key] = [value]
elif value and value not in all_filters[key]:
all_filters[key].append(value)

for d in filters:
filters[d].sort()
for d in all_filters:
all_filters[d].sort()

return filters
return all_filters


def get_filters_txt_and_order_by(args):
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]
elif d == "query":
txt = args["query"][0]
elif d == "sort":
if args["sort"][0] in ["Oldest Post", "Earliest Closing", "Latest Closing"]:
order_by = args["sort"][0]
return filters, txt, order_by

0 comments on commit a1624e7

Please sign in to comment.