forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor: Use the 'ConfirmDialog' component in template validation noti…
…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
1 parent
32d7f93
commit 10f6b12
Showing
3 changed files
with
48 additions
and
60 deletions.
There are no files selected for viewing
98 changes: 48 additions & 50 deletions
98
packages/editor/src/components/template-validation-notice/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); |
9 changes: 0 additions & 9 deletions
9
packages/editor/src/components/template-validation-notice/style.scss
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters