File size: 6,107 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
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 (
<div className="form-field form-field--paths">
<label htmlFor="paths">{ translate( 'Test URL paths' ) }</label>
<Text className="info-msg">
{
/* translators: maxPaths is a number, e.g. 5 */
translate(
'Your schedule will test your front page for errors. Add up to %(maxPaths)s additional paths to test:',
{
args: { maxPaths: MAX_SELECTABLE_PATHS },
}
)
}
</Text>
<div className={ clsx( { 'form-control-container': borderWrapper } ) }>
<Text className="info-msg">Website URL paths</Text>
<div className="paths">
<Flex className="path" gap={ 2 }>
<FlexItem isBlock>
<InputControl value={ siteSlug } size="__unstable-large" readOnly />
</FlexItem>
<FlexItem>
<Button disabled icon={ check } __next40pxDefaultSize />
</FlexItem>
</Flex>
{ paths.map( ( path, i ) => (
<Flex className="path" gap={ 2 } key={ i }>
<FlexItem isBlock>
<InputControl value={ path } size="__unstable-large" readOnly />
</FlexItem>
<FlexItem>
<Button __next40pxDefaultSize onClick={ () => removePath( i ) } variant="secondary">
{ translate( 'Remove' ) }
</Button>
</FlexItem>
</Flex>
) ) }
</div>
{ paths.length < MAX_SELECTABLE_PATHS && (
<div className="new-path">
<Flex gap={ 2 }>
<FlexItem isBlock>
<InputControl
value={ newPath }
size="__unstable-large"
autoComplete="off"
placeholder={ translate( '/add-path' ) }
onChange={ ( p ) => setNewPath( p || '' ) }
onKeyPress={ ( e ) => {
if ( e.key === 'Enter' ) {
// Prevent form submission on Enter key press
e.preventDefault();
onNewPathSubmit();
}
} }
onPaste={ ( e ) => {
if ( ! newPath ) {
e.preventDefault();
const value = e.clipboardData.getData( 'text' );
setNewPath( prepareRelativePath( value ) );
}
} }
/>
</FlexItem>
<FlexItem>
<Button
className={ clsx( { 'is-verifying': isVerifying } ) }
icon={ isVerifying ? rotateRight : null }
disabled={ isVerifying }
variant="secondary"
onClick={ onNewPathSubmit }
__next40pxDefaultSize
>
{ translate( 'Add' ) }
</Button>
</FlexItem>
</Flex>
</div>
) }
</div>
{ newPathError && <Text className="validation-msg">{ newPathError }</Text> }
{ MAX_SELECTABLE_PATHS === paths.length && (
<Text className="info-msg">
{ translate( 'You reached the maximum number of paths.' ) }
</Text>
) }
{ error && showError ? (
<Text className="validation-msg">
<Icon className="icon-info" icon={ info } size={ 16 } />
{ error }
</Text>
) : null }
</div>
);
}
|