{"version":3,"file":"rename-pattern-category-modal.cjs","names":["useId","useRef","useState","decodeEntities","useDispatch","coreStore","noticesStore","__","CATEGORY_SLUG","Modal","Stack","TextControl","Button"],"sources":["../../src/components/rename-pattern-category-modal.tsx"],"sourcesContent":["import { Modal, Button, TextControl } from '@wordpress/components';\nimport { Stack } from '@wordpress/ui';\nimport { store as coreStore } from '@wordpress/core-data';\nimport type { Term } from '@wordpress/core-data';\nimport { useDispatch } from '@wordpress/data';\nimport { useId, useRef, useState } from '@wordpress/element';\nimport { decodeEntities } from '@wordpress/html-entities';\nimport { __ } from '@wordpress/i18n';\nimport { store as noticesStore } from '@wordpress/notices';\nimport { speak } from '@wordpress/a11y';\nimport type { FormEvent, RefObject } from 'react';\nimport type { RestError } from '../types';\nimport { CATEGORY_SLUG } from './category-selector';\nimport type { RenamePatternCategoryModalProps } from './types';\n\nexport default function RenamePatternCategoryModal( {\n\tcategory,\n\texistingCategories,\n\tonClose,\n\tonError,\n\tonSuccess,\n\t...props\n}: RenamePatternCategoryModalProps ) {\n\tconst id = useId();\n\tconst textControlRef = useRef< HTMLInputElement >( undefined );\n\tconst [ name, setName ] = useState( decodeEntities( category.name ) );\n\tconst [ isSaving, setIsSaving ] = useState( false );\n\tconst [ validationMessage, setValidationMessage ] = useState<\n\t\tstring | false | undefined\n\t>( false );\n\tconst validationMessageId = validationMessage\n\t\t? `patterns-rename-pattern-category-modal__validation-message-${ id }`\n\t\t: undefined;\n\n\tconst { saveEntityRecord, invalidateResolution } = useDispatch( coreStore );\n\tconst { createErrorNotice, createSuccessNotice } =\n\t\tuseDispatch( noticesStore );\n\n\tconst onChange = ( newName: string ) => {\n\t\tif ( validationMessage ) {\n\t\t\tsetValidationMessage( undefined );\n\t\t}\n\t\tsetName( newName );\n\t};\n\n\tconst onSave = async ( event: FormEvent ) => {\n\t\tevent.preventDefault();\n\n\t\tif ( isSaving ) {\n\t\t\treturn;\n\t\t}\n\n\t\tif ( ! name || name === category.name ) {\n\t\t\tconst message = __( 'Please enter a new name for this category.' );\n\t\t\tspeak( message, 'assertive' );\n\t\t\tsetValidationMessage( message );\n\t\t\ttextControlRef.current?.focus();\n\t\t\treturn;\n\t\t}\n\n\t\t// Check existing categories to avoid creating duplicates.\n\t\tif (\n\t\t\texistingCategories.patternCategories.find( ( existingCategory ) => {\n\t\t\t\t// Compare the id so that the we don't disallow the user changing the case of their current category\n\t\t\t\t// (i.e. renaming 'test' to 'Test').\n\t\t\t\treturn (\n\t\t\t\t\texistingCategory.id !== category.id &&\n\t\t\t\t\texistingCategory.label.toLowerCase() === name.toLowerCase()\n\t\t\t\t);\n\t\t\t} )\n\t\t) {\n\t\t\tconst message = __(\n\t\t\t\t'This category already exists. Please use a different name.'\n\t\t\t);\n\t\t\tspeak( message, 'assertive' );\n\t\t\tsetValidationMessage( message );\n\t\t\ttextControlRef.current?.focus();\n\t\t\treturn;\n\t\t}\n\n\t\ttry {\n\t\t\tsetIsSaving( true );\n\n\t\t\t// User pattern category properties may differ as they can be\n\t\t\t// normalized for use alongside template part areas, core pattern\n\t\t\t// categories etc. As a result we won't just destructure the passed\n\t\t\t// category object.\n\t\t\tconst savedRecord = ( await saveEntityRecord(\n\t\t\t\t'taxonomy',\n\t\t\t\tCATEGORY_SLUG,\n\t\t\t\t{\n\t\t\t\t\tid: category.id,\n\t\t\t\t\tslug: category.slug,\n\t\t\t\t\tname,\n\t\t\t\t},\n\t\t\t\t{ throwOnError: true }\n\t\t\t) ) as Term< 'edit' >;\n\n\t\t\tinvalidateResolution( 'getUserPatternCategories' );\n\t\t\tonSuccess?.( savedRecord );\n\n\t\t\tcreateSuccessNotice( __( 'Pattern category renamed.' ), {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'pattern-category-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: __(\n\t\t\t\t\t\t\t'An error occurred while renaming the pattern category.'\n\t\t\t\t\t\t);\n\n\t\t\tcreateErrorNotice( errorMessage, {\n\t\t\t\ttype: 'snackbar',\n\t\t\t\tid: 'pattern-category-update',\n\t\t\t} );\n\t\t} finally {\n\t\t\tsetIsSaving( false );\n\t\t\tsetName( '' );\n\t\t\tonClose();\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\tonRequestClose={ onRequestClose }\n\t\t\t{ ...props }\n\t\t>\n\t\t\t<form onSubmit={ onSave }>\n\t\t\t\t<Stack direction=\"column\" gap=\"lg\">\n\t\t\t\t\t<Stack direction=\"column\" gap=\"sm\">\n\t\t\t\t\t\t<TextControl\n\t\t\t\t\t\t\t// React only assigns the input or `null` to the ref.\n\t\t\t\t\t\t\tref={\n\t\t\t\t\t\t\t\ttextControlRef as RefObject< HTMLInputElement | null >\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tlabel={ __( 'Name' ) }\n\t\t\t\t\t\t\tvalue={ name }\n\t\t\t\t\t\t\tonChange={ onChange }\n\t\t\t\t\t\t\taria-describedby={ validationMessageId }\n\t\t\t\t\t\t\trequired\n\t\t\t\t\t\t/>\n\t\t\t\t\t\t{ validationMessage && (\n\t\t\t\t\t\t\t<span\n\t\t\t\t\t\t\t\tclassName=\"patterns-rename-pattern-category-modal__validation-message\"\n\t\t\t\t\t\t\t\tid={ validationMessageId }\n\t\t\t\t\t\t\t>\n\t\t\t\t\t\t\t\t{ validationMessage }\n\t\t\t\t\t\t\t</span>\n\t\t\t\t\t\t) }\n\t\t\t\t\t</Stack>\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\taria-disabled={\n\t\t\t\t\t\t\t\t! name || name === category.name || isSaving\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tisBusy={ isSaving }\n\t\t\t\t\t\t>\n\t\t\t\t\t\t\t{ __( 'Save' ) }\n\t\t\t\t\t\t</Button>\n\t\t\t\t\t</Stack>\n\t\t\t\t</Stack>\n\t\t\t</form>\n\t\t</Modal>\n\t);\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAeA,SAAwB,2BAA4B,EACnD,UACA,oBACA,SACA,SACA,WACA,GAAG,SACiC;CACpC,MAAM,MAAA,GAAKA,mBAAAA,MAAAA,CAAM;CACjB,MAAM,kBAAA,GAAiBC,mBAAAA,OAAAA,CAA4B,KAAA,CAAU;CAC7D,MAAM,CAAE,MAAM,YAAA,GAAYC,mBAAAA,SAAAA,EAAAA,GAAUC,yBAAAA,eAAAA,CAAgB,SAAS,IAAK,CAAE;CACpE,MAAM,CAAE,UAAU,gBAAA,GAAgBD,mBAAAA,SAAAA,CAAU,KAAM;CAClD,MAAM,CAAE,mBAAmB,yBAAA,GAAyBA,mBAAAA,SAAAA,CAEjD,KAAM;CACT,MAAM,sBAAsB,oBACzB,8DAA+D,OAC/D,KAAA;CAEH,MAAM,EAAE,kBAAkB,0BAAA,GAAyBE,gBAAAA,YAAAA,CAAaC,qBAAAA,KAAU;CAC1E,MAAM,EAAE,mBAAmB,yBAAA,GAC1BD,gBAAAA,YAAAA,CAAaE,mBAAAA,KAAa;CAE3B,MAAM,YAAa,YAAqB;EACvC,IAAK,mBACJ,qBAAsB,KAAA,CAAU;EAEjC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