Skip to content
This repository has been archived by the owner on Mar 13, 2023. It is now read-only.

Commit

Permalink
feat: add label and project suggestions on task creation
Browse files Browse the repository at this point in the history
  • Loading branch information
moranje committed Oct 13, 2018
1 parent 2577fc3 commit 9478347
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 24 deletions.
17 changes: 15 additions & 2 deletions src/todoist/label.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
})
)
})
}
}
)
4 changes: 3 additions & 1 deletion src/todoist/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
}
}
})
Expand Down
19 changes: 17 additions & 2 deletions src/todoist/project.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
})
)
})
}
}
)
47 changes: 30 additions & 17 deletions src/todoist/query.ts
Original file line number Diff line number Diff line change
@@ -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)
}

Expand All @@ -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',
Expand All @@ -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()
}
6 changes: 4 additions & 2 deletions test/integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
})
})
Expand Down

0 comments on commit 9478347

Please sign in to comment.