File size: 6,492 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
import {
	__experimentalConfirmDialog as ConfirmDialog,
	Button,
	Spinner,
} from '@wordpress/components';
import { useTranslate } from 'i18n-calypso';
import { useContext, useEffect, useState } from 'react';
import { MultisitePluginUpdateManagerContext } from 'calypso/blocks/plugins-scheduled-updates-multisite/context';
import { useBatchDeleteUpdateScheduleMutation } from 'calypso/data/plugins/use-update-schedules-mutation';
import { useMultisiteUpdateScheduleQuery } from 'calypso/data/plugins/use-update-schedules-query';
import { recordTracksEvent } from 'calypso/lib/analytics/tracks';
import { SiteSlug } from 'calypso/types';
import { ScheduleErrors } from './schedule-errors';
import { ScheduleListCardNew } from './schedule-list-card-new';
import { ScheduleListCards } from './schedule-list-cards';
import { ScheduleListEmpty } from './schedule-list-empty';
import { ScheduleListFilter } from './schedule-list-filter';
import { ScheduleListTable } from './schedule-list-table';

import './styles.scss';

type Props = {
	compact?: boolean;
	previewMode: 'table' | 'card';
	showSubtitle?: boolean;
	showNewScheduleBtn?: boolean;
	selectedScheduleId?: string;
	onEditSchedule: ( id: string ) => void;
	onShowLogs: ( id: string, siteSlug: string ) => void;
	onCreateNewSchedule: () => void;
};

export const ScheduleList = ( props: Props ) => {
	const {
		compact,
		previewMode,
		showSubtitle = true,
		showNewScheduleBtn = true,
		selectedScheduleId: initSelectedScheduleId,
		onEditSchedule,
		onShowLogs,
		onCreateNewSchedule,
	} = props;
	const {
		data: schedules = [],
		isLoading: isLoadingSchedules,
		isFetched,
		refetch,
	} = useMultisiteUpdateScheduleQuery( true );
	const translate = useTranslate();
	const { searchTerm } = useContext( MultisitePluginUpdateManagerContext );
	const [ removeDialogOpen, setRemoveDialogOpen ] = useState( false );
	const [ selectedScheduleId, setSelectedScheduleId ] = useState< string | undefined >(
		initSelectedScheduleId
	);
	const selectedSchedule = schedules?.find( ( s ) => s.schedule_id === selectedScheduleId );
	const [ selectedSiteSlug, setSelectedSiteSlug ] = useState< string | undefined >();
	const [ selectedSiteSlugs, setSelectedSiteSlugs ] = useState< string[] >( [] );
	const selectedSiteSlugsForMutate = selectedSiteSlug ? [ selectedSiteSlug ] : selectedSiteSlugs;
	const selectedSiteIdsForMutate = selectedSiteSlugsForMutate
		.map( ( slug ) => selectedSchedule?.sites.find( ( site ) => site.slug === slug )?.ID )
		.filter( ( id ) => !! id ) as number[];

	useEffect( () => {
		const schedule = schedules?.find( ( schedule ) => schedule.schedule_id === selectedScheduleId );
		setSelectedSiteSlugs( schedule?.sites?.map( ( site ) => site.slug ) || [] );
	}, [ selectedScheduleId ] );
	useEffect( () => setSelectedScheduleId( initSelectedScheduleId ), [ initSelectedScheduleId ] );

	const deleteUpdateSchedules = useBatchDeleteUpdateScheduleMutation(
		[ ...selectedSiteSlugsForMutate ],
		[ ...selectedSiteIdsForMutate ],
		{
			onSuccess: () => {
				// Re-fetch again after 5 seconds
				setTimeout( () => {
					refetch();
				}, 5000 );
			},
		}
	);

	const openRemoveDialog = ( id: string, siteSlug?: SiteSlug ) => {
		setRemoveDialogOpen( true );
		setSelectedSiteSlug( siteSlug );
		setSelectedScheduleId( id );
	};

	const closeRemoveConfirm = () => {
		setRemoveDialogOpen( false );
		setSelectedScheduleId( undefined );
	};

	const onRemoveDialogConfirm = () => {
		if ( selectedSiteSlugs && selectedScheduleId ) {
			deleteUpdateSchedules.mutate( selectedScheduleId );
			recordTracksEvent( 'calypso_scheduled_updates_multisite_delete_schedule', {
				site_slugs: selectedSiteSlugsForMutate.join( ',' ),
				sites_count: selectedSiteSlugsForMutate.length,
			} );

			selectedSiteSlugsForMutate.forEach( ( siteSlug ) => {
				recordTracksEvent( 'calypso_scheduled_updates_delete_schedule', {
					site_slug: siteSlug,
				} );
			} );
		}
		closeRemoveConfirm();
	};
	const lowercasedSearchTerm = searchTerm?.toLowerCase();
	const filteredSchedules = schedules
		?.map( ( schedule ) => {
			if ( ! searchTerm || ! searchTerm.length ) {
				return schedule;
			}
			const filteredSites = schedule.sites.filter(
				( site ) =>
					site.title?.toLowerCase().includes( lowercasedSearchTerm ) ||
					site.URL?.toLowerCase().includes( lowercasedSearchTerm )
			);

			return {
				...schedule,
				sites: filteredSites,
			};
		} )
		.filter( ( schedule ) => schedule.sites.length > 0 );

	const isLoading = isLoadingSchedules;
	const ScheduleListComponent = previewMode === 'table' ? ScheduleListTable : ScheduleListCards;
	const isScheduleEmpty = schedules.length === 0 && isFetched;

	return (
		<div className="plugins-update-manager plugins-update-manager-multisite">
			<div className="plugins-update-manager-multisite__header">
				<div className="plugins-update-manager-multisite__header-main">
					<div className="plugins-update-manager-multisite__header-main-title">
						<h1>{ translate( 'Scheduled updates' ) }</h1>
						{ showSubtitle && (
							<p>
								{ translate(
									'Streamline your workflow with scheduled updates, timed to suit your needs.'
								) }
							</p>
						) }
					</div>
					{ showNewScheduleBtn && ! isScheduleEmpty && (
						<Button
							__next40pxDefaultSize={ ! compact }
							isSmall={ compact }
							variant={ compact ? 'secondary' : 'primary' }
							onClick={ onCreateNewSchedule }
							disabled={ false }
						>
							{ translate( 'New schedule' ) }
						</Button>
					) }
				</div>
			</div>

			<ScheduleErrors />
			{ isScheduleEmpty && ! compact && (
				<ScheduleListEmpty onCreateNewSchedule={ onCreateNewSchedule } />
			) }
			{ isScheduleEmpty && compact && <ScheduleListCardNew className="is-selected" /> }
			{ ! isScheduleEmpty && ScheduleListComponent ? (
				<>
					<ScheduleListFilter />
					{ ! isLoadingSchedules && (
						<ScheduleListComponent
							compact={ compact }
							schedules={ filteredSchedules }
							selectedScheduleId={ selectedScheduleId }
							onRemoveClick={ openRemoveDialog }
							onEditClick={ onEditSchedule }
							onLogsClick={ onShowLogs }
						/>
					) }
				</>
			) : null }
			{ schedules.length === 0 && isLoading && <Spinner /> }
			<ConfirmDialog
				isOpen={ removeDialogOpen }
				onConfirm={ onRemoveDialogConfirm }
				onCancel={ closeRemoveConfirm }
			>
				{ translate( 'Are you sure you want to delete this schedule?' ) }
			</ConfirmDialog>
		</div>
	);
};