<file_sep>const inquirer = require("inquirer");
require("colors");
const questions = [
  {
    type: "list",
    name: "option",
    message: "What would you like to do?",
    choices: [
      { value: "1", name: `${"1".green}. Create task` },
      { value: "2", name: `${"2".green}. List tasks` },
      { value: "3", name: `${"3".green}. List completed tasks` },
      { value: "4", name: `${"4".green}. List uncompleted tasks ` },
      { value: "5", name: `${"5".green}. Complete task(s) ` },
      { value: "6", name: `${"6".green}. Delete task ` },
      { value: "0", name: `${"0".green}. Exit` },
    ],
  },
];

const inquirerMenu = async () => {
  console.clear();
  console.log("============================================".green);
  console.log("	  Selected some option     ".white);
  console.log("============================================\n".green);
  const { option } = await inquirer.prompt(questions);
  return option;
};

const pause = async () => {
  const question = [
    {
      type: "input",
      name: "Enter",
      message: `Press ${"enter".green} to continue`,
    },
  ];
  console.log("\n");
  await inquirer.prompt(question);
};

const readInput = async (message) => {
  const question = [
    {
      type: "input",
      name: "desc",
      message,
      validate(value) {
        if (value.length === 0) {
          return "Please type some value";
        }
        return true;
      },
    },
  ];

  const { desc } = await inquirer.prompt(question);
  return desc;
};

const deleteTasksList = async (tasks = []) => {
  const choices = tasks.map((task, i) => {
    const idx = `${i + 1}.`.green;
    return {
      value: task.id,
      name: `${idx} ${task.desc}`,
    };
  });
  choices.unshift({ value: "0", name: "0.".green + " Cancel" });
  const questions = [{ type: "list", name: "id", message: "Delete", choices }];
  const { id } = await inquirer.prompt(questions);
  return id;
};

const confirm = async (message) => {
  const question = [{ type: "confirm", name: "ok", message }];
  const { ok } = await inquirer.prompt(question);
  return ok;
};
const completeTaskCheckList = async (tasks = []) => {
  const choices = tasks.map((task, i) => {
    const idx = `${i + 1}.`.green;
    return {
      value: task.id,
      name: `${idx} ${task.desc}`,
      checked: task.completedOn ? true : false,
    };
  });
  const question = [
    { type: "checkbox", name: "ids", message: "Select", choices },
  ];
  const { ids } = await inquirer.prompt(question);
  return ids;
};
module.exports = {
  inquirerMenu,
  pause,
  readInput,
  deleteTasksList,
  confirm,
  completeTaskCheckList,
};
<file_sep># tasks-node
Project to learn about console interaction with node.

## Quick start 
Install [nodejs](https://nodejs.org/en/download/)

Clone the and open the project:
```sh
git clone https://github.com/jela3105/tasks-node.git
cd tasks-node
```

Run the project with the command:
```sh
node index.js
```
<file_sep>require("colors");

const { readData, saveData } = require("./helpers/saveFile");
const {
  confirm,
  inquirerMenu,
  pause,
  readInput,
  deleteTasksList,
  completeTaskCheckList,
} = require("./helpers/inquirer");
const Tasks = require("./models/tasks");

const main = async () => {
  console.clear();
  let opt = "";
  const tasks = new Tasks();
  const tasksData = readData();
  if (tasksData) {
    tasks.loadTasksFromArray(tasksData);
  }
  do {
    opt = await inquirerMenu();
    switch (opt) {
      case "1":
        const desc = await readInput("Description: ");
        tasks.createTask(desc);
        break;
      case "2":
        tasks.showAllList();
        break;
      case "3":
        tasks.showTasksByCompleted();
        break;
      case "4":
        tasks.showTasksByCompleted(false);
        break;
      case "5":
        const ids = await completeTaskCheckList(tasks.arrList);
        tasks.toggleCompleted(ids);
        break;
      case "6":
        const id = await deleteTasksList(tasks.arrList);
        if (id !== "0") {
          const ok = await confirm("Are you sure?");
          if (ok) {
            tasks.deleteTask(id);
            console.log("Task deleted succesfully");
          }
        }

        break;
    }

    saveData(tasks.arrList);
    await pause();
  } while (opt !== "0");
};

main();
