File size: 5,888 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
import {
	__experimentalText as Text,
	CheckboxControl,
	SearchControl,
	Spinner,
} from '@wordpress/components';
import { Icon, info } from '@wordpress/icons';
import clsx from 'clsx';
import { useTranslate } from 'i18n-calypso';
import { Fragment, useCallback, useEffect, useState, useMemo } from 'react';
import { MAX_SELECTABLE_PLUGINS } from './config';
import type { CorePlugin } from 'calypso/data/plugins/types';

interface Props {
	plugins?: CorePlugin[];
	isPluginsFetching: boolean;
	isPluginsFetched: boolean;
	selectedPlugins?: string[];
	touched?: boolean;
	error?: string;
	showError?: boolean;
	onTouch?: ( touched: boolean ) => void;
	onChange?: ( value: string[] ) => void;
	borderWrapper?: boolean;
	selectedSites?: number[] | null;
}
export function ScheduleFormPlugins( props: Props ) {
	const {
		selectedPlugins: initSelectedPlugins = [],
		isPluginsFetching,
		isPluginsFetched,
		error,
		showError,
		onChange,
		onTouch,
		borderWrapper = true,
		selectedSites = null,
	} = props;
	const translate = useTranslate();
	const plugins = useMemo( () => props.plugins || [], [ props.plugins ] );
	const pluginsAvailable = props.plugins !== undefined || isPluginsFetching;

	const [ pluginSearchTerm, setPluginSearchTerm ] = useState( '' );
	const [ selectedPlugins, setSelectedPlugins ] = useState< string[] >( initSelectedPlugins );
	const [ fieldTouched, setFieldTouched ] = useState( false );

	const removeUnlistedSelectedPlugins = useCallback( () => {
		if ( isPluginsFetched ) {
			setSelectedPlugins(
				selectedPlugins.filter( ( plugin ) => plugins.find( ( p ) => p.plugin === plugin ) )
			);
		}
	}, [ plugins, isPluginsFetched ] );
	const onPluginSelectionChange = useCallback(
		( plugin: CorePlugin, isChecked: boolean ) => {
			if ( isChecked ) {
				const _plugins: string[] = [ ...selectedPlugins ];
				_plugins.push( plugin.plugin );
				setSelectedPlugins( _plugins );
			} else {
				setSelectedPlugins( selectedPlugins.filter( ( name ) => name !== plugin.plugin ) );
			}
		},
		[ selectedPlugins ]
	);

	const onPluginSelectAllChange = useCallback(
		( isChecked: boolean ) => {
			isChecked
				? setSelectedPlugins( plugins.map( ( plugin ) => plugin.plugin ) ?? [] )
				: setSelectedPlugins( [] );
		},
		[ plugins ]
	);

	const isPluginSelectionDisabled = useCallback(
		( plugin: CorePlugin ) => {
			return (
				selectedPlugins.length >= MAX_SELECTABLE_PLUGINS &&
				! selectedPlugins.includes( plugin.plugin )
			);
		},
		[ selectedPlugins ]
	);

	useEffect( () => onTouch?.( fieldTouched ), [ fieldTouched ] );
	useEffect( () => onChange?.( selectedPlugins ), [ selectedPlugins ] );
	useEffect( () => removeUnlistedSelectedPlugins(), [ plugins, isPluginsFetched ] );

	return (
		<div className="form-field form-field--plugins">
			<label htmlFor="plugins">{ translate( 'Select plugins' ) }</label>
			{ pluginsAvailable && (
				<span className="plugin-select-stats">
					{ selectedPlugins.length }/
					{ plugins.length < MAX_SELECTABLE_PLUGINS ? plugins.length : MAX_SELECTABLE_PLUGINS }
				</span>
			) }

			{ pluginsAvailable && (
				<Text className="info-msg">
					{ translate( 'Plugins not listed below are automatically updated by WordPress.com.' ) }
				</Text>
			) }

			<div className={ clsx( { 'form-control-container': borderWrapper } ) }>
				{ pluginsAvailable && (
					<>
						<SearchControl
							id="plugins"
							onChange={ setPluginSearchTerm }
							value={ pluginSearchTerm }
							placeholder={ translate( 'Search plugins' ) }
						/>
						<div className="checkbox-options-container">
							{ isPluginsFetching && <Spinner /> }
							{ isPluginsFetched &&
								plugins.length !== 0 &&
								plugins.length <= MAX_SELECTABLE_PLUGINS && (
									<CheckboxControl
										label={ translate( 'Select all' ) }
										indeterminate={
											selectedPlugins.length > 0 && selectedPlugins.length < plugins.length
										}
										checked={ selectedPlugins.length === plugins.length }
										onChange={ onPluginSelectAllChange }
									/>
								) }
							{ isPluginsFetched &&
								plugins.map( ( plugin ) => (
									<Fragment key={ plugin.plugin }>
										{ plugin.name.toLowerCase().includes( pluginSearchTerm.toLowerCase() ) && (
											<CheckboxControl
												key={ plugin.plugin }
												label={ plugin.name }
												checked={ selectedPlugins.includes( plugin.plugin ) }
												disabled={ isPluginSelectionDisabled( plugin ) }
												className={ clsx( {
													disabled: isPluginSelectionDisabled( plugin ),
												} ) }
												onChange={ ( isChecked ) => {
													onPluginSelectionChange( plugin, isChecked );
													setFieldTouched( true );
												} }
											/>
										) }
									</Fragment>
								) ) }
						</div>
					</>
				) }
				{ ! pluginsAvailable && selectedSites && selectedSites.length === 0 && (
					<p className="placeholder-info">
						{ translate( 'Please select a site to view available plugins.' ) }
					</p>
				) }
				{ ! pluginsAvailable && selectedSites && selectedSites.length > 0 && (
					<p className="placeholder-info">{ translate( 'No plugins to update.' ) }</p>
				) }
			</div>
			{ ( () => {
				if ( ( showError && error ) || ( fieldTouched && error ) ) {
					return (
						<Text className="validation-msg">
							<Icon className="icon-info" icon={ info } size={ 16 } />
							{ error }
						</Text>
					);
				} else if ( isPluginsFetched && plugins.length === 0 ) {
					return (
						<Text className="validation-msg">
							<Icon className="icon-info" icon={ info } size={ 16 } />
							{ translate(
								'All installed plugins are provided by WordPress.com and automatically updated for you. Add a plugin from the WordPress.com Marketplace to create a schedule!'
							) }
						</Text>
					);
				}
			} )() }
		</div>
	);
}