-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Patterns: add option to set sync status when adding from wp-admin patterns list #52352
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
929dee0
Show a modal to set sync status if adding pattern from pattern list page
glendaviesnz 4d9d97a
Make sure the modal loads if post settings panel not open
glendaviesnz 670079a
don't load modal component at all if not new post
glendaviesnz 81ddf49
Simplify the sync status so undefined always = synced
glendaviesnz 46b3c29
Update packages/editor/src/components/post-sync-status/index.js
ramonjd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
106 changes: 99 additions & 7 deletions
106
packages/editor/src/components/post-sync-status/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,35 +1,127 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useSelect } from '@wordpress/data'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { PanelRow } from '@wordpress/components'; | ||
import { | ||
PanelRow, | ||
Modal, | ||
Button, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
ToggleControl, | ||
} from '@wordpress/components'; | ||
import { useEffect, useState } from '@wordpress/element'; | ||
import { ReusableBlocksRenameHint } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { store as editorStore } from '../../store'; | ||
|
||
export default function PostSyncStatus() { | ||
const { syncStatus, postType } = useSelect( ( select ) => { | ||
const { syncStatus, postType, meta } = useSelect( ( select ) => { | ||
const { getEditedPostAttribute } = select( editorStore ); | ||
return { | ||
syncStatus: getEditedPostAttribute( 'wp_pattern_sync_status' ), | ||
meta: getEditedPostAttribute( 'meta' ), | ||
postType: getEditedPostAttribute( 'type' ), | ||
}; | ||
}, [] ); | ||
} ); | ||
|
||
if ( postType !== 'wp_block' ) { | ||
return null; | ||
} | ||
|
||
const isFullySynced = ! syncStatus; | ||
// When the post is first created, the top level wp_pattern_sync_status is not set so get meta value instead. | ||
const currentSyncStatus = | ||
meta?.wp_pattern_sync_status === 'unsynced' ? 'unsynced' : syncStatus; | ||
|
||
return ( | ||
<PanelRow className="edit-post-sync-status"> | ||
<span>{ __( 'Sync status' ) }</span> | ||
<div> | ||
{ isFullySynced ? __( 'Fully synced' ) : __( 'Not synced' ) } | ||
{ currentSyncStatus === 'unsynced' | ||
? __( 'Not synced' ) | ||
: __( 'Fully synced' ) } | ||
</div> | ||
</PanelRow> | ||
); | ||
} | ||
|
||
export function PostSyncStatusModal() { | ||
const { editPost } = useDispatch( editorStore ); | ||
const [ isModalOpen, setIsModalOpen ] = useState( false ); | ||
const [ syncType, setSyncType ] = useState( undefined ); | ||
|
||
const { postType, isNewPost } = useSelect( ( select ) => { | ||
const { getEditedPostAttribute, isCleanNewPost } = | ||
select( editorStore ); | ||
return { | ||
postType: getEditedPostAttribute( 'type' ), | ||
isNewPost: isCleanNewPost(), | ||
}; | ||
}, [] ); | ||
|
||
useEffect( () => { | ||
if ( isNewPost && postType === 'wp_block' ) { | ||
setIsModalOpen( true ); | ||
} | ||
// We only want the modal to open when the page is first loaded. | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [] ); | ||
|
||
const setSyncStatus = () => { | ||
editPost( { | ||
meta: { | ||
wp_pattern_sync_status: syncType, | ||
}, | ||
} ); | ||
}; | ||
|
||
if ( postType !== 'wp_block' || ! isNewPost ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<> | ||
{ isModalOpen && ( | ||
<Modal | ||
title={ __( 'Set pattern sync status' ) } | ||
onRequestClose={ () => { | ||
setIsModalOpen( false ); | ||
} } | ||
overlayClassName="reusable-blocks-menu-items__convert-modal" | ||
> | ||
<form | ||
onSubmit={ ( event ) => { | ||
event.preventDefault(); | ||
setIsModalOpen( false ); | ||
setSyncStatus(); | ||
} } | ||
> | ||
<VStack spacing="5"> | ||
<ReusableBlocksRenameHint /> | ||
<ToggleControl | ||
label={ __( 'Synced' ) } | ||
help={ __( | ||
'Editing the pattern will update it anywhere it is used.' | ||
) } | ||
checked={ ! syncType } | ||
onChange={ () => { | ||
setSyncType( | ||
! syncType ? 'unsynced' : undefined | ||
); | ||
} } | ||
/> | ||
<HStack justify="right"> | ||
<Button variant="primary" type="submit"> | ||
{ __( 'Create' ) } | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
</form> | ||
</Modal> | ||
) } | ||
</> | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe change the help text to match the state depending on
syncType
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to @SaxonF the toggle control standard is to always/only show the positive state help text
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough, makes sense 👍🏻 I guess it can be seen as a checkbox in that regard. Cheers!