|
| 1 | +import React from "react"; |
| 2 | +import { Typography, IconButton, Tooltip } from "@material-ui/core"; |
| 3 | +import { BaseComponentProps, RowModel } from "@material-ui/data-grid"; |
| 4 | +import { BaseModal } from "../BaseModal"; |
| 5 | +import { useSnackbar } from "notistack"; |
| 6 | +import { Delete, ThumbDown, ThumbUp } from "@material-ui/icons"; |
| 7 | +import { testRunService } from "../../services"; |
| 8 | +import { TestStatus } from "../../types"; |
| 9 | + |
| 10 | +export const BulkOperation: React.FunctionComponent<BaseComponentProps> = ( |
| 11 | + props: BaseComponentProps |
| 12 | +) => { |
| 13 | + const { enqueueSnackbar } = useSnackbar(); |
| 14 | + const [approveDialogOpen, setApproveDialogOpen] = React.useState(false); |
| 15 | + const [rejectDialogOpen, setRejectDialogOpen] = React.useState(false); |
| 16 | + const [deleteDialogOpen, setDeleteDialogOpen] = React.useState(false); |
| 17 | + |
| 18 | + const rows: Record<React.ReactText, boolean> = props.state.selection; |
| 19 | + const count = Object.keys(rows).length; |
| 20 | + |
| 21 | + const toggleApproveDialogOpen = () => { |
| 22 | + setApproveDialogOpen(!approveDialogOpen); |
| 23 | + }; |
| 24 | + const toggleRejectDialogOpen = () => { |
| 25 | + setRejectDialogOpen(!rejectDialogOpen); |
| 26 | + }; |
| 27 | + const toggleDeleteDialogOpen = () => { |
| 28 | + setDeleteDialogOpen(!deleteDialogOpen); |
| 29 | + }; |
| 30 | + |
| 31 | + const getTitle = () => { |
| 32 | + return submitButtonText() + " Test Runs"; |
| 33 | + }; |
| 34 | + |
| 35 | + const submitButtonText = (): string => { |
| 36 | + if (deleteDialogOpen) { |
| 37 | + return "Delete"; |
| 38 | + } |
| 39 | + if (approveDialogOpen) { |
| 40 | + return "Approve"; |
| 41 | + } |
| 42 | + if (rejectDialogOpen) { |
| 43 | + return "Reject"; |
| 44 | + } |
| 45 | + return ""; |
| 46 | + }; |
| 47 | + |
| 48 | + const closeModal = () => { |
| 49 | + if (deleteDialogOpen) { |
| 50 | + return toggleDeleteDialogOpen(); |
| 51 | + } |
| 52 | + if (approveDialogOpen) { |
| 53 | + return toggleApproveDialogOpen(); |
| 54 | + } |
| 55 | + if (rejectDialogOpen) { |
| 56 | + return toggleRejectDialogOpen(); |
| 57 | + } |
| 58 | + }; |
| 59 | + |
| 60 | + const isRowEligibleForApproveOrReject = (id: string) => { |
| 61 | + //Find the test status of the current row |
| 62 | + let currentRow: any = props.rows.find((value: RowModel) => value.id.toString().includes(id)); |
| 63 | + let currentRowStatus = JSON.stringify(currentRow.status); |
| 64 | + //In line with how we can approve/reject only new and unresolved from details modal. |
| 65 | + return (currentRowStatus.includes(TestStatus.new) || currentRowStatus.includes(TestStatus.unresolved)); |
| 66 | + }; |
| 67 | + |
| 68 | + const processAction = (id: string) => { |
| 69 | + if (deleteDialogOpen) { |
| 70 | + testRunService.remove(id); |
| 71 | + } |
| 72 | + if (isRowEligibleForApproveOrReject(id)) { |
| 73 | + processApproveReject(id); |
| 74 | + } |
| 75 | + }; |
| 76 | + |
| 77 | + const processApproveReject = (id: string) => { |
| 78 | + if (approveDialogOpen) { |
| 79 | + testRunService.approve(id, false); |
| 80 | + } |
| 81 | + if (rejectDialogOpen) { |
| 82 | + testRunService.reject(id); |
| 83 | + } |
| 84 | + }; |
| 85 | + |
| 86 | + const dismissDialog = () => { |
| 87 | + if (deleteDialogOpen) { |
| 88 | + return toggleDeleteDialogOpen(); |
| 89 | + } |
| 90 | + if (approveDialogOpen) { |
| 91 | + return toggleApproveDialogOpen(); |
| 92 | + } |
| 93 | + return toggleRejectDialogOpen(); |
| 94 | + }; |
| 95 | + |
| 96 | + return ( |
| 97 | + <> |
| 98 | + <Tooltip title="Approve unresolved in selected rows." aria-label="approve"> |
| 99 | + <span> |
| 100 | + <IconButton disabled={count === 0} onClick={toggleApproveDialogOpen}> |
| 101 | + <ThumbUp /> |
| 102 | + </IconButton> |
| 103 | + </span> |
| 104 | + </Tooltip> |
| 105 | + <Tooltip title="Reject unresolved in selected rows." aria-label="reject"> |
| 106 | + <span> |
| 107 | + <IconButton disabled={count === 0} onClick={toggleRejectDialogOpen}> |
| 108 | + <ThumbDown /> |
| 109 | + </IconButton> |
| 110 | + </span> |
| 111 | + </Tooltip> |
| 112 | + <Tooltip title="Delete selected rows." aria-label="delete"> |
| 113 | + <span> |
| 114 | + <IconButton disabled={count === 0} onClick={toggleDeleteDialogOpen}> |
| 115 | + <Delete /> |
| 116 | + </IconButton> |
| 117 | + </span> |
| 118 | + </Tooltip> |
| 119 | + |
| 120 | + <BaseModal |
| 121 | + open={deleteDialogOpen || approveDialogOpen || rejectDialogOpen} |
| 122 | + title={getTitle()} |
| 123 | + submitButtonText={submitButtonText()} |
| 124 | + onCancel={dismissDialog} |
| 125 | + content={ |
| 126 | + <Typography>{`Are you sure you want to ${submitButtonText().toLowerCase()} ${count} items?`}</Typography> |
| 127 | + } |
| 128 | + onSubmit={() => { |
| 129 | + enqueueSnackbar( |
| 130 | + "Wait for the confirmation message until operation is completed.", |
| 131 | + { |
| 132 | + variant: "info", |
| 133 | + } |
| 134 | + ); |
| 135 | + |
| 136 | + Promise.all( |
| 137 | + Object.keys(rows).map((id: string) => processAction(id)) |
| 138 | + ) |
| 139 | + .then(() => { |
| 140 | + enqueueSnackbar(`${count} test runs processed.`, { |
| 141 | + variant: "success", |
| 142 | + }); |
| 143 | + }) |
| 144 | + .catch((err) => |
| 145 | + enqueueSnackbar(err, { |
| 146 | + variant: "error", |
| 147 | + }) |
| 148 | + ); |
| 149 | + closeModal(); |
| 150 | + }} |
| 151 | + /> |
| 152 | + </> |
| 153 | + ); |
| 154 | +}; |
0 commit comments