Once again, asynchronicity is kicking my butt. Problem: I have a configuration file based on which I have to build a multi level object, for example:
export const personnelTasks = {
manager: {
administrative: ['meetings', 'evaluation', 'time-reporting', 'vacation-approval'],
operational: ['budgeting', 'supervising', 'customer-complaints']
},
frontDesk: {
administrative: ['time-tracking', 'file-reports', 'cash-reporting', 'take_on', 'dribble'],
operational: ['answer-calls', 'face-to-face-meetings', 'cash-counting', 'process-requests']
}
};
So, depending on whether the employee is a manager or a front desk employee, I will have a report with 2 tabs, administrative
and operational
and with the various tasks they have to perform. For each of these, the back end returns a series of data, but served from a different API end point. For example, for meetings, I have an API endpoint {DEV_API}/meetings
etc.
What I want to do is call all API endpoints at once and build an object for each type of employee, that would look something like this:
const timeTrackingData = {
administrative: {
meetings: {...}
evaluation: {...}
.....
},
operational: {
budgeting: {...},
supervising: {...}
.....
}
}
My solution is to use the configuration object and build the new object based on it, and for each key, to call the API with an async/ await call.
getTimeTrackingData (employeeID, employeeType) {
const timeTrackingDataObject = {};
Object.keys(personnelTasks[employeeType]).forEach((taskCategory) => {
timeTrackingDataObject[taskCategory] = {};
personnelTasks[employeeType][taskCategory].forEach(async (task) => {
timeTrackingDataObject[taskCategory][task] = {};
timeTrackingDataObject[taskCategory][task] = await axios.get(`${DEV_API}/employees/${employeeId}/time-tracking/${task}`);
});
});
return timeTrackingDataObject;
},
So when I access the timeTrackingObject
, I can see the first level of the Object, but below I see observables that I cannot access. Also, I have serious doubts forEach
loop and async/await
is the way to go, but I just want to get is solved now and then think of nicer/ more efficient implementations.