From 8bf3f5d0756eff0bb512c98560afb275412a5511 Mon Sep 17 00:00:00 2001 From: Allen Lee Date: Thu, 24 Oct 2024 09:49:45 -0700 Subject: [PATCH] fix: adjust sort by ordering logic selection 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" | ``` --- django/library/views.py | 10 +++------- frontend/src/apps/codebase_list.ts | 9 +++++---- 2 files changed, 8 insertions(+), 11 deletions(-) diff --git a/django/library/views.py b/django/library/views.py index 8d43942f0..3b510b163 100644 --- a/django/library/views.py +++ b/django/library/views.py @@ -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) diff --git a/frontend/src/apps/codebase_list.ts b/frontend/src/apps/codebase_list.ts index 1e33c919a..bae8a47ea 100644 --- a/frontend/src/apps/codebase_list.ts +++ b/frontend/src/apps/codebase_list.ts @@ -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"; @@ -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");