import { __experimentalText as Text, __experimentalInputControl as InputControl, Button, Flex, FlexItem, } from '@wordpress/components'; import { check, Icon, info, rotateRight } from '@wordpress/icons'; import clsx from 'clsx'; import { useTranslate } from 'i18n-calypso'; import { useState, useCallback, useEffect } from 'react'; import { useScheduledUpdatesVerifyPathQuery } from 'calypso/data/plugins/use-scheduled-updates-verify-path-query'; import { useSelector } from 'calypso/state'; import getSiteId from 'calypso/state/sites/selectors/get-site-id'; import { MAX_SELECTABLE_PATHS } from './config'; import { useSiteSlug } from './hooks/use-site-slug'; import { prepareRelativePath, validatePath } from './schedule-form.helper'; import type { PathsOnChangeEvent } from './types'; interface Props { paths?: string[]; onChange?: ( data: PathsOnChangeEvent ) => void; borderWrapper?: boolean; error?: string; showError?: boolean; onTouch?: ( touched: boolean ) => void; } export function ScheduleFormPaths( props: Props ) { const translate = useTranslate(); const siteSlug = useSiteSlug(); const siteId = useSelector( ( state ) => getSiteId( state, siteSlug ) ); const { paths: initPaths = [], onChange, borderWrapper = true, error, showError = false, onTouch, } = props; const [ paths, setPaths ] = useState( initPaths ); const [ newPath, setNewPath ] = useState( '' ); const [ newPathError, setNewPathError ] = useState( '' ); const [ newPathSubmitted, setNewPathSubmitted ] = useState( false ); const { data: verificationData, isFetching: isVerifying, isFetched: isVerified, } = useScheduledUpdatesVerifyPathQuery( siteId as number, newPath, { enabled: newPathSubmitted && !! newPath && !! siteId, } ); const pathAvailable = verificationData?.available; /** * Callbacks */ const resetFormState = useCallback( () => { setNewPath( '' ); setNewPathError( '' ); setNewPathSubmitted( false ); }, [] ); const addPath = useCallback( () => { if ( newPathSubmitted && ! newPathError && pathAvailable ) { setPaths( [ ...paths, newPath ] ); resetFormState(); } }, [ newPath, paths, newPathSubmitted, newPathError, pathAvailable ] ); const removePath = useCallback( ( index: number ) => { setPaths( paths.filter( ( _, i ) => i !== index ) ); }, [ paths ] ); const handleAsyncValidationError = useCallback( () => { if ( newPathSubmitted && isVerified && ! pathAvailable ) { setNewPathError( translate( 'This path is not available.' ) ); } }, [ newPathSubmitted, isVerified, pathAvailable ] ); const onNewPathSubmit = useCallback( () => { const validationErrors = validatePath( newPath, paths ); ! validationErrors && setNewPathSubmitted( true ); setNewPathError( validationErrors ); }, [ newPath, paths, newPathError ] ); /** * Effects */ useEffect( addPath, [ addPath ] ); useEffect( handleAsyncValidationError, [ handleAsyncValidationError ] ); useEffect( () => { setNewPathSubmitted( false ); onTouch?.( false ); }, [ newPath ] ); useEffect( () => onChange?.( { paths, hasUnsubmittedPath: newPath.length > 0 } ), [ paths, newPath, newPathSubmitted ] ); return (