Skip to content

Commit

Permalink
Reuse getAllTaskRequests
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Chong <aaronchongth@gmail.com>
  • Loading branch information
aaronchongth committed Nov 28, 2023
1 parent b20c8c0 commit dfaf4e1
Showing 1 changed file with 22 additions and 35 deletions.
57 changes: 22 additions & 35 deletions packages/dashboard/src/components/tasks/tasks-app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,27 @@ export const TasksApp = React.memo(
};
}, [autoRefresh]);

const getAllTaskRequests = async (tasks: TaskState[]) => {
if (!rmf) {
return {};
}

const taskIds: string[] = tasks.map((task) => task.booking.id);
const taskIdsQuery = taskIds.join(',');
const taskRequests = (await rmf.tasksApi.queryTaskRequestsTasksRequestsGet(taskIdsQuery))
.data;

const taskRequestMap: Record<string, TaskRequest> = {};
let requestIndex = 0;
for (const id of taskIds) {
if (requestIndex < taskRequests.length && taskRequests[requestIndex]) {
taskRequestMap[id] = taskRequests[requestIndex];
}
++requestIndex;
}
return taskRequestMap;
};

// TODO: parameterize this variable
const GET_LIMIT = 10;
React.useEffect(() => {
Expand Down Expand Up @@ -182,20 +203,7 @@ export const TasksApp = React.memo(
);
const results = resp.data as TaskState[];
const newTasks = results.slice(0, GET_LIMIT);

const taskIds: string[] = newTasks.map((task) => task.booking.id);
const taskIdsQuery = taskIds.join(',');
const taskRequests = (await rmf.tasksApi.queryTaskRequestsTasksRequestsGet(taskIdsQuery))
.data;

const taskRequestMap: Record<string, TaskRequest> = {};
let requestIndex = 0;
for (const id of taskIds) {
if (requestIndex < taskRequests.length && taskRequests[requestIndex]) {
taskRequestMap[id] = taskRequests[requestIndex];
}
++requestIndex;
}
const taskRequestMap = await getAllTaskRequests(newTasks);

setTasksState((old) => ({
...old,
Expand Down Expand Up @@ -242,27 +250,6 @@ export const TasksApp = React.memo(
return allTasks;
};

const getAllTaskRequests = async (tasks: TaskState[]) => {
if (!rmf) {
return {};
}

const taskIds: string[] = tasks.map((task) => task.booking.id);
const taskIdsQuery = taskIds.join(',');
const taskRequests = (await rmf.tasksApi.queryTaskRequestsTasksRequestsGet(taskIdsQuery))
.data;

const taskRequestMap: Record<string, TaskRequest> = {};
let requestIndex = 0;
for (const id of taskIds) {
if (requestIndex < taskRequests.length && taskRequests[requestIndex]) {
taskRequestMap[id] = taskRequests[requestIndex];
}
++requestIndex;
}
return taskRequestMap;
};

const exportTasksToCsv = async (minimal: boolean) => {
const now = new Date();
const allTasks = await getAllTasks(now);
Expand Down

0 comments on commit dfaf4e1

Please sign in to comment.