Action

Append comment to Todoist task

Posted by ejheise, Last update over 4 years ago

Appends draft content as a comment to an existing Todoist

Steps

  • script

    //heavily borrowing from AgileTortoise's Import Project script
    //Draft appends as comment to an existing task in Todoist
    
    // utility function
    let mapIds = (arr, key, valKey) => {
    	let result = {};
    	arr.forEach(o => {
    		result[o[key]] = o[valKey];
    	})
    	return result;
    }
    
    let f = () => {
    	// create Todoist object and load projects
    	let todoist = Todoist.create();
    	let projects = todoist.getProjects();
    
    	if (!projects) { 
    		console.log("Unable to retreive data from Todoist.");
    		return false; 
    	}
    
    	// create prompt
    	let p = Prompt.create();
    	p.title = "Select Project";
    
    	// add projects
    	let projectLookup = mapIds(projects, "name", "id");
    	let projectNames = Object.keys(projectLookup);
    	p.addPicker("project", "Project", [projectNames], [0]);
    
    	p.addButton("Select");
    	if (!p.show()) {
    		context.cancel();
    		return true;
    	}
    
    	// read values from prompt
    	let projectIndex = p.fieldValues["project"][0];
    	let projectName = projectNames[projectIndex];
    	let projectID = projectLookup[projectName];
    
    	let tasks = todoist.getTasks({
    		"project_id": projectID
    	});
    	if (!tasks) { 
    		alert("No tasks in this project");
    		return false;
    	}
    
    	//create tasks prompt
    	let pTasks = Prompt.create();
    	pTasks.title = "Select Task";
    
    	//add tasks
    	let taskLookup = mapIds(tasks, "content", "id");
    	let taskNames = Object.keys(taskLookup);
    	pTasks.addPicker("task", "Task", [taskNames], [0]);
    
    	pTasks.addButton("Select");
    	if (!pTasks.show()) {
    		context.cancel();
    		return true;
    	}
    
    	//read values from task prompt
    	let taskIndex = pTasks.fieldValues["task"][0];
    	let taskName = taskNames[taskIndex];
    	let taskID = taskLookup[taskName];
    
    	//add comment
    	let comment = todoist.createComment({
    		"task_id": taskID,
    		"content": draft.content
    	});
    	console.log(`Comment added to ${taskName} in ${projectName}`);
    }
    
    if (!f()) {
    	context.fail();
    }

Options

  • After Success Default
    Notification Info
    Log Level Info
Items available in the Drafts Directory are uploaded by community members. Use appropriate caution reviewing downloaded items before use.