Skip to content

Commit

Permalink
fix: if query is empty, Relevance ordering option in UI should no…
Browse files Browse the repository at this point in the history
…t be visible for /codebases/
  • Loading branch information
asuworks committed Oct 22, 2024
1 parent 971d6ab commit fdfacaa
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
5 changes: 1 addition & 4 deletions django/core/jinja_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,7 @@ def generate_search_form_inputs(query_params):
Returns:
list: A list of tuples representing the hidden input fields.
"""
# set default ordering to relevance, if it is not specified
search_parameters = {
"ordering": "relevance",
}
search_parameters = {}
if query_params:
# parse_qsl handles splitting and unquoting key-value pairs and generates a list of tuples
# do not include the actual query in the query parameters
Expand Down
9 changes: 5 additions & 4 deletions django/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,12 +399,13 @@ def filter_queryset(self, request, queryset, view):
criteria.update(id__in=codebases.values_list("id", flat=True))
# or we could include the PL in the query
# qs += " ".join(programming_languages)

default_ordering = "-first_published_at"
if ordering:
criteria.update(ordering=ordering)
criteria.update(ordering=ordering if qs else default_ordering)
else:
if qs:
# set default ordering for search when ordering is not specified
criteria.update(ordering="relevance")
# set default ordering for search when ordering is not specified
criteria.update(ordering="relevance" if qs else default_ordering)

return get_search_queryset(qs, queryset, tags=tags, criteria=criteria)

Expand Down
24 changes: 16 additions & 8 deletions frontend/src/apps/codebase_list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,19 @@ import { extractDataParams } from "@/util";
const props = extractDataParams("sidebar", ["languageFacets"]);
createApp(CodebaseListSidebar, props).mount("#sidebar");

createApp(SortBy, {
sortOptions: [
{ value: "relevance", label: "Relevance" },
{ value: "-first_published_at", label: "Publish date: newest" },
{ value: "first_published_at", label: "Publish date: oldest" },
{ value: "-last_modified", label: "Recently Modified" },
],
}).mount("#sortby");
// Function to check if 'query' exists and is not empty
function hasQueryParam(param: string) {
const params = new URLSearchParams(window.location.search);
const value = params.get(param);
return value !== null && value.trim() !== "";
}

// Conditionally include the 'relevance' sorting option
const sortOptions = [
...(hasQueryParam("query") ? [{ value: "relevance", label: "Relevance" }] : []),

This comment has been minimized.

Copy link
@alee

alee Oct 22, 2024

Member

little too cutesy, I think I prefer having a basic conditional to insert the relevance option if hasQueryParam("query")

{ value: "-first_published_at", label: "Publish date: newest" },
{ value: "first_published_at", label: "Publish date: oldest" },
{ value: "-last_modified", label: "Recently Modified" },
];

createApp(SortBy, { sortOptions }).mount("#sortby");
2 changes: 1 addition & 1 deletion frontend/src/components/CodebaseListSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ const initialFilterValues = {
onMounted(() => {
if (props.languageFacets) {
const localLanguageFacets = { ...props.languageFacets };
console.log(localLanguageFacets);
// console.log(localLanguageFacets);
parsedLanguageFacets = Object.entries(localLanguageFacets)
.sort(([, valueA], [, valueB]) => valueB - valueA) // Sort by value in descending order
.map(([name, value]) => ({ value: name, label: `${name} (${value})` }));
Expand Down

0 comments on commit fdfacaa

Please sign in to comment.