File size: 3,510 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
import { type UseQueryOptions, useQuery } from '@tanstack/react-query';
import wpcomRequest from 'wpcom-proxy-request';

export interface BulkDomainUpdateStatus {
	results: {
		[ key: string ]: '' | 'success' | 'failed';
	};
	action: 'set_auto_renew' | 'update_contact_info';
	created_at: number;
	auto_renew?: boolean;
	whois?: unknown;
	transfer_lock?: boolean;
}

export interface BulkDomainUpdateStatusQueryFnData {
	[ key: string ]: BulkDomainUpdateStatus;
}

export interface JobStatus {
	id: string;
	action: 'set_auto_renew' | 'update_contact_info';
	created_at: number;
	success: string[];
	failed: string[];
	pending: string[];
	complete: boolean;
	params: {
		auto_renew?: boolean;
		whois?: unknown;
		transfer_lock?: boolean;
	};
}

interface DomainUpdateRemoteStatus {
	status: '' | 'success' | 'failed';
	action: 'set_auto_renew' | 'update_contact_info';
	created_at: number;
	auto_renew?: boolean;
	whois?: unknown;
	transfer_lock?: boolean;
}

interface DomainUpdateDerivedStatus {
	status: '';
	message: string;
	created_at: number;
}

export type DomainUpdateStatus = DomainUpdateRemoteStatus | DomainUpdateDerivedStatus;

export const getBulkDomainUpdateStatusQueryKey = () => {
	return [ 'domains', 'bulk-actions' ];
};

export interface BulkDomainUpdateStatusResult {
	domainResults: Map< string, DomainUpdateStatus[] >;
	completedJobs: JobStatus[];
}

export function useBulkDomainUpdateStatusQuery< TError = unknown >(
	pollingInterval: number,
	options: Omit<
		UseQueryOptions< BulkDomainUpdateStatusQueryFnData, TError, BulkDomainUpdateStatusResult >,
		'queryKey'
	> = {}
) {
	return useQuery( {
		queryFn: () =>
			wpcomRequest< BulkDomainUpdateStatusQueryFnData >( {
				path: '/domains/bulk-actions',
				apiNamespace: 'wpcom/v2',
			} ),
		select: ( data ): BulkDomainUpdateStatusResult => {
			// get top-level info about recent jobs
			const allJobs: JobStatus[] = Object.keys( data ).map( ( jobId ) => {
				const { action, created_at, results, ...rest } = data[ jobId ];
				const success: string[] = [];
				const failed: string[] = [];
				const pending: string[] = [];

				Object.entries( results ).forEach( ( entry ) => {
					if ( entry[ 1 ] === 'success' ) {
						success.push( entry[ 0 ] );
					} else if ( entry[ 1 ] === 'failed' ) {
						failed.push( entry[ 0 ] );
					} else {
						pending.push( entry[ 0 ] );
					}
				} );

				return {
					id: jobId,
					action: action,
					created_at: created_at,
					success,
					failed,
					pending,
					complete: pending.length === 0,
					params: rest,
				};
			} );

			// get domain-level updates that can be shown inline in the table rows
			const domainResults = new Map< string, DomainUpdateStatus[] >();

			Object.keys( data ).forEach( ( jobId ) => {
				// only create domain-level results for jobs that
				// are still running
				if ( ! allJobs.find( ( job ) => job.id === jobId )?.complete ) {
					const entry = data[ jobId ] as BulkDomainUpdateStatus;
					const { results, ...rest } = entry;
					Object.keys( results ).forEach( ( domain ) => {
						if ( ! domainResults.has( domain ) ) {
							domainResults.set( domain, [] );
						}
						const status = results[ domain ];
						domainResults.get( domain )?.push( { ...rest, status } );
					} );
				}
			} );

			const completedJobs = allJobs.filter( ( job ) => job.complete );

			return { domainResults, completedJobs };
		},
		refetchInterval: pollingInterval,
		...options,
		queryKey: getBulkDomainUpdateStatusQueryKey(),
	} );
}