diff --git a/scripts/processCommand.js b/scripts/processCommand.js index 16c6846..f09f446 100644 --- a/scripts/processCommand.js +++ b/scripts/processCommand.js @@ -147,20 +147,6 @@ async function processCommand(user, command, message, flags, extra) { params.newTask = `${openQuote}${nextRequest.body.newTask}${closeQuote}`; respond(nextResponse, params); - } else if (getCommand("nowTaskCommands").includes(command)) { - let nowRequest = await nowTask(user, extra.userColor, message); - - if (nowRequest.status !== 200) { - respond(nowRequest.body.error, params); - return; - } - - // task now - let nowResponse = getResponse("nowTask"); - - params.task = `${openQuote}${nowRequest.body.task}${closeQuote}`; - - await respond(nowResponse, params); } else if (getCommand("logTaskCommands").includes(command)) { let logRequest = await logTask(user, extra.userColor, message); @@ -179,8 +165,11 @@ async function processCommand(user, command, message, flags, extra) { } await respond(loggedResponse, params); - } else if (getCommand("focusTaskCommands").includes(command)) { - let focusRequest = await focusTask(user, message); + } else if ( + getCommand("focusTaskCommands").includes(command) || + getCommand("nowTaskCommands").includes(command) + ) { + let focusRequest = await focusTask(user, extra.userColor, message); if (focusRequest.status !== 200) { respond(focusRequest.body.error, params); @@ -188,11 +177,11 @@ async function processCommand(user, command, message, flags, extra) { } // task focused - let focusedResponse = getResponse("taskFocused"); + let focusedResponse = getResponse("nowTask"); params.task = `${openQuote}${focusRequest.body.focusedTask}${closeQuote}`; - await respond(focusedResponse, params); + respond(focusedResponse, params); } else if (getCommand("focusedTaskCommands").includes(command)) { let focusedRequest = await focusedTask(user); diff --git a/scripts/taskList.js b/scripts/taskList.js index 7820b02..3e788e0 100644 --- a/scripts/taskList.js +++ b/scripts/taskList.js @@ -970,33 +970,8 @@ function createErrorResponse(errorMessage, errorType) { * @returns {Object} An object with a status and body. The status is 200 if successful, otherwise it indicates the error. The body contains the task text or an error message. */ async function nowTask(username, userColor, task) { - // if task is integer, return error - let addTaskFunction = await _addTask(username, userColor, task, true); - - let addedTaskIndices = addTaskFunction.body.addedTaskIndices; - let addedTaskData = addedTaskIndices[0]; - - if (addTaskFunction.status !== 200) { - return addTaskFunction; - } - - // focus on it - let focusTaskFunction = await focusTask(username, addedTaskData.task); - - if (focusTaskFunction.status !== 200) { - return focusTaskFunction; - } - - if (!scrolling) { - await renderTaskListToDOM(); - } - - return { - status: 200, - body: { - task: `(${addedTaskData.index + 1}) ${addedTaskData.task}`, - }, - }; + // nowtask is now the same as focus task + return focusTask(username, userColor, task); } /** @@ -1006,23 +981,18 @@ async function nowTask(username, userColor, task) { * @param {string} task - The task to be focused. * @returns {Object} An object with a status and body. The status is 200 if the task is successfully focused, and the body contains the focused task. If the task is not focused, the status indicates the error and the body contains an error message. */ -async function focusTask(username, task) { - const tasks = await DBHandler.get("tasks"); +async function focusTask(username, userColor, task) { + let tasks = await DBHandler.get("tasks"); if (!tasks[username] || tasks[username].todos.length === 0) { - return createErrorResponse( - `@${username} has no tasks`, - getResponse("noTask") - ); + tasks[username] = { + todos: [], + done: [], + userColor: userColor, + }; } const incompleteTasks = tasks[username].todos.filter((t) => !t.done); - if (incompleteTasks.length === 0) { - return createErrorResponse( - `@${username} has no incomplete tasks`, - getResponse("noTask") - ); - } if (taskSeparator.some((separator) => task.includes(separator))) { return createErrorResponse( @@ -1038,13 +1008,18 @@ async function focusTask(username, task) { if (index === -1) { if (isInt(task)) { index = parseInt(task) - 1; // ACTUAL INDEX + } else if (task !== "") { + // add it as a focused task + let addTaskFunction = await _addTask(username, userColor, task); + + if (addTaskFunction.status !== 200) { + return addTaskFunction; + } + + index = addTaskFunction.body.addedTaskIndices[0].index; + tasks = await DBHandler.get("tasks"); } else if (incompleteTasks.length === 1) { index = tasks[username].todos.findIndex((t) => !t.done); - } else { - return createErrorResponse( - `@${username} invalid input`, - getResponse("specifyFocusTask") - ); } if (index < 0 || index > tasks[username].todos.length - 1) { @@ -1097,7 +1072,7 @@ async function focusTask(username, task) { return { status: 200, body: { - focusedTask: focusedTask, + focusedTask: `(${index + 1}) ${focusedTask}`, }, }; }