Skip to content

Commit

Permalink
Editor: Use the 'ConfirmDialog' component in template validation noti…
Browse files Browse the repository at this point in the history
…ce (WordPress#60385)


Unlinked contributors: aduth.

Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: t-hamano <[email protected]>
Co-authored-by: kjellr <[email protected]>
Co-authored-by: noisysocks <[email protected]>
Co-authored-by: fullofcaffeine <[email protected]>
  • Loading branch information
6 people authored and cbravobernal committed Apr 9, 2024
1 parent 32d7f93 commit 10f6b12
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 60 deletions.
98 changes: 48 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,61 @@
/**
* 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 select( blockEditorStore ).isValidTemplate();
}, [] );
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"
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 }
confirmButtonText={ __( 'Reset' ) }
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";

0 comments on commit 10f6b12

Please sign in to comment.