-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
220 lines (194 loc) · 5.99 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#! /usr/bin/env node
import inquirer from "inquirer";
import chalk from "chalk";
import chalkAnimation from "chalk-animation"
console.clear();
//------------------------------------------------------------------------------
//CLASSES/CONSTRUCTORS/OOPs HERE
//------------------------------------------------------------------------------
const defValue ={
Completed: 'Completed',
Pending: 'Pending'
}
interface ToDo {
Action: string;
Status: string;
}
//------------------------------------------------------------------------------
//FUNCTIONS HERE
//------------------------------------------------------------------------------
const stopTime = ()=>{
return new Promise((res:any)=>{
setTimeout(res,3500);
})
}
async function welcome() {
let rainbowTitle = chalkAnimation.neon("Welcome To ToDO App!\n\nCoded By Hosein Sirat Mohammad\n");
await stopTime();
rainbowTitle.stop();
}
async function mainMenu(){
var selectedOption:any = await inquirer.prompt([
{
type: "list",
name: "menuOption",
message:"What would you like to do?",
choices:[
{
name: 'Show all ToDo task',
value: 'showTask'
},
{
name: 'Add a new ToDo task',
value: 'addTask',
},
{
name: 'Complete the ToDo task',
value: 'completeTask'
},
{
name: 'Delete a ToDo task',
value: 'deleteTask'
},
{
name: 'Bye',
value: 'bye'
}
]
}
]);
if (selectedOption.menuOption == 'showTask')
{
await showTaskFunc();
mainMenu();
}
else if (selectedOption.menuOption == 'addTask')
{
await addTaskFunc();
mainMenu();
}
else if (selectedOption.menuOption == 'completeTask')
{
await completeTaskFunc();
mainMenu();
}
else if (selectedOption.menuOption == 'deleteTask')
{
await deleteTaskFunc();
mainMenu();
}
else
{
console.log(selectedOption.menuOption);
}
};
async function showTaskFunc(){
if (arrToDos.length > 0)
{
let i:number = 1;
arrToDos.forEach((value) =>
{
if(value.Status == defValue.Pending)
console.log( chalk.bgBlue('\t\tTask '+i) +' >>>>>>>>> '+ (chalk.bgBlue(value.Action)) + ' >>>>>>>>> '+ (chalk.bgBlue(value.Status)));
else
console.log( chalk.bgGreen('\t\tTask '+i) +' >>>>>>>>> '+ (chalk.bgGreen(value.Action)) + ' >>>>>>>>> '+ (chalk.bgGreen(value.Status)));
i++;
})
console.log('\n');
}
else
{
console.log(chalk.bgYellowBright('No task added.\n'));
}
};
async function addTaskFunc(){
await inquirer.prompt([
{
type: 'input',
name: 'addTask',
message:'Enter the ToDo task:',
validate(valid){
if(valid.length >1)
return true;
else
return chalk.bgRedBright('Must enter inout.');
}
}
])
.then((value) =>{
arrToDos.push({
Action: value.addTask,
Status: defValue.Pending
});
console.log(chalk.inverse('Task added successfully.\n'));
});
};
async function completeTaskFunc(){
if (arrToDos.filter((todo) => todo.Status==defValue.Pending).map((todo) => todo.Action).length > 0)
{
await inquirer.prompt([
{
type: 'list',
name: 'completed',
message:'Select the task to complete.',
choices: arrToDos.filter((todo) => todo.Status==defValue.Pending).map((todo) => todo.Action)
}
])
.then((answers)=>
{
const todo:any = arrToDos.find((todo) => todo.Action === answers.completed);
todo.Status = defValue.Completed;
console.log(todo);
//arrToDos[todo].Status = 'Completed';
console.log(chalk.bgGreenBright("Task completed successfully.\n"));
}
)
}
else
{
console.log(chalk.bgYellowBright('No task Pending.\n'));
}
};
async function deleteTaskFunc() {
if (arrToDos.filter((todo) => todo.Status==defValue.Pending).map((todo) => todo.Action).length > 0)
{
await inquirer.prompt([
{
type: 'list',
name: 'deleted',
message:'Select the task to delete',
choices: arrToDos.filter((todo) => todo.Status==defValue.Pending).map((todo) => todo.Action)
},
{
type: 'confirm',
name: 'confirm',
message:chalk.red('Do you want to continue delete?')
}
])
.then((answers)=>
{
if(answers.confirm == true)
{
// const todo:any = arrToDos.find((todo) => todo.Action === answers.deleted);
let delToDo:number = arrToDos.indexOf(answers.deleted);
arrToDos.splice(delToDo,1);
console.log(chalk.bgGreenBright("Task deleted successfully.\n"));
}
else
{
console.log(chalk.bgRed("Task iqnored.\n"));
}
}
)
}
else
{
console.log(chalk.bgYellowBright('No task Pending.\n'));
}
}
//------------------------------------------------------------------------------
//MAIN
//------------------------------------------------------------------------------
await welcome();
const arrToDos: ToDo[] = [];
await mainMenu();