-
-
Notifications
You must be signed in to change notification settings - Fork 88
feat: add support for viewing completed tasks #391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import type { DueDate } from "@/api/domain/dueDate"; | ||
| import type { Priority } from "@/api/domain/task"; | ||
|
|
||
| export type CompletedTask = { | ||
| id: string; | ||
| task_id: string; | ||
| project_id: string; | ||
| section_id: string | null; | ||
| content: string; | ||
| completed_at: string; | ||
| note_count: number; | ||
| meta_data: string | null; | ||
| item_object: { | ||
| due?: DueDate | null; | ||
| description: string; | ||
| priority: Priority; | ||
| labels: string[]; | ||
| }; | ||
| }; | ||
|
|
||
| export type CompletedTasksResponse = { | ||
| items: CompletedTask[]; | ||
| }; | ||
|
|
||
| export type GetCompletedTasksParams = { | ||
| project_id?: string; | ||
| limit?: number; | ||
| until?: Date; | ||
| since?: Date; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,13 @@ | ||
| import { type TodoistApiClient, TodoistApiError } from "@/api"; | ||
| import type { CompletedTask, GetCompletedTasksParams } from "@/api/domain/completedTask"; | ||
| import type { Label, LabelId } from "@/api/domain/label"; | ||
| import type { Project, ProjectId } from "@/api/domain/project"; | ||
| import type { Section, SectionId } from "@/api/domain/section"; | ||
| import type { Task as ApiTask, CreateTaskParams, TaskId } from "@/api/domain/task"; | ||
| import { Repository, type RepositoryReader } from "@/data/repository"; | ||
| import { SubscriptionManager, type UnsubscribeCallback } from "@/data/subscriptions"; | ||
| import type { Task } from "@/data/task"; | ||
| import type { Query } from "@/query/query"; | ||
| import { Maybe } from "@/utils/maybe"; | ||
|
|
||
| export enum QueryErrorKind { | ||
|
|
@@ -93,30 +95,41 @@ export class TodoistAdapter { | |
| }; | ||
| } | ||
|
|
||
| public subscribe(query: string, callback: OnSubscriptionChange): [UnsubscribeCallback, Refresh] { | ||
| public subscribe(query: Query, callback: OnSubscriptionChange): [UnsubscribeCallback, Refresh] { | ||
| const fetcher = this.buildQueryFetcher(query); | ||
| const subscription = new Subscription(callback, fetcher, () => true); | ||
| return [this.subscriptions.subscribe(subscription), subscription.update]; | ||
| } | ||
|
|
||
| private buildQueryFetcher(query: string): SubscriptionFetcher { | ||
| private buildQueryFetcher(query: Query): SubscriptionFetcher { | ||
| return async () => { | ||
| if (!this.api.hasValue()) { | ||
| return undefined; | ||
| } | ||
| const data = await this.api.withInner((api) => api.getTasks(query)); | ||
| const hydrated = data.map((t) => this.hydrate(t)); | ||
| return hydrated; | ||
|
|
||
| let result: Task[]; | ||
| if (query.viewCompleted) { | ||
| const params: GetCompletedTasksParams = { | ||
| limit: query.completedLimit, | ||
| since: query.completedSince, | ||
| until: query.completedUntil, | ||
| }; | ||
|
|
||
| const data = await this.api.withInner((api) => api.getCompletedTasks(params)); | ||
| result = data.map((t) => this.hydrateCompletedTask(t)); | ||
| } else { | ||
| const data = await this.api.withInner((api) => api.getTasks(query.filter)); | ||
| result = data.map((t) => this.hydrate(t)); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
| } | ||
|
|
||
| private hydrate(apiTask: ApiTask): Task { | ||
| const project = this.projects.byId(apiTask.projectId); | ||
| const section = apiTask.sectionId | ||
| ? (this.sections.byId(apiTask.sectionId) ?? makeUnknownSection(apiTask.sectionId)) | ||
| : undefined; | ||
|
|
||
| const labels = apiTask.labels.map((id) => this.labels.byName(id) ?? makeUnknownLabel()); | ||
| const project = this.getProjectById(apiTask.projectId); | ||
| const section = this.getSectionById(apiTask.sectionId); | ||
| const labels = this.getLabelsByIds(apiTask.labels); | ||
|
|
||
| return { | ||
| id: apiTask.id, | ||
|
|
@@ -137,6 +150,37 @@ export class TodoistAdapter { | |
| }; | ||
| } | ||
|
|
||
| private hydrateCompletedTask(apiCompletedTask: CompletedTask): Task { | ||
| const project = this.getProjectById(apiCompletedTask.project_id); | ||
| const section = this.getSectionById(apiCompletedTask.section_id); | ||
| const labels = this.getLabelsByIds(apiCompletedTask.item_object.labels); | ||
|
|
||
| return { | ||
| id: apiCompletedTask.task_id, | ||
| createdAt: apiCompletedTask.completed_at, | ||
| content: apiCompletedTask.content, | ||
| description: apiCompletedTask.item_object.description, | ||
| priority: apiCompletedTask.item_object.priority, | ||
| due: apiCompletedTask.item_object.due ?? undefined, | ||
|
|
||
| project: project ?? makeUnknownProject(apiCompletedTask.project_id), | ||
| section: section, | ||
| labels: labels, | ||
| }; | ||
| } | ||
|
|
||
| private getProjectById(projectId: string): Project | undefined { | ||
| return this.projects.byId(projectId); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we include the |
||
| } | ||
|
|
||
| private getSectionById(sectionId: string | null): Section | undefined { | ||
| return sectionId ? (this.sections.byId(sectionId) ?? makeUnknownSection(sectionId)) : undefined; | ||
| } | ||
|
|
||
| private getLabelsByIds(labels: string[]): Label[] { | ||
| return labels.map((id) => this.labels.byName(id) ?? makeUnknownLabel()); | ||
| } | ||
|
|
||
| private async closeTask(id: TaskId): Promise<void> { | ||
| this.tasksPendingClose.push(id); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,7 +6,7 @@ import type { Priority, TaskId } from "@/api/domain/task"; | |
|
|
||
| export type Task = { | ||
| id: TaskId; | ||
| createdAt: string; | ||
| createdAt?: string; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We are using the completed task's |
||
|
|
||
| content: string; | ||
| description: string; | ||
|
|
@@ -19,5 +19,5 @@ export type Task = { | |
| priority: Priority; | ||
|
|
||
| due?: DueDate; | ||
| order: number; | ||
| order?: number; | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we debug log the
RequestParamsthat gets passed intofetcher.fetchlike what happens indo?