-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fff451d
commit ebdfaf6
Showing
6 changed files
with
194 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import PatternRenameModal from './rename'; | ||
|
||
export const PATTERN_MODALS = { | ||
rename: 'edit-site/pattern-rename', | ||
}; | ||
|
||
export default function PatternModal() { | ||
// Further modals are likely | ||
// e.g. duplicating and switching up sync status etc. | ||
return <PatternRenameModal />; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useDispatch, useSelect } from '@wordpress/data'; | ||
import { store as interfaceStore } from '@wordpress/interface'; | ||
import { privateApis as patternsPrivateApis } from '@wordpress/patterns'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PATTERN_MODALS } from './'; | ||
import { unlock } from '../../lock-unlock'; | ||
import useEditedEntityRecord from '../use-edited-entity-record'; | ||
|
||
const { RenamePatternModal } = unlock( patternsPrivateApis ); | ||
|
||
export default function PatternRenameModal() { | ||
const { record: pattern } = useEditedEntityRecord(); | ||
const { closeModal } = useDispatch( interfaceStore ); | ||
const isActive = useSelect( ( select ) => | ||
select( interfaceStore ).isModalActive( PATTERN_MODALS.rename ) | ||
); | ||
|
||
if ( ! isActive ) { | ||
return null; | ||
} | ||
|
||
return <RenamePatternModal onClose={ closeModal } pattern={ pattern } />; | ||
} |
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
115 changes: 115 additions & 0 deletions
115
packages/patterns/src/components/rename-pattern-modal.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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
Button, | ||
Modal, | ||
TextControl, | ||
__experimentalHStack as HStack, | ||
__experimentalVStack as VStack, | ||
} from '@wordpress/components'; | ||
import { store as coreStore } from '@wordpress/core-data'; | ||
import { useDispatch } from '@wordpress/data'; | ||
import { useState } from '@wordpress/element'; | ||
import { decodeEntities } from '@wordpress/html-entities'; | ||
import { __ } from '@wordpress/i18n'; | ||
import { store as noticesStore } from '@wordpress/notices'; | ||
|
||
export default function RenamePatternModal( { | ||
onClose, | ||
onError, | ||
onSuccess, | ||
pattern, | ||
...props | ||
} ) { | ||
const originalName = decodeEntities( pattern.title ); | ||
const [ name, setName ] = useState( originalName ); | ||
const [ isSaving, setIsSaving ] = useState( false ); | ||
|
||
const { | ||
editEntityRecord, | ||
__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits, | ||
} = useDispatch( coreStore ); | ||
|
||
const { createSuccessNotice, createErrorNotice } = | ||
useDispatch( noticesStore ); | ||
|
||
const onRename = async ( event ) => { | ||
event.preventDefault(); | ||
|
||
if ( ! name || name === pattern.title || isSaving ) { | ||
return; | ||
} | ||
|
||
try { | ||
await editEntityRecord( 'postType', pattern.type, pattern.id, { | ||
title: name, | ||
} ); | ||
|
||
setIsSaving( true ); | ||
setName( '' ); | ||
onClose?.(); | ||
|
||
const savedRecord = await saveSpecifiedEntityEdits( | ||
'postType', | ||
pattern.type, | ||
pattern.id, | ||
[ 'title' ], | ||
{ throwOnError: true } | ||
); | ||
|
||
onSuccess?.( savedRecord ); | ||
|
||
createSuccessNotice( __( 'Pattern renamed' ), { | ||
type: 'snackbar', | ||
id: 'pattern-update', | ||
} ); | ||
} catch ( error ) { | ||
onError?.(); | ||
|
||
const errorMessage = | ||
error.message && error.code !== 'unknown_error' | ||
? error.message | ||
: __( 'An error occurred while reverting the pattern.' ); | ||
|
||
createErrorNotice( errorMessage, { | ||
type: 'snackbar', | ||
id: 'pattern-update', | ||
} ); | ||
} finally { | ||
setIsSaving( false ); | ||
setName( '' ); | ||
} | ||
}; | ||
|
||
const onRequestClose = () => { | ||
onClose?.(); | ||
setName( '' ); | ||
}; | ||
|
||
return ( | ||
<Modal title={ __( 'Rename' ) } { ...props } onRequestClose={ onClose }> | ||
<form onSubmit={ onRename }> | ||
<VStack spacing="5"> | ||
<TextControl | ||
__nextHasNoMarginBottom | ||
label={ __( 'Name' ) } | ||
value={ name } | ||
onChange={ setName } | ||
required | ||
/> | ||
|
||
<HStack justify="right"> | ||
<Button variant="tertiary" onClick={ onRequestClose }> | ||
{ __( 'Cancel' ) } | ||
</Button> | ||
|
||
<Button variant="primary" type="submit"> | ||
{ __( 'Save' ) } | ||
</Button> | ||
</HStack> | ||
</VStack> | ||
</form> | ||
</Modal> | ||
); | ||
} |
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