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

Commit

Permalink
feat: allow created tasks to assign project and labels
Browse files Browse the repository at this point in the history
  • Loading branch information
moranje committed Oct 14, 2018
1 parent 40d821a commit 550b15b
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 51 deletions.
14 changes: 8 additions & 6 deletions src/todoist/parser.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as grammar from '@/todoist/grammar'
import { Task } from '@/todoist/task'
import nearley from 'nearley'

import * as grammar from './grammar'
import { Task } from './task'

interface Token {
type: string
value: string
Expand Down Expand Up @@ -52,9 +51,12 @@ function organize([results]: Token[][]) {
return {
content: this.content,
priority: +(this.priority || 1),
due_string: this.due_string,
project: `${this.project}`,
labels: (this.labels || []).map(label => `${label}`)
due_string: this.due_string || void 0,
project: this.project ? `${this.project}` : void 0,
project_id: this.project_id,
labels:
this.labels && this.labels.length > 0 ? this.labels.map(label => `${label}`) : void 0,
label_ids: (this.label_ids && this.label_ids.length > 0) || void 0
}
}
})
Expand Down
123 changes: 78 additions & 45 deletions src/workflow/todoist-workflow.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,97 @@
import { Label } from '@/todoist/label'
import { Project } from '@/todoist/project'
import { init } from '@/todoist/query'
import { LabelAdapter, ProjectAdapter, TaskAdapter } from '@/todoist/rest-api-v8'
import { Task, TaskList } from '@/todoist/task'
import { Notification } from '@/workflow/notifier'
import { edit, getSetting, getSettings, list, update } from '@/workflow/settings'
import { Item, List } from '@/workflow/workflow'
import omit from 'lodash.omit'
import compose from 'stampit'

import { init } from '../todoist/query'
import { TaskAdapter } from '../todoist/rest-api-v8'
import { Task, TaskList } from '../todoist/task'
import { edit, getSettings, list, update } from './settings'

interface Workflow {}

// import { Item } from './workflow';
async function replaceNamesWithIds(task: Task) {
let allProjects = await ProjectAdapter({ token: getSetting('token') }).findAll()
let allLabels = await LabelAdapter({ token: getSetting('token') }).findAll()

if (task && task.labels) {
Object.assign(task, { label_ids: [] }, task)

task.labels.forEach((label: Label) => {
let matchedLabel = allLabels.find((aLabel: Label) => aLabel.name === `${label}`)

// @ts-ignore: is properly checked as far a I can see
if (matchedLabel) task.label_ids.push(matchedLabel.id)
})
}

if (task && task.project) {
let matchedProject = allProjects.find((aProject: Project) => {
// @ts-ignore: object is possibly undefined
return aProject.name === task.project
})

if (matchedProject) task.project_id = matchedProject.id
}

return omit(task, ['labels', 'project'])
}

export const TodoistWorkflow = compose({
methods: {
read(this: Workflow, query: string) {
return TaskAdapter({ token: getSettings().token })
.query(query)
.then(async (tasks: any) => {
let taskList: TaskList = TaskList({
tasks,
locale: getSettings().language
})
async read(this: Workflow, query: string) {
let tasks = await TaskAdapter({ token: getSettings().token }).query(query)

if (tasks.length > 0) {
return TaskList({
tasks,
locale: getSetting('language')
}).write()
}

taskList.write({ items: taskList.items })
})
return List({
items: [
Item({
title: "SORRY: There's just nothing here...",
subtitle: "Don't let that get you down though, try something less specific",
valid: false
})
]
}).write()
},

create(this: Workflow, query: string) {
let locale = getSettings().language
return init(query, locale)
// @ts-ignore: don't know how to express this in typescript but this will
// be a string
return init(query, getSetting('language'))
},

submit(this: Workflow, task: Task) {
return TaskAdapter({ token: getSettings().token })
.create(task)
.then(({ statusCode, body }: any) => {
if (statusCode === 200) {
return Notification({
message: 'Task added',
timeout: void 0,
open: `https://todoist.com/showTask?id=${body.id}`
}).write()
}

return Notification({
message: 'Task probably added',
timeout: void 0,
open: `https://todoist.com/showTask?id=${body.id}`
}).write()
})
async submit(this: Workflow, task: Task) {
let apiTask = await replaceNamesWithIds(task)
let { statusCode, body } = await TaskAdapter({ token: getSetting('token') }).create(apiTask)

if (statusCode === 200) {
return Notification({
message: 'Task added',
open: `https://todoist.com/showTask?id=${body.id}`
}).write()
}

return Notification({
message: 'Task probably added',
open: `https://todoist.com/showTask?id=${body.id}`
}).write()
},

remove(this: Workflow, task: Task) {
return TaskAdapter({ token: getSettings().token })
.remove(task.id)
.then(({ statusCode }: any) => {
if (statusCode === 204) {
return Notification({ message: 'Task completed' }).write()
}
async remove(this: Workflow, task: Task) {
let { statusCode } = await TaskAdapter({ token: getSetting('token') }).remove(task.id)

if (statusCode === 204) {
return Notification({ message: 'Task completed' }).write()
}

return Notification({ message: 'Task probably completed' }).write()
})
return Notification({ message: 'Task might not actually be completed' }).write()
},

settings(this: Workflow) {
Expand Down

0 comments on commit 550b15b

Please sign in to comment.