File size: 6,541 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 202 203 |
import { APIError } from '@automattic/data-stores';
import { useBreakpoint } from '@automattic/viewport-react';
import { __experimentalText as Text } from '@wordpress/components';
import { useTranslate } from 'i18n-calypso';
import { useState, useEffect } from 'react';
import { handleErrorMessage } from 'calypso/blocks/plugin-scheduled-updates-common/error-utils';
import { useCorePluginsQuery } from 'calypso/data/plugins/use-core-plugins-query';
import {
useCreateUpdateScheduleMutation,
useEditUpdateScheduleMutation,
} from 'calypso/data/plugins/use-update-schedules-mutation';
import {
useUpdateScheduleQuery,
ScheduleUpdates,
} from 'calypso/data/plugins/use-update-schedules-query';
import { useIsEligibleForFeature } from './hooks/use-is-eligible-for-feature';
import { useSiteSlug } from './hooks/use-site-slug';
import { ScheduleFormFrequency } from './schedule-form-frequency';
import { ScheduleFormPaths } from './schedule-form-paths';
import { ScheduleFormPlugins } from './schedule-form-plugins';
import { validatePaths, validatePlugins, validateTimeSlot } from './schedule-form.helper';
import type { PathsOnChangeEvent, SyncSuccessParams } from './types';
import './schedule-form.scss';
interface Props {
scheduleForEdit?: ScheduleUpdates;
onSyncSuccess?: ( params: SyncSuccessParams ) => void;
onSyncError?: ( error: string ) => void;
}
export const ScheduleForm = ( props: Props ) => {
const siteSlug = useSiteSlug();
const translate = useTranslate();
const isSmallScreen = useBreakpoint( '<660px' );
const { isEligibleForFeature } = useIsEligibleForFeature();
const { scheduleForEdit, onSyncSuccess, onSyncError } = props;
const { data: schedulesData = [] } = useUpdateScheduleQuery( siteSlug, isEligibleForFeature );
const schedules = schedulesData.filter( ( s ) => s.id !== scheduleForEdit?.id ) ?? [];
const [ selectedPlugins, setSelectedPlugins ] = useState< string[] >(
scheduleForEdit?.args || []
);
const [ frequency, setFrequency ] = useState< 'daily' | 'weekly' >(
scheduleForEdit?.schedule || 'daily'
);
const [ timestamp, setTimestamp ] = useState< number >(
scheduleForEdit ? scheduleForEdit?.timestamp * 1000 : Date.now()
);
const [ healthCheckPaths, setHealthCheckPaths ] = useState< string[] >(
scheduleForEdit?.health_check_paths || []
);
const [ hasUnsubmittedPath, setHasUnsubmittedPath ] = useState( false );
const scheduledTimeSlots = schedules.map( ( schedule ) => ( {
timestamp: schedule.timestamp,
frequency: schedule.schedule,
} ) );
const scheduledPlugins = schedules.map( ( schedule ) => schedule.args );
const [ validationErrors, setValidationErrors ] = useState< Record< string, string > >( {
plugins: validatePlugins( selectedPlugins, scheduledPlugins ),
timestamp: validateTimeSlot( { frequency, timestamp }, scheduledTimeSlots ),
paths: validatePaths( hasUnsubmittedPath ),
} );
const [ fieldTouched, setFieldTouched ] = useState< Record< string, boolean > >( {} );
const serverSyncCallbacks = {
onSuccess: () => {
const date = new Date( timestamp * 1000 );
onSyncSuccess?.( {
plugins: selectedPlugins,
frequency,
hours: date.getHours(),
weekday: frequency === 'weekly' ? date.getDay() : undefined,
} );
},
onError: ( e: APIError ) => onSyncError && onSyncError( handleErrorMessage( e ) ),
};
const { createUpdateSchedule } = useCreateUpdateScheduleMutation( siteSlug, serverSyncCallbacks );
const { editUpdateSchedule } = useEditUpdateScheduleMutation( siteSlug, serverSyncCallbacks );
const {
data: plugins = [],
isLoading: isPluginsFetching,
isFetched: isPluginsFetched,
} = useCorePluginsQuery( siteSlug, true, true );
const onFormSubmit = () => {
const formValid = ! Object.values( validationErrors ).filter( ( e ) => !! e ).length;
setFieldTouched( {
plugins: true,
timestamp: true,
paths: true,
} );
const params = {
plugins: selectedPlugins,
schedule: {
timestamp,
interval: frequency,
// Temporary: this field is left here for backward compatibility.
// Will be removed after https://github.com/Automattic/jetpack/pull/37223 is landed.
health_check_paths: healthCheckPaths,
},
health_check_paths: healthCheckPaths,
};
if ( formValid ) {
scheduleForEdit
? editUpdateSchedule( scheduleForEdit.id, params )
: createUpdateSchedule( params );
}
};
// Plugin selection validation
useEffect(
() =>
setValidationErrors( {
...validationErrors,
plugins: validatePlugins( selectedPlugins, scheduledPlugins ),
} ),
[ selectedPlugins ]
);
// Time slot/timestamp validation
useEffect(
() =>
setValidationErrors( {
...validationErrors,
timestamp: validateTimeSlot( { frequency, timestamp }, scheduledTimeSlots ),
} ),
[ timestamp ]
);
// Paths validation
useEffect(
() =>
setValidationErrors( {
...validationErrors,
paths: validatePaths( hasUnsubmittedPath ),
} ),
[ hasUnsubmittedPath ]
);
const onPathChange = ( data: PathsOnChangeEvent ) => {
setHealthCheckPaths( data.paths );
setHasUnsubmittedPath( data.hasUnsubmittedPath );
};
return (
<form
id="schedule"
className="schedule-form"
onSubmit={ ( e ) => {
e.preventDefault();
onFormSubmit();
} }
>
<Text>{ translate( 'Step 1' ) }</Text>
<ScheduleFormFrequency
initTimestamp={ timestamp }
initFrequency={ frequency }
error={ validationErrors?.timestamp }
showError={ fieldTouched?.timestamp }
onChange={ ( frequency, timestamp ) => {
setTimestamp( timestamp );
setFrequency( frequency );
} }
onTouch={ ( touched ) => {
setFieldTouched( { ...fieldTouched, timestamp: touched } );
} }
/>
<Text>{ translate( 'Step 2' ) }</Text>
<ScheduleFormPlugins
plugins={ plugins }
selectedPlugins={ selectedPlugins }
isPluginsFetching={ isPluginsFetching }
isPluginsFetched={ isPluginsFetched }
borderWrapper={ ! isSmallScreen }
error={ validationErrors?.plugins }
showError={ fieldTouched?.plugins }
onChange={ setSelectedPlugins }
onTouch={ ( touched ) => {
setFieldTouched( { ...fieldTouched, plugins: touched } );
} }
/>
<Text>{ translate( 'Step 3' ) }</Text>
<ScheduleFormPaths
paths={ healthCheckPaths }
borderWrapper={ false }
onChange={ onPathChange }
error={ validationErrors?.paths }
showError={ fieldTouched?.paths }
onTouch={ ( touched ) => {
setFieldTouched( { ...fieldTouched, paths: touched } );
} }
/>
</form>
);
};
|