From dfaf4e1aaec482da64f3a728677b755f8c4962c6 Mon Sep 17 00:00:00 2001 From: Aaron Chong Date: Tue, 28 Nov 2023 07:30:45 +0000 Subject: [PATCH] Reuse getAllTaskRequests Signed-off-by: Aaron Chong --- .../src/components/tasks/tasks-app.tsx | 57 +++++++------------ 1 file changed, 22 insertions(+), 35 deletions(-) diff --git a/packages/dashboard/src/components/tasks/tasks-app.tsx b/packages/dashboard/src/components/tasks/tasks-app.tsx index 676f39ffb..82c697da1 100644 --- a/packages/dashboard/src/components/tasks/tasks-app.tsx +++ b/packages/dashboard/src/components/tasks/tasks-app.tsx @@ -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 = {}; + 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(() => { @@ -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 = {}; - 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, @@ -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 = {}; - 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);