File size: 4,106 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
import { recordTracksEvent } from '@automattic/calypso-analytics';
import page from '@automattic/calypso-router';
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 { useEffect, useState } from 'react';
import { Banner } from 'calypso/components/banner';
import { scrollToTop } from '../plugin-scheduled-updates-common/utils';
import { useCanCreateSchedules } from './hooks/use-can-create-schedules';
import { useCreateMonitor } from './hooks/use-create-monitor';
import { useIsEligibleForFeature } from './hooks/use-is-eligible-for-feature';
import { useSiteHasEligiblePlugins } from './hooks/use-site-has-eligible-plugins';
import { useSiteSlug } from './hooks/use-site-slug';
import { ScheduleForm } from './schedule-form';
import type { SyncSuccessParams } from './types';

interface Props {
	onNavBack?: () => void;
}
export const ScheduleCreate = ( props: Props ) => {
	const siteSlug = useSiteSlug();
	const translate = useTranslate();
	const { createMonitor } = useCreateMonitor( siteSlug );
	const { isEligibleForFeature } = useIsEligibleForFeature();
	const { siteHasEligiblePlugins, loading: siteHasEligiblePluginsLoading } =
		useSiteHasEligiblePlugins();
	const { onNavBack } = props;
	const { canCreateSchedules, errors: eligibilityCheckErrors } = useCanCreateSchedules(
		siteSlug,
		isEligibleForFeature
	);
	const pendingMutations = useMutationState( {
		filters: { mutationKey: [ 'create-update-schedule', siteSlug ], status: 'pending' },
	} );
	const isBusy = pendingMutations.length > 0;
	const [ syncError, setSyncError ] = useState( '' );

	// Redirect back to list when no eligible plugins are installed
	useEffect( () => {
		if ( ! siteHasEligiblePlugins && ! siteHasEligiblePluginsLoading ) {
			onNavBack && onNavBack();
		}
	}, [ siteHasEligiblePlugins, siteHasEligiblePluginsLoading ] );

	const onSyncSuccess = ( params: SyncSuccessParams ) => {
		recordTracksEvent( 'calypso_scheduled_updates_create_schedule', {
			site_slug: siteSlug,
			frequency: params.frequency,
			plugins_number: params.plugins.length,
			hours: params.hours,
			weekday: params.weekday,
		} );

		createMonitor();
		setSyncError( '' );
		scrollToTop();

		return onNavBack && onNavBack();
	};

	return (
		<>
			{ ! siteHasEligiblePlugins && (
				<Banner
					title={ translate( 'No plugins to update' ) }
					description={ translate(
						'You dont have any plugins that can be updated. Visit the {{a}}Plugins{{/a}} section to explore and install new plugins.',
						{
							components: {
								a: <a href={ `/plugins/${ siteSlug }` } />,
							},
						}
					) }
					onClick={ () => {
						page.show( `/plugins/${ siteSlug }` );
					} }
				/>
			) }
			<Card className="plugins-update-manager">
				<CardHeader size="extraSmall">
					<div className="ch-placeholder">
						{ onNavBack && (
							<Button icon={ arrowLeft } onClick={ onNavBack }>
								{ translate( 'Back' ) }
							</Button>
						) }
					</div>
					<Text>{ translate( 'New Schedule' ) }</Text>
					<div className="ch-placeholder"></div>
				</CardHeader>
				<CardBody>
					<ScheduleForm onSyncSuccess={ onSyncSuccess } onSyncError={ setSyncError } />
				</CardBody>
				<CardFooter>
					<Button
						form="schedule"
						type="submit"
						variant={ canCreateSchedules ? 'primary' : 'secondary' }
						disabled={ ! canCreateSchedules || ! siteHasEligiblePlugins }
						isBusy={ isBusy }
						className="schedule-form-button"
					>
						{ translate( 'Create' ) }
					</Button>
					{ ( ( ! canCreateSchedules && eligibilityCheckErrors?.length ) || syncError ) && (
						<Text as="p" className="validation-msg">
							<Icon className="icon-info" icon={ info } size={ 16 } />
							{ ( eligibilityCheckErrors?.length && eligibilityCheckErrors[ 0 ].message ) || '' }
							{ syncError }
						</Text>
					) }
				</CardFooter>
			</Card>
		</>
	);
};