forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
delete_job_modal.tsx
162 lines (149 loc) · 4.83 KB
/
delete_job_modal.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { FC, useState, useEffect, useCallback } from 'react';
import { FormattedMessage } from '@kbn/i18n/react';
import {
EuiSpacer,
EuiModal,
EuiModalHeader,
EuiModalHeaderTitle,
EuiModalBody,
EuiModalFooter,
EuiButtonEmpty,
EuiButton,
EuiLoadingSpinner,
EuiText,
} from '@elastic/eui';
import { deleteJobs } from '../utils';
import { DELETING_JOBS_REFRESH_INTERVAL_MS } from '../../../../../../common/constants/jobs_list';
import { DeleteJobCheckModal } from '../../../../components/delete_job_check_modal';
import { MlSummaryJob } from '../../../../../../common/types/anomaly_detection_jobs';
type ShowFunc = (jobs: MlSummaryJob[]) => void;
interface Props {
setShowFunction(showFunc: ShowFunc): void;
unsetShowFunction(): void;
refreshJobs(): void;
}
export const DeleteJobModal: FC<Props> = ({ setShowFunction, unsetShowFunction, refreshJobs }) => {
const [deleting, setDeleting] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
const [jobIds, setJobIds] = useState<string[]>([]);
const [canDelete, setCanDelete] = useState(false);
useEffect(() => {
if (typeof setShowFunction === 'function') {
setShowFunction(showModal);
}
return () => {
if (typeof unsetShowFunction === 'function') {
unsetShowFunction();
}
};
}, []);
const showModal = useCallback((jobs: MlSummaryJob[]) => {
setJobIds(jobs.map(({ id }) => id));
setModalVisible(true);
setDeleting(false);
}, []);
const closeModal = useCallback(() => {
setModalVisible(false);
setCanDelete(false);
}, []);
const deleteJob = useCallback(() => {
setDeleting(true);
deleteJobs(jobIds.map((id) => ({ id })));
setTimeout(() => {
closeModal();
refreshJobs();
}, DELETING_JOBS_REFRESH_INTERVAL_MS);
}, [jobIds, refreshJobs]);
if (modalVisible === false || jobIds.length === 0) {
return null;
}
if (canDelete) {
return (
<EuiModal data-test-subj="mlDeleteJobConfirmModal" onClose={closeModal}>
<EuiModalHeader>
<EuiModalHeaderTitle>
<FormattedMessage
id="xpack.ml.jobsList.deleteJobModal.deleteJobsTitle"
defaultMessage="Delete {jobsCount, plural, one {{jobId}} other {# jobs}}?"
values={{
jobsCount: jobIds.length,
jobId: jobIds[0],
}}
/>
</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody>
<p>
{deleting === true ? (
<div>
<FormattedMessage
id="xpack.ml.jobsList.deleteJobModal.deletingJobsStatusLabel"
defaultMessage="Deleting jobs"
/>
<EuiSpacer />
<div style={{ textAlign: 'center' }}>
<EuiLoadingSpinner size="l" />
</div>
</div>
) : (
<EuiText>
<FormattedMessage
id="xpack.ml.jobsList.deleteJobModal.deleteMultipleJobsDescription"
defaultMessage="Deleting {jobsCount, plural, one {a job} other {multiple jobs}} can be time consuming.
{jobsCount, plural, one {It} other {They}} will be deleted in the background
and may not disappear from the jobs list instantly."
values={{
jobsCount: jobIds.length,
}}
/>
</EuiText>
)}
</p>
</EuiModalBody>
<>
<EuiSpacer />
<EuiModalFooter>
<EuiButtonEmpty onClick={closeModal} disabled={deleting}>
<FormattedMessage
id="xpack.ml.jobsList.deleteJobModal.cancelButtonLabel"
defaultMessage="Cancel"
/>
</EuiButtonEmpty>
<EuiButton
onClick={deleteJob}
fill
disabled={deleting}
color="danger"
data-test-subj="mlDeleteJobConfirmModalButton"
>
<FormattedMessage
id="xpack.ml.jobsList.deleteJobModal.deleteButtonLabel"
defaultMessage="Delete"
/>
</EuiButton>
</EuiModalFooter>
</>
</EuiModal>
);
} else {
return (
<>
<DeleteJobCheckModal
jobIds={jobIds}
jobType="anomaly-detector"
canDeleteCallback={() => {
setCanDelete(true);
}}
onCloseCallback={closeModal}
refreshJobsCallback={refreshJobs}
/>
</>
);
}
};