UNPKG

@craftercms/studio-ui

Version:

Services, components, models & utils to build CrafterCMS authoring extensions.

145 lines (143 loc) 5.62 kB
/* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ /* * Copyright (C) 2007-2022 Crafter Software Corporation. All Rights Reserved. * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License version 3 as published by * the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import * as React from 'react'; import { useEffect, useState } from 'react'; import ConfirmDialog from '../ConfirmDialog/ConfirmDialog'; import { Alert } from '@mui/material'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { TextField } from '@mui/material'; import { clearLock } from '../../services/publishing'; import { useDispatch } from 'react-redux'; import { showSystemNotification } from '../../state/actions/system'; import { fetchPublishingStatus } from '../../state/actions/publishingStatus'; import { useActiveSiteId } from '../../hooks/useActiveSiteId'; const messages = defineMessages({ unlockComplete: { id: 'unlockPublisherDialog.unlockCompleteMessage', defaultMessage: 'Publisher lock released successfully.' }, unlockFailed: { id: 'unlockPublisherDialog.unlockFailedMessage', defaultMessage: 'Error releasing publisher lock.' } }); function UnlockPublisherDialog(props) { const { open, onCancel, onComplete, onError, password = 'unlock' } = props; const [submitting, setSubmitting] = useState(false); const [confirmPasswordPassed, setConfirmPasswordPassed] = useState(false); const [passwordFieldValue, setPasswordFieldValue] = useState(''); const site = useActiveSiteId(); const dispatch = useDispatch(); const { formatMessage } = useIntl(); useEffect(() => { setConfirmPasswordPassed(passwordFieldValue.toLowerCase() === password.toLowerCase()); }, [password, passwordFieldValue]); useEffect(() => { !open && setPasswordFieldValue(''); }, [open]); const onSubmit = () => { if (confirmPasswordPassed) { setSubmitting(true); clearLock(site).subscribe( () => { setSubmitting(false); dispatch(showSystemNotification({ message: formatMessage(messages.unlockComplete) })); dispatch(fetchPublishingStatus()); onComplete === null || onComplete === void 0 ? void 0 : onComplete(); }, (error) => { var _a, _b, _c, _d; setSubmitting(false); const response = (_c = (_b = (_a = error.response) === null || _a === void 0 ? void 0 : _a.response) !== null && _b !== void 0 ? _b : error.response) !== null && _c !== void 0 ? _c : error; dispatch( showSystemNotification({ message: (_d = response === null || response === void 0 ? void 0 : response.message) !== null && _d !== void 0 ? _d : formatMessage(messages.unlockFailed), options: { variant: 'error' } }) ); onError === null || onError === void 0 ? void 0 : onError({ error }); } ); } }; return React.createElement( ConfirmDialog, { open: open, disableOkButton: !confirmPasswordPassed || submitting, disableCancelButton: submitting, disableBackdropClick: submitting, disableEscapeKeyDown: submitting, title: React.createElement(FormattedMessage, { id: 'unlockPublisherDialog.dialogTitle', defaultMessage: 'Confirm Publisher Unlock' }), body: React.createElement(FormattedMessage, { id: 'unlockPublisherDialog.dialogCopy', defaultMessage: 'Please confirm the release of the publisher lock' }), onOk: onSubmit, onCancel: onCancel }, React.createElement( Alert, { severity: 'warning', icon: false }, React.createElement(FormattedMessage, { id: 'unlockPublisherDialog.typeConfirmPassword', defaultMessage: 'Type the word "<b>{password}</b>" to confirm you understand the implications and wish to proceed.', values: { password, b: (message) => React.createElement('strong', null, message) } }), React.createElement(TextField, { fullWidth: true, autoFocus: true, disabled: submitting, style: { marginTop: '1em' }, value: passwordFieldValue, onChange: (e) => setPasswordFieldValue(e.target.value), onKeyPress: (e) => e.key === 'Enter' && onSubmit() }) ) ); } export default UnlockPublisherDialog;