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
const db = require('./db/connection');
const inquirer = require('inquirer');
// response handling
const { viewDepartments, viewRoles, viewEmployees, viewByManager, viewByDepartment } = require('./lib/viewFuncs');
const { addDepartment, addRole, addEmployee } = require('./lib/addFuncs');
const { updateRole, updateManager } = require('./lib/updateFuncs');
const { deleteDepartment, deleteRole, deleteEmployee } = require('./lib/deleteFuncs');
function prompt() {
return inquirer.prompt({
name: 'choice',
type: 'list',
message: 'What would you like to do?',
choices: [
'View all departments',
'View all roles',
'View all employees',
'View employees by manager',
'View employees by department',
'Add a department',
'Add a role',
'Add an employee',
'Update an employee role',
'Update employee manager',
'Delete a department',
'Delete a role',
'Delete an employee',
'EXIT'
]
}).then(answer => {
switch (answer.choice) {
case 'View all departments':
viewDepartments();
break;
case 'View all roles':
viewRoles();
break;
case 'View all employees':
viewEmployees();
break;
case 'View employees by manager':
viewByManager();
break;
case 'View employees by department':
viewByDepartment();
break;
case 'Add a department':
addDepartment();
break;
case 'Add a role':
addRole();
break;
case 'Add an employee':
addEmployee();
break;
case 'Update an employee role':
updateRole();
break;
case 'Update an employee manager':
updateManager();
break;
case 'Delete a department':
deleteDepartment();
break;
case 'Delete a role':
deleteRole();
break;
case 'Delete an employee':
deleteEmployee();
break;
case 'EXIT':
exitApp();
break;
default:
break;
}
})
};
// connect to database
db.connect(err => {
if (err) throw err;
console.log('Welcome to the employee-tracker!');
// start the app
prompt();
});
// exit the app
function exitApp() {
console.log('Thank you for using the employee-tracker!')
db.end();
};
CodePudding user response:
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.
const choiceObj = {
'View all departments':viewDepartments(),
'View all roles':viewAllRoles(),
...etc etc
}
const prompt = async () => {
const answer = await inquirer.prompt({
name: 'choice',
type: 'list',
message: 'What would you like to do?',
choices: choiceObj.keys() // returns an array of the object keys
})
if (answer) {
choiceObj[answer];
alert('answer submitted'); // UX
setTimeout(function(){
prompt(); // recursion after 2s
}, 2000);
}
};
I can answer any questions in the comment section. Thanks!