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;