{"version":3,"file":"rename-pattern-modal.mjs","names":["coreStore","noticesStore"],"sources":["../../src/components/rename-pattern-modal.tsx"],"sourcesContent":["import { Button, Modal, TextControl } from '@wordpress/components';\nimport { Stack } from '@wordpress/ui';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { Block as PatternRecord } from '@wordpress/core-data';\nimport { useDispatch } from '@wordpress/data';\nimport { useState } from '@wordpress/element';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\nimport type { FormEvent } from 'react';\nimport type { RestError } from '../types';\nimport type { RenamePatternModalProps } from './types';\n\nexport default function RenamePatternModal( {\n\tonClose,\n\tonError,\n\tonSuccess,\n\tpattern,\n\t...props\n}: RenamePatternModalProps ) {\n\tconst originalName = decodeEntities( pattern.title );\n\tconst [ name, setName ] = useState( originalName );\n\tconst [ isSaving, setIsSaving ] = useState( false );\n\n\tconst {\n\t\teditEntityRecord,\n\t\t__experimentalSaveSpecifiedEntityEdits: saveSpecifiedEntityEdits,\n\t} = useDispatch( coreStore );\n\n\tconst { createSuccessNotice, createErrorNotice } =\n\t\tuseDispatch( noticesStore );\n\n\tconst onRename = async ( event: FormEvent ) => {\n\t\tevent.preventDefault();\n\n\t\tif ( ! name || name === pattern.title || isSaving ) {\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tawait editEntityRecord( 'postType', pattern.type, pattern.id, {\n\t\t\t\ttitle: name,\n\t\t\t} );\n\n\t\t\tsetIsSaving( true );\n\t\t\tsetName( '' );\n\t\t\tonClose?.();\n\n\t\t\tconst savedRecord = ( await saveSpecifiedEntityEdits(\n\t\t\t\t'postType',\n\t\t\t\tpattern.type,\n\t\t\t\tpattern.id,\n\t\t\t\t[ 'title' ],\n\t\t\t\t{ throwOnError: true }\n\t\t\t) ) as PatternRecord< 'edit' >;\n\n\t\t\tonSuccess?.( savedRecord );\n\n\t\t\tcreateSuccessNotice( __( 'Pattern renamed' ), {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'pattern-update',\n\t\t\t} );\n\t\t} catch ( error ) {\n\t\t\tonError?.();\n\n\t\t\t// A REST API error is a plain object, not an `Error`.\n\t\t\tconst restError = error as RestError;\n\t\t\tconst errorMessage =\n\t\t\t\trestError.message && restError.code !== 'unknown_error'\n\t\t\t\t\t? restError.message\n\t\t\t\t\t: __( 'An error occurred while renaming the pattern.' );\n\n\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'pattern-update',\n\t\t\t} );\n\t\t} finally {\n\t\t\tsetIsSaving( false );\n\t\t\tsetName( '' );\n\t\t}\n\t};\n\n\tconst onRequestClose = () => {\n\t\tonClose?.();\n\t\tsetName( '' );\n\t};\n\n\treturn (\n\t\t<Modal\n\t\t\ttitle={ __( 'Rename' ) }\n\t\t\t{ ...props }\n\t\t\tonRequestClose={ onClose }\n\t\t\tfocusOnMount=\"firstContentElement\"\n\t\t\tsize=\"small\"\n\t\t>\n\t\t\t<form onSubmit={ onRename }>\n\t\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t\t<TextControl\n\t\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\t\tvalue={ name }\n\t\t\t\t\t\tonChange={ setName }\n\t\t\t\t\t\trequired\n\t\t\t\t\t/>\n\t\t\t\t\t<Stack gap=\"sm\" justify=\"end\">\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\tvariant=\"tertiary\"\n\t\t\t\t\t\t\tonClick={ onRequestClose }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Cancel' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t\t<Button\n\t\t\t\t\t\t\t__next40pxDefaultSize\n\t\t\t\t\t\t\tvariant=\"primary\"\n\t\t\t\t\t\t\ttype=\"submit\"\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 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