-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathSessionConfirmationModal.js
More file actions
62 lines (54 loc) · 1.97 KB
/
Copy pathSessionConfirmationModal.js
File metadata and controls
62 lines (54 loc) · 1.97 KB
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
import { useCallback, useRef, useState, useEffect } from 'react';
import PropTypes from 'prop-types';
import uniqueId from 'lodash/uniqueId';
import { useSessionStorage } from 'usehooks-ts'
import { useIntl } from 'react-intl';
import ConfirmationModal, { ConfirmationModalPropTypes } from './ConfirmationModal';
import Checkbox from '../Checkbox';
import Layout from '../Layout';
const propTypes = {
...ConfirmationModalPropTypes,
checkboxLabel: PropTypes.node,
sessionKey: PropTypes.string,
};
const SessionConfirmationModal = ({ sessionKey, checkboxLabel, onConfirm, open, message, ...rest }) => {
const { formatMessage } = useIntl();
const sessionKeyRef = useRef(sessionKey || uniqueId('confirmation-modal-'));
const [suppressStorage, setSuppressStorage] = useSessionStorage(sessionKeyRef.current, false);
const [suppress, setSuppress] = useState(false);
useEffect(() => {
if (suppressStorage && open) {
onConfirm();
}
}, [suppressStorage, open, onConfirm]);
const handleSessionConfirm = useCallback(() => {
onConfirm();
if (suppress) {
setSuppressStorage(suppress);
}
}, [onConfirm, suppress, setSuppressStorage]);
const appendedMessage = (
<>
{message}
<Layout className="marginTop1">
<Checkbox
label={formatMessage({
id: 'ConfirmationModal.suppressLabel',
defaultMessage: 'Do not display this message again.'
})}
onChange={(e) => setSuppress(e.target.checked)}
/>
<div className="margin-start-gutter">{formatMessage({
id: 'ConfirmationModal.suppressMessage',
defaultMessage: 'If checked, this message will be automatically confirmed for the rest of the session.'
})}
</div>
</Layout>
</>
);
return (
<ConfirmationModal {...rest} open={open} message={appendedMessage} onConfirm={handleSessionConfirm} />
)
}
SessionConfirmationModal.propTypes = propTypes;
export default SessionConfirmationModal;