import { Modal, Button, TextControl } from '@wordpress/components'; import { Stack } from '@wordpress/ui'; import { store as coreStore } from '@wordpress/core-data'; import type { Term } from '@wordpress/core-data'; import { useDispatch } from '@wordpress/data'; import { useId, useRef, useState } from '@wordpress/element'; import { decodeEntities } from '@wordpress/html-entities'; import { __ } from '@wordpress/i18n'; import { store as noticesStore } from '@wordpress/notices'; import { speak } from '@wordpress/a11y'; import type { FormEvent, RefObject } from 'react'; import type { RestError } from '../types'; import { CATEGORY_SLUG } from './category-selector'; import type { RenamePatternCategoryModalProps } from './types'; export default function RenamePatternCategoryModal( { category, existingCategories, onClose, onError, onSuccess, ...props }: RenamePatternCategoryModalProps ) { const id = useId(); const textControlRef = useRef< HTMLInputElement >( undefined ); const [ name, setName ] = useState( decodeEntities( category.name ) ); const [ isSaving, setIsSaving ] = useState( false ); const [ validationMessage, setValidationMessage ] = useState< string | false | undefined >( false ); const validationMessageId = validationMessage ? `patterns-rename-pattern-category-modal__validation-message-${ id }` : undefined; const { saveEntityRecord, invalidateResolution } = useDispatch( coreStore ); const { createErrorNotice, createSuccessNotice } = useDispatch( noticesStore ); const onChange = ( newName: string ) => { if ( validationMessage ) { setValidationMessage( undefined ); } setName( newName ); }; const onSave = async ( event: FormEvent ) => { event.preventDefault(); if ( isSaving ) { return; } if ( ! name || name === category.name ) { const message = __( 'Please enter a new name for this category.' ); speak( message, 'assertive' ); setValidationMessage( message ); textControlRef.current?.focus(); return; } // Check existing categories to avoid creating duplicates. if ( existingCategories.patternCategories.find( ( existingCategory ) => { // Compare the id so that the we don't disallow the user changing the case of their current category // (i.e. renaming 'test' to 'Test'). return ( existingCategory.id !== category.id && existingCategory.label.toLowerCase() === name.toLowerCase() ); } ) ) { const message = __( 'This category already exists. Please use a different name.' ); speak( message, 'assertive' ); setValidationMessage( message ); textControlRef.current?.focus(); return; } try { setIsSaving( true ); // User pattern category properties may differ as they can be // normalized for use alongside template part areas, core pattern // categories etc. As a result we won't just destructure the passed // category object. const savedRecord = ( await saveEntityRecord( 'taxonomy', CATEGORY_SLUG, { id: category.id, slug: category.slug, name, }, { throwOnError: true } ) ) as Term< 'edit' >; invalidateResolution( 'getUserPatternCategories' ); onSuccess?.( savedRecord ); createSuccessNotice( __( 'Pattern category renamed.' ), { type: 'snackbar', id: 'pattern-category-update', } ); } catch ( error ) { onError?.(); // A REST API error is a plain object, not an `Error`. const restError = error as RestError; const errorMessage = restError.message && restError.code !== 'unknown_error' ? restError.message : __( 'An error occurred while renaming the pattern category.' ); createErrorNotice( errorMessage, { type: 'snackbar', id: 'pattern-category-update', } ); } finally { setIsSaving( false ); setName( '' ); onClose(); } }; const onRequestClose = () => { onClose(); setName( '' ); }; return (
} label={ __( 'Name' ) } value={ name } onChange={ onChange } aria-describedby={ validationMessageId } required /> { validationMessage && ( { validationMessage } ) }
); }