-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete-identifiers-button.tsx
74 lines (67 loc) · 2.63 KB
/
delete-identifiers-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import React, { useState, useRef } from 'react';
import { useAppSelector as useSelector } from '../../hooks';
import DeleteOutlineOutlinedIcon from '@material-ui/icons/DeleteOutlineOutlined';
import { useDispatch } from '../../utils/redux';
import ToolbarButtonWithPopper from './toolbar-button-with-popper';
import ConfirmationPopper from '../confirmation-popper';
import MessageModal from '../message-modal';
import ConfirmationCodeModal from '../confirmation-code-modal';
import { json_get, json_post } from 'etna-js/utils/fetch';
import { magmaPath } from 'etna-js/api/magma_api';
const DeleteIdentifiersButton = ({ small, project_name, data, buttonText, className, refresh }: { small: boolean, className?: string, data: Array<any>, buttonText: string }) => {
const [confirmation, setConfirmation] = useState('');
const [confirmationError, setConfirmationError] = useState('');
const [ message, setMessage ] = useState('');
const [ messageTitle, setMessageTitle ] = useState('');
const handleDelete = () => {
json_post(magmaPath(`gnomon/${project_name}/delete`),
{ identifiers: data.map(d => d.name), confirmation }).then(
({success}) => {
setMessage(success);
setMessageTitle('Deleted Identifiers');
setConfirmation('');
refresh();
}
).catch(
(error) => error.then(
({error}) => {
setConfirmation('');
if (error.includes('confirmation')) setConfirmationError(error);
else {
setMessage(error.length > 100 ? `${error.substring(0,100)}...` : error);
setMessageTitle('Deletion Failed');
}
}
)
)
};
return (
<React.Fragment>
<ToolbarButtonWithPopper
text={ buttonText }
iconComponent={<DeleteOutlineOutlinedIcon />}
variant={small ? 'compact' : 'full'}
color="#FF0000"
onClickOrPopperChange={handleDelete}
disabled={data.length == 0}
className={className}
/>
<ConfirmationCodeModal
open={!!confirmationError}
confirmationError={ confirmationError }
confirmation={ confirmation }
onChange={ setConfirmation }
onClose={ () => {
setConfirmationError('');
setConfirmation('');
} }
onConfirm={ () => {
setConfirmationError('');
handleDelete();
}}
/>
<MessageModal title={messageTitle} open={!!message} onClose={ () => setMessage('') } message={message}/>
</React.Fragment>
);
};
export default DeleteIdentifiersButton;