Skip to content

Commit

Permalink
fix: adjust sort by ordering logic selection
Browse files Browse the repository at this point in the history
Desired behavior:

```
| ordering | qs    | default_ordering    | criteria['ordering']   |
|----------|-------|--------------------|------------------------|
| True     | True  | "relevance"        | ordering               |
| True     | False | "-first_published_at" | ordering               |
| False    | True  | "relevance"        | "relevance"            |
| False    | False | "-first_published_at" | "-first_published_at"  |
```
  • Loading branch information
alee committed Oct 24, 2024
1 parent b2b2a38 commit 8bf3f5d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
10 changes: 3 additions & 7 deletions django/library/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,13 +400,9 @@ def filter_queryset(self, request, queryset, view):
# or we could include the PL in the query
# qs += " ".join(programming_languages)

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

# default ordering is relevance if there's a query string, otherwise "-first_published_at"
default_ordering = "relevance" if qs else "-first_published_at"
criteria.update(ordering=ordering if ordering else default_ordering)
return get_search_queryset(qs, queryset, tags=tags, criteria=criteria)


Expand Down
9 changes: 5 additions & 4 deletions frontend/src/apps/codebase_list.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import "vite/modulepreload-polyfill"; // Ensure that this is needed based on your project setup

import { isEmpty } from "lodash";
import { createApp } from "vue";
import CodebaseListSidebar from "@/components/CodebaseListSidebar.vue";
import SortBy from "@/components/ListSortBy.vue";
Expand All @@ -12,16 +13,16 @@ createApp(CodebaseListSidebar, props).mount("#sidebar");
function hasQueryParam(param: string) {
const params = new URLSearchParams(window.location.search);
const value = params.get(param);
return value !== null && value.trim() !== "";
return !isEmpty(value);
}

const relevanceOption = hasQueryParam("query") ? [{ value: "relevance", label: "Relevance" }] : [];

const sortOptions = [
...relevanceOption,
{ value: "-first_published_at", label: "Publish date: newest" },
{ value: "first_published_at", label: "Publish date: oldest" },
{ value: "-last_modified", label: "Recently Modified" },
{ value: "-first_published_at", label: "Most recently published" },
{ value: "first_published_at", label: "Earliest published" },
{ value: "-last_modified", label: "Recently modified" },
];

createApp(SortBy, { sortOptions }).mount("#sortby");

0 comments on commit 8bf3f5d

Please sign in to comment.