import { recordTracksEvent } from '@automattic/calypso-analytics'; import { useMutationState } from '@tanstack/react-query'; import { __experimentalText as Text, Button, Icon, Card, CardHeader, CardBody, CardFooter, } from '@wordpress/components'; import { info, arrowLeft } from '@wordpress/icons'; import { useTranslate } from 'i18n-calypso'; import { useState } from 'react'; import { useUpdateScheduleQuery } from 'calypso/data/plugins/use-update-schedules-query'; import { scrollToTop } from '../plugin-scheduled-updates-common/utils'; import { useCanCreateSchedules } from './hooks/use-can-create-schedules'; import { useIsEligibleForFeature } from './hooks/use-is-eligible-for-feature'; import { useSiteSlug } from './hooks/use-site-slug'; import { ScheduleForm } from './schedule-form'; import type { SyncSuccessParams } from './types'; interface Props { scheduleId?: string; onNavBack?: () => void; } export const ScheduleEdit = ( props: Props ) => { const siteSlug = useSiteSlug(); const translate = useTranslate(); const { scheduleId, onNavBack } = props; const { isEligibleForFeature } = useIsEligibleForFeature(); const { data: schedules = [], isFetched } = useUpdateScheduleQuery( siteSlug, isEligibleForFeature ); const schedule = schedules.find( ( s ) => s.id === scheduleId ); const pendingMutations = useMutationState( { filters: { mutationKey: [ 'edit-update-schedule', siteSlug ], status: 'pending' }, } ); const isBusy = pendingMutations.length > 0; const [ syncError, setSyncError ] = useState( '' ); const { canCreateSchedules, errors: eligibilityCheckErrors } = useCanCreateSchedules( siteSlug, isEligibleForFeature ); // If the schedule is not found, navigate back to the list if ( isFetched && ! schedule ) { onNavBack && onNavBack(); return null; } const onSyncSuccess = ( params: SyncSuccessParams ) => { recordTracksEvent( 'calypso_scheduled_updates_edit_schedule', { site_slug: siteSlug, frequency: params.frequency, plugins_number: params.plugins.length, hours: params.hours, weekday: params.weekday, } ); setSyncError( '' ); scrollToTop(); return onNavBack && onNavBack(); }; return (
{ onNavBack && ( ) }
{ translate( 'Edit Schedule' ) }
{ schedule && ( ) } { ( ( ! canCreateSchedules && eligibilityCheckErrors?.length ) || syncError ) && ( { ( eligibilityCheckErrors?.length && eligibilityCheckErrors[ 0 ].message ) || '' } { syncError } ) }
); };