From e20c01dedbe37ca574bc5d29987e3c0aee3334db Mon Sep 17 00:00:00 2001 From: Abbey Campbell Date: Wed, 27 May 2026 16:26:30 -0700 Subject: [PATCH] add new shared observationsSort helper + option subset exports --- src/sharedHelpers/observationsSort.ts | 29 +++++++++++++++++++++++++++ src/types/sorting.ts | 13 ++++++------ 2 files changed, 35 insertions(+), 7 deletions(-) create mode 100644 src/sharedHelpers/observationsSort.ts diff --git a/src/sharedHelpers/observationsSort.ts b/src/sharedHelpers/observationsSort.ts new file mode 100644 index 000000000..f793bbfaf --- /dev/null +++ b/src/sharedHelpers/observationsSort.ts @@ -0,0 +1,29 @@ +import type { ObservationSortAPIParams } from "types/sorting"; +import { OBSERVATIONS_SORT } from "types/sorting"; + +const SORT_TO_API_PARAMS: Record = { + [OBSERVATIONS_SORT.DATE_UPLOADED_NEWEST]: { order_by: "created_at", order: "desc" }, + [OBSERVATIONS_SORT.DATE_UPLOADED_OLDEST]: { order_by: "created_at", order: "asc" }, + [OBSERVATIONS_SORT.DATE_OBSERVED_NEWEST]: { order_by: "observed_on", order: "desc" }, + [OBSERVATIONS_SORT.DATE_OBSERVED_OLDEST]: { order_by: "observed_on", order: "asc" }, + [OBSERVATIONS_SORT.MOST_FAVED]: { order_by: "votes", order: "desc" }, +}; + +export const EXPLORE_OBSERVATIONS_SORT_OPTIONS: OBSERVATIONS_SORT[] = [ + OBSERVATIONS_SORT.DATE_UPLOADED_NEWEST, + OBSERVATIONS_SORT.DATE_UPLOADED_OLDEST, + OBSERVATIONS_SORT.DATE_OBSERVED_NEWEST, + OBSERVATIONS_SORT.DATE_OBSERVED_OLDEST, + OBSERVATIONS_SORT.MOST_FAVED, +]; + +export const MY_OBSERVATIONS_SORT_OPTIONS: OBSERVATIONS_SORT[] = [ + OBSERVATIONS_SORT.DATE_UPLOADED_NEWEST, + OBSERVATIONS_SORT.DATE_UPLOADED_OLDEST, + OBSERVATIONS_SORT.DATE_OBSERVED_NEWEST, + OBSERVATIONS_SORT.DATE_OBSERVED_OLDEST, +]; + +export function sortToApiParams( sort: OBSERVATIONS_SORT ): ObservationSortAPIParams { + return SORT_TO_API_PARAMS[sort]; +} diff --git a/src/types/sorting.ts b/src/types/sorting.ts index fa94556d7..37e52c4d0 100644 --- a/src/types/sorting.ts +++ b/src/types/sorting.ts @@ -3,6 +3,12 @@ import type { RealmTaxon } from "realmModels/types"; export type SortItemType = "observations" | "taxa"; export type SortDirection = "asc" | "desc"; +export type ObservationOrderBy = "created_at" | "observed_on" | "votes"; + +export interface ObservationSortAPIParams { + order_by: ObservationOrderBy; + order: SortDirection; +} export enum OBSERVATIONS_SORT { DATE_UPLOADED_NEWEST = "DATE_UPLOADED_NEWEST", @@ -15,13 +21,6 @@ export enum OBSERVATIONS_SORT { // eventually this will be a union type that accounts for different types of sorting, i.e. name export type SpeciesSortBy = "count" -export interface ObservationSort { - by: - | "created_at" - | "observed_on"; - direction?: SortDirection; -} - export interface SpeciesSort { by: SpeciesSortBy; direction?: SortDirection;