Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Use the 'ConfirmDialog' component in template validation notice #60385

Merged
merged 5 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 49 additions & 50 deletions packages/editor/src/components/template-validation-notice/index.js
Original file line number Diff line number Diff line change
@@ -1,63 +1,62 @@
/**
* WordPress dependencies
*/
import { Notice } from '@wordpress/components';
import {
Notice,
__experimentalConfirmDialog as ConfirmDialog,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { withSelect, withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { useDispatch, useSelect } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { store as blockEditorStore } from '@wordpress/block-editor';

function TemplateValidationNotice( { isValid, ...props } ) {
export default function TemplateValidationNotice() {
const [ showConfirmDialog, setShowConfirmDialog ] = useState( false );
const { isValid } = useSelect( ( select ) => {
return {
isValid: select( blockEditorStore ).isValidTemplate(),
};
}, [] );
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
const { setTemplateValidity, synchronizeTemplate } =
useDispatch( blockEditorStore );

if ( isValid ) {
return null;
}

const confirmSynchronization = () => {
if (
// eslint-disable-next-line no-alert
window.confirm(
__(
'Resetting the template may result in loss of content, do you want to continue?'
)
)
) {
props.synchronizeTemplate();
}
};

return (
<Notice
className="editor-template-validation-notice"
isDismissible={ false }
status="warning"
actions={ [
{
label: __( 'Keep it as is' ),
onClick: props.resetTemplateValidity,
},
{
label: __( 'Reset the template' ),
onClick: confirmSynchronization,
},
] }
>
{ __(
'The content of your post doesn’t match the template assigned to your post type.'
) }
</Notice>
<>
<Notice
className="editor-template-validation-notice"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this class name is only used in E2E tests, I am wondering if it is possible to delete it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Notices are hard to target in e2e tests. Let's keep the class.

isDismissible={ false }
status="warning"
actions={ [
{
label: __( 'Keep it as is' ),
onClick: () => setTemplateValidity( true ),
},
{
label: __( 'Reset the template' ),
onClick: () => setShowConfirmDialog( true ),
},
] }
>
{ __(
'The content of your post doesn’t match the template assigned to your post type.'
) }
</Notice>
<ConfirmDialog
isOpen={ showConfirmDialog }
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
onConfirm={ () => {
setShowConfirmDialog( false );
synchronizeTemplate();
} }
onCancel={ () => setShowConfirmDialog( false ) }
>
{ __(
'Resetting the template may result in loss of content, do you want to continue?'
) }
</ConfirmDialog>
</>
);
}

export default compose( [
withSelect( ( select ) => ( {
isValid: select( blockEditorStore ).isValidTemplate(),
} ) ),
withDispatch( ( dispatch ) => {
const { setTemplateValidity, synchronizeTemplate } =
dispatch( blockEditorStore );
return {
resetTemplateValidity: () => setTemplateValidity( true ),
synchronizeTemplate,
};
} ),
] )( TemplateValidationNotice );

This file was deleted.

1 change: 0 additions & 1 deletion packages/editor/src/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,3 @@
@import "./components/preview-dropdown/style.scss";
@import "./components/table-of-contents/style.scss";
@import "./components/template-areas/style.scss";
@import "./components/template-validation-notice/style.scss";
Loading