| 1 |
import React from "react"; |
| 2 |
// PatternFly |
| 3 |
import { |
| 4 |
Button, |
| 5 |
Content, |
| 6 |
ContentVariants, |
| 7 |
Spinner, |
| 8 |
} from "@patternfly/react-core"; |
| 9 |
import useApiError from "src/hooks/useApiErrorModals"; |
| 10 |
// Redux |
| 11 |
import { useAppDispatch } from "src/store/hooks"; |
| 12 |
// RPC |
| 13 |
import { addAlert } from "src/store/Global/alerts-slice"; |
| 14 |
import { useDeleteTrustsMutation } from "src/services/rpcTrusts"; |
| 15 |
// Data types |
| 16 |
import { ErrorData, Trust } from "src/utils/datatypes/globalDataTypes"; |
| 17 |
import { BatchRPCResponse } from "src/services/rpc"; |
| 18 |
import { FetchBaseQueryError } from "@reduxjs/toolkit/query"; |
| 19 |
// Components |
| 20 |
import ModalWithFormLayout from "src/components/layouts/ModalWithFormLayout"; |
| 21 |
import DeletedElementsTable from "src/components/tables/DeletedElementsTable"; |
| 22 |
|
| 23 |
interface DeleteTrustModalProps { |
| 24 |
isOpen: boolean; |
| 25 |
onClose: () => void; |
| 26 |
elementsToDelete: Trust[]; |
| 27 |
clearSelectedElements: () => void; |
| 28 |
columnNames: string[]; // E.g. ["Trust Name", "Trust Type"] |
| 29 |
keyNames: string[]; // E.g. for trust.name, trust.type --> ["name", "type"] |
| 30 |
onRefresh: () => void; |
| 31 |
updateIsDeleteButtonDisabled: (value: boolean) => void; |
| 32 |
updateIsDeletion: (value: boolean) => void; |
| 33 |
} |
| 34 |
|
| 35 |
const DeleteTrustModal = (props: DeleteTrustModalProps) => { |
| 36 |
const dispatch = useAppDispatch(); |
| 37 |
|
| 38 |
// RPC calls |
| 39 |
const [deleteTrusts] = useDeleteTrustsMutation(); |
| 40 |
|
| 41 |
// States |
| 42 |
const [spinning, setBtnSpinning] = React.useState<boolean>(false); |
| 43 |
const { handleAPIError, ErrorModalComponent } = useApiError(); |
| 44 |
|
| 45 |
const onDelete = () => { |
| 46 |
setBtnSpinning(true); |
| 47 |
|
| 48 |
const elementsToDelete = props.elementsToDelete.map((element) => |
| 49 |
element.cn.toString() |
| 50 |
); |
| 51 |
|
| 52 |
deleteTrusts(elementsToDelete) |
| 53 |
.then((response) => { |
| 54 |
if ("data" in response) { |
| 55 |
const data = response.data as BatchRPCResponse; |
| 56 |
const result = data.result; |
| 57 |
|
| 58 |
if (result) { |
| 59 |
if ("error" in result.results[0] && result.results[0].error) { |
| 60 |
const errorData = { |
| 61 |
code: result.results[0].error_code, |
| 62 |
name: result.results[0].error_name, |
| 63 |
error: result.results[0].error, |
| 64 |
} as ErrorData; |
| 65 |
|
| 66 |
const error = { |
| 67 |
status: "CUSTOM_ERROR", |
| 68 |
data: errorData, |
| 69 |
} as FetchBaseQueryError; |
| 70 |
|
| 71 |
// Handle error |
| 72 |
handleAPIError(error); |
| 73 |
} else { |
| 74 |
props.clearSelectedElements(); |
| 75 |
props.updateIsDeleteButtonDisabled(true); |
| 76 |
props.updateIsDeletion(true); |
| 77 |
|
| 78 |
dispatch( |
| 79 |
addAlert({ |
| 80 |
name: "remove-trusts-success", |
| 81 |
title: "Trusts removed", |
| 82 |
variant: "success", |
| 83 |
}) |
| 84 |
); |
| 85 |
|
| 86 |
props.onClose(); |
| 87 |
// Refresh data |
| 88 |
props.onRefresh(); |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
if ("error" in response) { |
| 93 |
const error = response.error as FetchBaseQueryError; |
| 94 |
handleAPIError(error); |
| 95 |
} |
| 96 |
}) |
| 97 |
.finally(() => { |
| 98 |
setBtnSpinning(false); |
| 99 |
}); |
| 100 |
}; |
| 101 |
|
| 102 |
// List of fields |
| 103 |
const fields = [ |
| 104 |
{ |
| 105 |
id: "question-text", |
| 106 |
pfComponent: ( |
| 107 |
<Content component={ContentVariants.p}> |
| 108 |
Are you sure you want to delete the selected entries? |
| 109 |
</Content> |
| 110 |
), |
| 111 |
}, |
| 112 |
{ |
| 113 |
id: "deleted-elements-table", |
| 114 |
pfComponent: ( |
| 115 |
<DeletedElementsTable |
| 116 |
mode="passing_id" |
| 117 |
elementsToDelete={props.elementsToDelete.map((element) => |
| 118 |
element.cn.toString() |
| 119 |
)} |
| 120 |
columnNames={props.columnNames} |
| 121 |
columnIds={props.keyNames} |
| 122 |
elementType="Trust" |
| 123 |
idAttr="cn" |
| 124 |
/> |
| 125 |
), |
| 126 |
}, |
| 127 |
]; |
| 128 |
|
| 129 |
// Modal actions |
| 130 |
const modalActionsDelete: JSX.Element[] = [ |
| 131 |
<Button |
| 132 |
data-cy="modal-button-ok" |
| 133 |
key="delete-trusts" |
| 134 |
variant="danger" |
| 135 |
onClick={onDelete} |
| 136 |
form="delete-trusts-modal" |
| 137 |
spinnerAriaValueText="Deleting" |
| 138 |
spinnerAriaLabel="Deleting" |
| 139 |
isLoading={spinning} |
| 140 |
isDisabled={spinning} |
| 141 |
> |
| 142 |
{spinning ? ( |
| 143 |
<> |
| 144 |
<Spinner size="sm" /> |
| 145 |
{"Deleting"} |
| 146 |
</> |
| 147 |
) : ( |
| 148 |
"Delete" |
| 149 |
)} |
| 150 |
</Button>, |
| 151 |
<Button |
| 152 |
data-cy="modal-button-cancel" |
| 153 |
key="cancel-delete-trusts" |
| 154 |
variant="link" |
| 155 |
onClick={props.onClose} |
| 156 |
> |
| 157 |
Cancel |
| 158 |
</Button>, |
| 159 |
]; |
| 160 |
|
| 161 |
return ( |
| 162 |
<> |
| 163 |
<ModalWithFormLayout |
| 164 |
dataCy="trusts-delete-modal" |
| 165 |
variantType="medium" |
| 166 |
modalPosition="top" |
| 167 |
offPosition="76px" |
| 168 |
title="Remove Trusts" |
| 169 |
formId="remove-trusts-modal" |
| 170 |
fields={fields} |
| 171 |
show={props.isOpen} |
| 172 |
onClose={props.onClose} |
| 173 |
actions={modalActionsDelete} |
| 174 |
/> |
| 175 |
{ErrorModalComponent} |
| 176 |
</> |
| 177 |
); |
| 178 |
}; |
| 179 |
|
| 180 |
export default DeleteTrustModal; |