forked from openedx/frontend-app-authoring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeXToConfirmModal.tsx
More file actions
117 lines (109 loc) · 3.03 KB
/
TypeXToConfirmModal.tsx
File metadata and controls
117 lines (109 loc) · 3.03 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
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
import React, { useEffect } from 'react';
import {
ActionRow,
Button,
Card,
Form,
Icon,
ModalDialog,
} from '@openedx/paragon';
import { WarningFilled } from '@openedx/paragon/icons';
import { useIntl } from '@edx/frontend-platform/i18n';
import messages from './messages';
interface TypeXToConfirmModalProps {
label: string;
bodyText: string | React.ReactNode;
confirmLabel: string;
cancelLabel: string;
X: string;
confirmPayload?: Record<string, any> | null;
isOpen: boolean;
onConfirm: (confirmPayload?: Record<string, any> | null) => void;
onCancel: () => void;
setConfirmPayload?: (confirmPayload: Record<string, any> | null) => void;
}
const TypeXToConfirmModal: React.FC<TypeXToConfirmModalProps> = ({
label,
X,
bodyText,
confirmLabel,
cancelLabel,
isOpen,
confirmPayload,
onConfirm,
onCancel,
setConfirmPayload,
}) => {
const [confirmedByTyping, setConfirmedByTyping] = React.useState(false);
const intl = useIntl();
const handleConfirm = () => {
if (!confirmedByTyping) { return; }
setConfirmedByTyping(false);
onConfirm(confirmPayload);
};
const handleCancel = () => {
setConfirmedByTyping(false);
onCancel();
};
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.value === X) {
setConfirmedByTyping(true);
} else {
setConfirmedByTyping(false);
}
};
// Don't remove. This is necessary to prevent an old state from erroneously enabling the confirm button
useEffect(() => {
if (!isOpen) {
setConfirmedByTyping(false);
if (setConfirmPayload) {
setConfirmPayload(null);
}
}
}, [X, isOpen, confirmPayload, setConfirmPayload]);
return (
<ModalDialog
title={label}
isOpen={isOpen}
onClose={handleCancel}
isOverflowVisible
>
<ModalDialog.Header>
<ModalDialog.Title>{label}</ModalDialog.Title>
</ModalDialog.Header>
<ModalDialog.Body>
<Card className="bg-warning-100">
<Card.Section>
<div className="d-flex align-items-start mb-2">
<Icon src={WarningFilled} className="text-warning-500 mr-2" />
<div className="small">{bodyText}</div>
</div>
</Card.Section>
</Card>
<div className="mt-3">
<div>
{intl.formatMessage(messages.typeToConfirmInstruction, {
X,
strong: (chunks: React.ReactNode) => <strong>{chunks}</strong>,
})}
</div>
<Form.Control
onChange={handleChange}
className="mt-4"
/>
</div>
</ModalDialog.Body>
<ModalDialog.Footer>
<ActionRow>
<Button variant="tertiary" onClick={handleCancel}>
{cancelLabel}
</Button>
<Button onClick={handleConfirm} disabled={!confirmedByTyping} variant="danger">
{confirmLabel}
</Button>
</ActionRow>
</ModalDialog.Footer>
</ModalDialog>
);
};
export default React.memo(TypeXToConfirmModal);