I am building an employee tracking app. I want my app to work so that after each one of the functions for the various list selections is complete, the app will re-prompt you with the options list again. There are a few methods I can think of to achieve this but I was hoping a more seasoned developer could recommend which method is the best. Thanks!
- make all the functions in the switch statements into promises and then make a promise chain that recalls itself
- try and use async-await instead of a promise chain???
- export the prompt() function and import it and all the other corresponding functions in each lib file
here is the entry point of the app
JavaScript
x
92
92
1
const db = require('./db/connection');
2
const inquirer = require('inquirer');
3
4
// response handling
5
const { viewDepartments, viewRoles, viewEmployees, viewByManager, viewByDepartment } = require('./lib/viewFuncs');
6
const { addDepartment, addRole, addEmployee } = require('./lib/addFuncs');
7
const { updateRole, updateManager } = require('./lib/updateFuncs');
8
const { deleteDepartment, deleteRole, deleteEmployee } = require('./lib/deleteFuncs');
9
10
function prompt() {
11
return inquirer.prompt({
12
name: 'choice',
13
type: 'list',
14
message: 'What would you like to do?',
15
choices: [
16
'View all departments',
17
'View all roles',
18
'View all employees',
19
'View employees by manager',
20
'View employees by department',
21
'Add a department',
22
'Add a role',
23
'Add an employee',
24
'Update an employee role',
25
'Update employee manager',
26
'Delete a department',
27
'Delete a role',
28
'Delete an employee',
29
'EXIT'
30
]
31
}).then(answer => {
32
switch (answer.choice) {
33
case 'View all departments':
34
viewDepartments();
35
break;
36
case 'View all roles':
37
viewRoles();
38
break;
39
case 'View all employees':
40
viewEmployees();
41
break;
42
case 'View employees by manager':
43
viewByManager();
44
break;
45
case 'View employees by department':
46
viewByDepartment();
47
break;
48
case 'Add a department':
49
addDepartment();
50
break;
51
case 'Add a role':
52
addRole();
53
break;
54
case 'Add an employee':
55
addEmployee();
56
break;
57
case 'Update an employee role':
58
updateRole();
59
break;
60
case 'Update an employee manager':
61
updateManager();
62
break;
63
case 'Delete a department':
64
deleteDepartment();
65
break;
66
case 'Delete a role':
67
deleteRole();
68
break;
69
case 'Delete an employee':
70
deleteEmployee();
71
break;
72
case 'EXIT':
73
exitApp();
74
break;
75
default:
76
break;
77
}
78
})
79
};
80
// connect to database
81
db.connect(err => {
82
if (err) throw err;
83
console.log('Welcome to the employee-tracker!');
84
// start the app
85
prompt();
86
});
87
// exit the app
88
function exitApp() {
89
console.log('Thank you for using the employee-tracker!')
90
db.end();
91
};
92
Advertisement
Answer
I think the switch statement is overkill. If you create an object where the key is the option and the function is the value then it is a 1:1 lookup and hypothetically more performant.
Additionally, drop the then statements because we write modern code here. Also congrats on the internship.
JavaScript
1
24
24
1
const choiceObj = {
2
'View all departments':viewDepartments(),
3
'View all roles':viewAllRoles(),
4
etc etc
5
}
6
7
const prompt = async () => {
8
const answer = await inquirer.prompt({
9
name: 'choice',
10
type: 'list',
11
message: 'What would you like to do?',
12
choices: Object.keys(choiceObj) // returns an array of the object keys
13
})
14
15
if (answer) {
16
choiceObj[answer];
17
18
alert('answer submitted'); // UX++
19
setTimeout(function(){
20
prompt(); // recursion after 2s
21
}, 2000);
22
}
23
};
24
I can answer any questions in the comment section. Thanks!