File size: 2,561 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { Dialog } from '@automattic/components';
import { Button, Modal } from '@wordpress/components';
import clsx from 'clsx';
import { localize } from 'i18n-calypso';
import PropTypes from 'prop-types';
import { Component } from 'react';

import './dialog.scss';

class AcceptDialog extends Component {
	static displayName = 'AcceptDialog';

	static propTypes = {
		translate: PropTypes.func,
		message: PropTypes.node,
		title: PropTypes.node,
		onClose: PropTypes.func.isRequired,
		confirmButtonText: PropTypes.node,
		cancelButtonText: PropTypes.node,
		options: PropTypes.shape( {
			isScary: PropTypes.bool,
			additionalClassNames: PropTypes.string,
			useModal: PropTypes.bool,
			modalOptions: PropTypes.object,
		} ),
	};

	state = { isVisible: true };

	onClose = ( action ) => {
		this.setState( { isVisible: false } );
		this.props.onClose( 'accept' === action );
	};

	getActionButtons = () => {
		const { options } = this.props;
		const isScary = options && options.isScary;
		const additionalClassNames = clsx( { 'is-scary': isScary } );
		return [
			{
				action: 'cancel',
				label: this.props.cancelButtonText
					? this.props.cancelButtonText
					: this.props.translate( 'Cancel' ),
				additionalClassNames: 'is-cancel',
			},
			{
				action: 'accept',
				label: this.props.confirmButtonText
					? this.props.confirmButtonText
					: this.props.translate( 'OK' ),
				isPrimary: true,
				additionalClassNames,
			},
		];
	};

	render() {
		if ( ! this.state.isVisible ) {
			return null;
		}
		if ( this.props.options?.useModal ) {
			return (
				<Modal
					title={ this.props.options?.modalOptions?.title }
					onRequestClose={ this.onClose }
					className={ clsx(
						'accept__dialog-use-modal',
						this.props?.options?.additionalClassNames
					) }
					size="medium"
				>
					{ this.props.message }
					<div className="accept__dialog-buttons">
						{ this.getActionButtons().map( ( button ) => (
							<Button
								key={ button.action }
								onClick={ () => this.onClose( button.action ) }
								variant={ button.isPrimary ? 'primary' : 'secondary' }
								className={ button.additionalClassNames }
							>
								{ button.label }
							</Button>
						) ) }
					</div>
				</Modal>
			);
		}
		return (
			<Dialog
				buttons={ this.getActionButtons() }
				onClose={ this.onClose }
				className="accept__dialog"
				isVisible
				additionalClassNames={ this.props?.options?.additionalClassNames }
			>
				{ this.props.message }
			</Dialog>
		);
	}
}

export default localize( AcceptDialog );