Skip to content

Commit

Permalink
Add ability to sort by date added (#280)
Browse files Browse the repository at this point in the history
* parse createdAt through the API response

* add dateAdded & dateAddedDescending sorting options

* implement sorting operation

* add changelog entry
  • Loading branch information
jamiebrynes7 authored Feb 1, 2024
1 parent 4b4ff22 commit 0e0c480
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- For example, if the full file path is `My Vault/Notes/How to Take Smart Notes.md` then the replaced file name will be `How to Take Smart Notes`.
- Added "Add item" button to rendered queries. This will open the task creation modal.
- Added the ability to control which elements of the task metadata render with tasks inside the query with the `show` keyword.
- You can now sort by when the task was added to Todoist using `dateAdded` and `dateAddedAscending` sort options.

### 🔁 Changes

Expand Down
2 changes: 1 addition & 1 deletion src/api/domain/task.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import moment from "moment";
import type { DueDate } from "./dueDate";
import type { ProjectId } from "./project";
import type { SectionId } from "./section";
Expand All @@ -7,6 +6,7 @@ export type TaskId = string;

export type Task = {
id: TaskId,
createdAt: string,

content: string,
description: string,
Expand Down
2 changes: 2 additions & 0 deletions src/data/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import type { Task } from "./task";
import type { Task as ApiTask, CreateTaskParams, TaskId } from "../api/domain/task";
import { SubscriptionManager, type UnsubscribeCallback } from "./subscriptions";
import { Maybe } from "../utils/maybe";
import moment from "moment";

type SubscriptionResult = { type: "success", tasks: Task[] } | { type: "error" };
type OnSubscriptionChange = (result: SubscriptionResult) => void;
Expand Down Expand Up @@ -100,6 +101,7 @@ export class TodoistAdapter {

return {
id: apiTask.id,
createdAt: apiTask.createdAt,

content: apiTask.content,
description: apiTask.description,
Expand Down
2 changes: 2 additions & 0 deletions src/data/task.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import type moment from "moment";
import type { DueDate } from "../api/domain/dueDate";
import type { Project } from "../api/domain/project";
import type { Section } from "../api/domain/section";
import type { Priority, TaskId } from "../api/domain/task";

export type Task = {
id: TaskId,
createdAt: string,

content: string,
description: string,
Expand Down
29 changes: 29 additions & 0 deletions src/data/transformations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { SortingVariant } from "../query/query";
function makeTask(id: string, opts?: Partial<Task>): Task {
return {
id,
createdAt: opts?.createdAt,
parentId: opts?.parentId,
content: "",
description: "",
Expand Down Expand Up @@ -284,6 +285,34 @@ describe("sortTasks", () => {
}),
],
},
{
description: "can sort by dateAdded",
input: [
makeTask("a", { createdAt: "2020-03-03T13:00:00" }),
makeTask("b", { createdAt: "2020-03-02T11:00:00" }),
makeTask("c", { createdAt: "2020-03-02T12:00:00" }),
],
sortingOpts: [SortingVariant.DateAdded],
expectedOutput: [
makeTask("b", { createdAt: "2020-03-02T11:00:00" }),
makeTask("c", { createdAt: "2020-03-02T12:00:00" }),
makeTask("a", { createdAt: "2020-03-03T13:00:00" }),
],
},
{
description: "can sort by dateAddedDescending",
input: [
makeTask("a", { createdAt: "2020-03-02T11:00:00" }),
makeTask("b", { createdAt: "2020-03-03T13:00:00" }),
makeTask("c", { createdAt: "2020-03-02T12:00:00" }),
],
sortingOpts: [SortingVariant.DateAddedDescending],
expectedOutput: [
makeTask("b", { createdAt: "2020-03-03T13:00:00" }),
makeTask("c", { createdAt: "2020-03-02T12:00:00" }),
makeTask("a", { createdAt: "2020-03-02T11:00:00" }),
],
},
{
description: "will sort using specified parameters in order",
input: [
Expand Down
16 changes: 16 additions & 0 deletions src/data/transformations.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import moment from "moment";
import { getDueDateInfo } from "../api/domain/dueDate";
import type { Project } from "../api/domain/project";
import type { TaskId } from "../api/domain/task";
Expand Down Expand Up @@ -64,6 +65,10 @@ function compareTask<T extends Task>(self: T, other: T, sorting: SortingVariant)
return -compareTaskDate(self, other);
case SortingVariant.Order:
return self.order - other.order;
case SortingVariant.DateAdded:
return compareTaskDateAdded(self, other);
case SortingVariant.DateAddedDescending:
return -compareTaskDateAdded(self, other);
default:
throw new Error(`Unexpected sorting type: '${sorting}'`)
}
Expand Down Expand Up @@ -106,6 +111,17 @@ function compareTaskDate<T extends Task>(self: T, other: T): number {
return selfDate.isBefore(otherDate) ? -1 : 1;
}

function compareTaskDateAdded<T extends Task>(self: T, other: T): number {
const selfDate = moment(self.createdAt);
const otherDate = moment(other.createdAt);

if (selfDate === otherDate) {
return 0;
}

return selfDate.isBefore(otherDate) ? -1 : 1;
}

export type TaskTree = Task & { children: TaskTree[] };

// Builds a task tree, preserving the sorting order.
Expand Down
3 changes: 3 additions & 0 deletions src/query/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ const sortingLookup: Record<string, SortingVariant> = {
"dateAscending": SortingVariant.Date,
"dateDescending": SortingVariant.DateDescending,
"order": SortingVariant.Order,
"dateAdded": SortingVariant.DateAdded,
"dateAddedAscending": SortingVariant.DateAdded,
"dateAddedDescending": SortingVariant.DateAddedDescending,
}

const showMetadataVariantLookup: Record<string, ShowMetadataVariant> = {
Expand Down
4 changes: 3 additions & 1 deletion src/query/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ export enum SortingVariant {
PriorityDescending,
Date,
DateDescending,
Order
Order,
DateAdded,
DateAddedDescending,
};

export enum ShowMetadataVariant {
Expand Down

0 comments on commit 0e0c480

Please sign in to comment.