diff --git a/src/todoist/label.ts b/src/todoist/label.ts index 6e77d5c2..6c7dc7ce 100644 --- a/src/todoist/label.ts +++ b/src/todoist/label.ts @@ -1,7 +1,7 @@ import { AlfredError } from '@/workflow/error' import compose from 'stampit' -import { List } from '../workflow/workflow' +import { Item, List } from '../workflow/workflow' export interface Label { [index: string]: string | number @@ -31,5 +31,18 @@ export const Label = compose({ export const LabelList = compose( List, - {} + { + init(this: List, { labels = [], query }: { labels: Label[]; query: string }) { + labels.forEach((label: Label) => { + this.items.push( + Item({ + title: label.name, + subtitle: `Add label ${label.name} to task`, + autocomplete: `${query.replace(/(^.*@).*/, '$1')}${label.name} `, + valid: false + }) + ) + }) + } + } ) diff --git a/src/todoist/parser.ts b/src/todoist/parser.ts index fe845092..fb154b9d 100644 --- a/src/todoist/parser.ts +++ b/src/todoist/parser.ts @@ -52,7 +52,9 @@ function organize([results]: Token[][]) { return { content: this.content, priority: +(this.priority || 1), - due_string: this.due_string + due_string: this.due_string, + project: `${this.project}`, + labels: (this.labels || []).map(label => `${label}`) } } }) diff --git a/src/todoist/project.ts b/src/todoist/project.ts index 7d747781..39709831 100644 --- a/src/todoist/project.ts +++ b/src/todoist/project.ts @@ -1,7 +1,7 @@ import { AlfredError } from '@/workflow/error' import compose from 'stampit' -import { List } from '../workflow/workflow' +import { Item, List } from '../workflow/workflow' // @ts-ignore: no declaration for v4 yet export interface Project { @@ -26,5 +26,20 @@ export const Project = compose({ export const ProjectList = compose( List, - {} + { + init(this: List, { projects = [], query }: { projects: Project[]; query: string }) { + projects.forEach((project: Project) => { + let name = project.name.indexOf(' ') !== -1 ? `[${project.name}]` : project.name + + this.items.push( + Item({ + title: project.name, + subtitle: `Move task to ${project.name}`, + autocomplete: `${query.replace(/(^.*#).*/, '$1')}${name} `, + valid: false + }) + ) + }) + } + } ) diff --git a/src/todoist/query.ts b/src/todoist/query.ts index 234586e5..487968ba 100644 --- a/src/todoist/query.ts +++ b/src/todoist/query.ts @@ -1,20 +1,23 @@ +import { LabelAdapter, ProjectAdapter } from '@/todoist/rest-api-v8' +import { getSettings } from '@/workflow/settings' import compose from 'stampit' import { Item, List } from '../workflow/workflow' +import { LabelList } from './label' import parser from './parser' +import { ProjectList } from './project' import { Task, TaskList } from './task' -// import { LabelList } from './label'; -// import { ProjectList } from './project'; interface ParsedTask {} export function init(query: string, locale: string) { let parsed = parser(query) - // if (parsed.last === 'project') { - // } else if (parsed.last === 'label') { - // } else - if (parsed.last().type === 'priority') { + if (parsed.last().type === 'project') { + return showProjects(query) + } else if (parsed.last().type === 'label') { + return showLabels(query) + } else if (parsed.last().type === 'priority') { return showPriorities(query) } @@ -28,19 +31,22 @@ function createTask(parsed: ParsedTask, locale: string) { return taskList.write() } -// function showProjects() { -// let projectList = ProjectList() +async function showProjects(query: string) { + let project = query.replace(/^.*#/, '').replace(/\[|\]/g, '') + let projects = await ProjectAdapter({ token: getSettings().token }).query(project, 'name') -// return projectList.write() -// } + return ProjectList({ projects, query }).write() +} -// function showLabels() { -// let labelList = LabelList() +async function showLabels(query: string) { + let label = query.replace(/^.*@/, '') + let labels = await LabelAdapter({ token: getSettings().token }).query(label, 'name') -// return labelList.write() -// } + return LabelList({ labels, query }).write() +} function showPriorities(query: string) { + let priority = query.replace(/^.*?!!/, '').replace(/^.*?p/, '') let priorityNames: { [index: string]: string } = { '1': 'urgent', '2': 'high', @@ -53,12 +59,19 @@ function showPriorities(query: string) { init(this: Item, title: number) { this.title = `${title}` this.subtitle = `Set priority to ${priorityNames[`${title}`]}` - this.autocomplete = `${query.replace(/!![1-4]/, '!!')}${title} ` + this.autocomplete = `${query + .replace(/(^.*!!)[1-4]$/, '$1') + .replace(/(^.*?p)[1-4]$/, '$1')}${title} ` this.valid = false } } ) - let priorityList = List({ items: [Priority(1), Priority(2), Priority(3), Priority(4)] }) - return priorityList.write() + if (+priority >= 1 && +priority <= 4) { + return List({ items: [Priority(+priority)] }).write() + } + + return List({ + items: [Priority(1), Priority(2), Priority(3), Priority(4)] + }).write() } diff --git a/test/integration.spec.ts b/test/integration.spec.ts index 54abdef8..ca5c8ddf 100644 --- a/test/integration.spec.ts +++ b/test/integration.spec.ts @@ -45,14 +45,16 @@ describe('Integration:', () => { describe('Create task', () => { it('should return a parsed JSON response', () => { - let query = 'Get milk @15min @at_home #Stuff p1, tomorrow' + let query = 'Get milk @15min @at_home #Work p1, tomorrow' TodoistWorkflow().create(query) let items = JSON.parse(spy.mock.calls[0][0]).items expect(JSON.parse(items[0].arg)).toEqual({ content: 'Get milk', due_string: 'tomorrow', - priority: 4 + labels: ['15min', 'at_home'], + priority: 4, + project: 'Work' }) }) })