-
Notifications
You must be signed in to change notification settings - Fork 8k
/
reset_job_modal.tsx
132 lines (120 loc) · 3.94 KB
/
reset_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
/*
* 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,
EuiText,
} from '@elastic/eui';
import { resetJobs } from '../utils';
import type { MlSummaryJob } from '../../../../../../common/types/anomaly_detection_jobs';
import { RESETTING_JOBS_REFRESH_INTERVAL_MS } from '../../../../../../common/constants/jobs_list';
import { OpenJobsWarningCallout } from './open_jobs_warning_callout';
type ShowFunc = (jobs: MlSummaryJob[]) => void;
interface Props {
setShowFunction(showFunc: ShowFunc): void;
unsetShowFunction(): void;
refreshJobs(): void;
}
export const ResetJobModal: FC<Props> = ({ setShowFunction, unsetShowFunction, refreshJobs }) => {
const [resetting, setResetting] = useState(false);
const [modalVisible, setModalVisible] = useState(false);
const [jobIds, setJobIds] = useState<string[]>([]);
const [jobs, setJobs] = useState<MlSummaryJob[]>([]);
useEffect(() => {
if (typeof setShowFunction === 'function') {
setShowFunction(showModal);
}
return () => {
if (typeof unsetShowFunction === 'function') {
unsetShowFunction();
}
};
}, []);
const showModal = useCallback((tempJobs: MlSummaryJob[]) => {
setJobIds(tempJobs.map(({ id }) => id));
setJobs(tempJobs);
setModalVisible(true);
setResetting(false);
}, []);
const closeModal = useCallback(() => {
setModalVisible(false);
}, []);
const resetJob = useCallback(async () => {
setResetting(true);
await resetJobs(jobIds);
closeModal();
setTimeout(() => {
refreshJobs();
}, RESETTING_JOBS_REFRESH_INTERVAL_MS);
}, [jobIds, refreshJobs]);
if (modalVisible === false || jobIds.length === 0) {
return null;
}
return (
<EuiModal data-test-subj="mlResetJobConfirmModal" onClose={closeModal}>
<EuiModalHeader>
<EuiModalHeaderTitle>
<FormattedMessage
id="xpack.ml.jobsList.resetJobModal.resetJobsTitle"
defaultMessage="Reset {jobsCount, plural, one {{jobId}} other {# jobs}}?"
values={{
jobsCount: jobIds.length,
jobId: jobIds[0],
}}
/>
</EuiModalHeaderTitle>
</EuiModalHeader>
<EuiModalBody>
<>
<OpenJobsWarningCallout jobs={jobs} />
<EuiText>
<FormattedMessage
id="xpack.ml.jobsList.resetJobModal.resetMultipleJobsDescription"
defaultMessage="Resetting {jobsCount, plural, one {a job} other {multiple jobs}} can be time consuming.
{jobsCount, plural, one {It} other {They}} will be reset in the background
and may not be updated in the jobs list instantly."
values={{
jobsCount: jobIds.length,
}}
/>
</EuiText>
</>
</EuiModalBody>
<>
<EuiSpacer />
<EuiModalFooter>
<EuiButtonEmpty onClick={closeModal} disabled={resetting}>
<FormattedMessage
id="xpack.ml.jobsList.resetJobModal.cancelButtonLabel"
defaultMessage="Cancel"
/>
</EuiButtonEmpty>
<EuiButton
onClick={resetJob}
fill
disabled={resetting}
color="danger"
data-test-subj="mlResetJobConfirmModalButton"
>
<FormattedMessage
id="xpack.ml.jobsList.resetJobModal.resetButtonLabel"
defaultMessage="Reset"
/>
</EuiButton>
</EuiModalFooter>
</>
</EuiModal>
);
};