From 5ed0d74ba9b697f7ea7a88a84d5f793b0a5184b2 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 3 Apr 2022 06:29:13 -0700 Subject: [PATCH] (ui): Make task action props optional --- ui/src/components/TasksTable.tsx | 167 +++++++++++++++++-------------- 1 file changed, 90 insertions(+), 77 deletions(-) diff --git a/ui/src/components/TasksTable.tsx b/ui/src/components/TasksTable.tsx index 6644476..9a29ac3 100644 --- a/ui/src/components/TasksTable.tsx +++ b/ui/src/components/TasksTable.tsx @@ -64,15 +64,15 @@ interface Props { // actions listTasks: (qname: string, pgn: PaginationOptions) => void; - batchDeleteTasks: (qname: string, taskIds: string[]) => Promise; - batchRunTasks: (qname: string, taskIds: string[]) => Promise; - batchArchiveTasks: (qname: string, taskIds: string[]) => Promise; - deleteAllTasks: (qname: string) => Promise; - runAllTasks: (qname: string) => Promise; - archiveAllTasks: (qname: string) => Promise; - deleteTask: (qname: string, taskId: string) => Promise; - runTask: (qname: string, taskId: string) => Promise; - archiveTask: (qname: string, taskId: string) => Promise; + batchDeleteTasks?: (qname: string, taskIds: string[]) => Promise; + batchRunTasks?: (qname: string, taskIds: string[]) => Promise; + batchArchiveTasks?: (qname: string, taskIds: string[]) => Promise; + deleteAllTasks?: (qname: string) => Promise; + runAllTasks?: (qname: string) => Promise; + archiveAllTasks?: (qname: string) => Promise; + deleteTask?: (qname: string, taskId: string) => Promise; + runTask?: (qname: string, taskId: string) => Promise; + archiveTask?: (qname: string, taskId: string) => Promise; taskRowsPerPageChange: (n: number) => void; } @@ -106,29 +106,71 @@ export default function TasksTable(props: Props) { } }; - const handleRunAllClick = () => { - props.runAllTasks(queue); - }; + function createAllTasksHandler(action: (qname: string) => Promise) { + return () => action(queue); + } - const handleDeleteAllClick = () => { - props.deleteAllTasks(queue); - }; + function createBatchTasksHandler( + action: (qname: string, taskIds: string[]) => Promise + ) { + return () => action(queue, selectedIds).then(() => setSelectedIds([])); + } - const handleArchiveAllClick = () => { - props.archiveAllTasks(queue); - }; + function createTaskAction( + action: (qname: string, taskId: string) => Promise, + taskId: string + ) { + return () => action(queue, taskId); + } - const handleBatchRunClick = () => { - props.batchRunTasks(queue, selectedIds).then(() => setSelectedIds([])); - }; + let allActions = []; + if (props.deleteAllTasks) { + allActions.push({ + label: "Delete All", + onClick: createAllTasksHandler(props.deleteAllTasks), + disabled: props.allActionPending, + }); + } + if (props.archiveAllTasks) { + allActions.push({ + label: "Archive All", + onClick: createAllTasksHandler(props.archiveAllTasks), + disabled: props.allActionPending, + }); + } + if (props.runAllTasks) { + allActions.push({ + label: "Run All", + onClick: createAllTasksHandler(props.runAllTasks), + disabled: props.allActionPending, + }); + } - const handleBatchDeleteClick = () => { - props.batchDeleteTasks(queue, selectedIds).then(() => setSelectedIds([])); - }; - - const handleBatchArchiveClick = () => { - props.batchArchiveTasks(queue, selectedIds).then(() => setSelectedIds([])); - }; + let batchActions = []; + if (props.batchDeleteTasks) { + batchActions.push({ + tooltip: "Delete", + icon: , + disabled: props.batchActionPending, + onClick: createBatchTasksHandler(props.batchDeleteTasks), + }); + } + if (props.batchArchiveTasks) { + batchActions.push({ + tooltip: "Archive", + icon: , + disabled: props.batchActionPending, + onClick: createBatchTasksHandler(props.batchArchiveTasks), + }); + } + if (props.batchRunTasks) { + batchActions.push({ + tooltip: "Run", + icon: , + disabled: props.batchActionPending, + onClick: createBatchTasksHandler(props.batchRunTasks), + }); + } const fetchData = useCallback(() => { const pageOpts = { page: page + 1, size: pageSize }; @@ -161,43 +203,8 @@ export default function TasksTable(props: Props) { {!window.READ_ONLY && ( 0} - iconButtonActions={[ - { - tooltip: "Delete", - icon: , - onClick: handleBatchDeleteClick, - disabled: props.batchActionPending, - }, - { - tooltip: "Archive", - icon: , - onClick: handleBatchArchiveClick, - disabled: props.batchActionPending, - }, - { - tooltip: "Run", - icon: , - onClick: handleBatchRunClick, - disabled: props.batchActionPending, - }, - ]} - menuItemActions={[ - { - label: "Delete All", - onClick: handleDeleteAllClick, - disabled: props.allActionPending, - }, - { - label: "Archive All", - onClick: handleArchiveAllClick, - disabled: props.allActionPending, - }, - { - label: "Run All", - onClick: handleRunAllClick, - disabled: props.allActionPending, - }, - ]} + iconButtonActions={batchActions} + menuItemActions={allActions} /> )} @@ -256,15 +263,21 @@ export default function TasksTable(props: Props) { setSelectedIds(selectedIds.filter((id) => id !== task.id)); } }} - onRunClick={() => { - props.runTask(queue, task.id); - }} - onDeleteClick={() => { - props.deleteTask(queue, task.id); - }} - onArchiveClick={() => { - props.archiveTask(queue, task.id); - }} + onRunClick={ + props.runTask + ? createTaskAction(props.runTask, task.id) + : undefined + } + onDeleteClick={ + props.deleteTask + ? createTaskAction(props.deleteTask, task.id) + : undefined + } + onArchiveClick={ + props.archiveTask + ? createTaskAction(props.archiveTask, task.id) + : undefined + } onActionCellEnter={() => setActiveTaskId(task.id)} onActionCellLeave={() => setActiveTaskId("")} showActions={activeTaskId === task.id} @@ -338,9 +351,9 @@ interface RowProps { task: TaskInfoExtended; isSelected: boolean; onSelectChange: (checked: boolean) => void; - onRunClick: () => void; - onDeleteClick: () => void; - onArchiveClick: () => void; + onRunClick?: () => void; + onDeleteClick?: () => void; + onArchiveClick?: () => void; allActionPending: boolean; showActions: boolean; onActionCellEnter: () => void;