Skip to content

Commit

Permalink
feat: adds ability to add notes to tasks (#31)
Browse files Browse the repository at this point in the history
Adds new command `addNote` and the alias `addNotes` which will accept
input of a title and body or just a title or prompt for the title and
body of the note.

Fixes #23
  • Loading branch information
beauraines authored Mar 7, 2023
1 parent edb6d74 commit f262ec6
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions src/cmd/addNote.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
'use strict';

const log = require('../utils/log.js');
const config = require('../utils/config.js');
const { prompt } = require('../utils/prompt.js');

const finish = require('../utils/finish.js');


/**
* This command displays a task url
* @param args index
* @param env
*/
function action(args, env) {

// Prompt for task
if ( args.length < 1 ) {
prompt('Task:', 'Title:', 'Body:', _promptFinished);
} else if (args.length = 2 ) {
_process(args[0],'',args[1])
}

// Use provided args
else {
_process(args[0], args[1], args[2]);
}

}


/**
* Process the returned answers
* @private
*/
function _promptFinished(answers) {
for ( let i = 0; i < answers.length; i++ ) {
let answer = answers[i];
_process(answer[0], answer[1], answer[2],i+1, answers.length);
}
}


/**
* Process the request
* @private
*/
function _process(index, title, body, count=1, max=1) {

// Display info
log.spinner.start("Updating Task(s)...");

// Get User
config.user(function(user) {

// Parse arguments
index = parseInt(index.trim());

// Add Notes
user.tasks.addNotes(index, title, body, function(err) {
if ( err ) {
log.spinner.error("Could not add notes for Task #" + index + " (" + err.msg + ")");
}
_processFinished(count, max);
});

});

}

/**
* Request Callback
* @private
*/
function _processFinished(count, max) {
log.spinner.start("Updating Task [" + count + "/" + max + "]...");

// All tasks returned...
if ( count === max ) {
log.spinner.stop();
return finish();
}


}


module.exports = {
command: 'addNote [index] [title] [body]',
alias:'addNotes',
description: 'Add note or prompt for the title and body of the note. If only an index and text are included the text will be the body of the note without a title',
action: action
};

0 comments on commit f262ec6

Please sign in to comment.