bigbuffet-rw/app/soapbox/features/ui/components/confirmation_modal.js

86 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-03-27 13:59:38 -07:00
import PropTypes from 'prop-types';
import React from 'react';
2020-03-27 13:59:38 -07:00
import { injectIntl, FormattedMessage } from 'react-intl';
import { SimpleForm, FieldsGroup, Checkbox } from 'soapbox/features/forms';
2022-03-21 11:09:01 -07:00
import { Modal } from '../../../components/ui';
2020-03-27 13:59:38 -07:00
export default @injectIntl
class ConfirmationModal extends React.PureComponent {
static propTypes = {
heading: PropTypes.node,
icon: PropTypes.node,
2020-03-27 13:59:38 -07:00
message: PropTypes.node.isRequired,
confirm: PropTypes.node.isRequired,
2020-03-27 13:59:38 -07:00
onClose: PropTypes.func.isRequired,
onConfirm: PropTypes.func.isRequired,
secondary: PropTypes.string,
onSecondary: PropTypes.func,
intl: PropTypes.object.isRequired,
onCancel: PropTypes.func,
checkbox: PropTypes.node,
2020-03-27 13:59:38 -07:00
};
state = {
checked: false,
}
2020-03-27 13:59:38 -07:00
handleClick = () => {
this.props.onClose('CONFIRM');
2020-03-27 13:59:38 -07:00
this.props.onConfirm();
}
handleSecondary = () => {
this.props.onClose('CONFIRM');
2020-03-27 13:59:38 -07:00
this.props.onSecondary();
}
handleCancel = () => {
2020-04-14 11:44:40 -07:00
const { onClose, onCancel } = this.props;
onClose('CONFIRM');
2020-03-27 13:59:38 -07:00
if (onCancel) onCancel();
}
handleCheckboxChange = e => {
this.setState({ checked: e.target.checked });
}
render() {
2022-03-21 11:09:01 -07:00
const { heading, message, confirm, secondary, checkbox } = this.props;
const { checked } = this.state;
2020-03-27 13:59:38 -07:00
return (
2022-03-21 11:09:01 -07:00
<Modal
title={heading}
confirmationAction={this.handleClick}
confirmationText={confirm}
confirmationDisabled={checkbox && !checked}
confirmationTheme='danger'
cancelText={<FormattedMessage id='confirmation_modal.cancel' defaultMessage='Cancel' />}
cancelAction={this.handleCancel}
secondaryText={secondary}
secondaryAction={this.props.onSecondary && this.handleSecondary}
2022-03-21 11:09:01 -07:00
>
<p className='text-gray-600 dark:text-gray-300'>{message}</p>
2022-03-21 11:09:01 -07:00
<div className='mt-2'>
{checkbox && <div className='confirmation-modal__checkbox'>
<SimpleForm>
<FieldsGroup>
<Checkbox
onChange={this.handleCheckboxChange}
label={checkbox}
checked={checked}
/>
</FieldsGroup>
</SimpleForm>
</div>}
2020-03-27 13:59:38 -07:00
</div>
2022-03-21 11:09:01 -07:00
</Modal>
2020-03-27 13:59:38 -07:00
);
}
}