Skip to content

Commit 818dafb

Browse files
Social | Improve types for connection actions and selectors (#41201)
1 parent 914318d commit 818dafb

File tree

4 files changed

+160
-134
lines changed

4 files changed

+160
-134
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Significance: patch
2+
Type: changed
3+
Comment: Improved TS types for connection actions and selectors
4+
5+

projects/js-packages/publicize-components/src/social-store/actions/connection-data.ts

+85-72
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { dispatch as coreDispatch } from '@wordpress/data';
44
import { store as editorStore } from '@wordpress/editor';
55
import { __, sprintf } from '@wordpress/i18n';
66
import { getSocialScriptData } from '../../utils/script-data';
7-
import { Connection } from '../types';
7+
import { Connection, KeyringResult } from '../types';
88
import {
99
ADD_CONNECTION,
1010
DELETE_CONNECTION,
@@ -23,10 +23,10 @@ import {
2323

2424
/**
2525
* Set connections list
26-
* @param {Array<import('../types').Connection>} connections - list of connections
27-
* @return {object} - an action object.
26+
* @param connections - list of connections
27+
* @return An action object.
2828
*/
29-
export function setConnections( connections ) {
29+
export function setConnections( connections: Array< Connection > ) {
3030
return {
3131
type: SET_CONNECTIONS,
3232
connections,
@@ -36,11 +36,11 @@ export function setConnections( connections ) {
3636
/**
3737
* Set keyring result
3838
*
39-
* @param {import('../types').KeyringResult} [keyringResult] - keyring result
39+
* @param keyringResult - keyring result
4040
*
41-
* @return {object} - an action object.
41+
* @return An action object.
4242
*/
43-
export function setKeyringResult( keyringResult ) {
43+
export function setKeyringResult( keyringResult?: KeyringResult ) {
4444
return {
4545
type: SET_KEYRING_RESULT,
4646
keyringResult,
@@ -49,10 +49,10 @@ export function setKeyringResult( keyringResult ) {
4949

5050
/**
5151
* Add connection to the list
52-
* @param {import('../types').Connection} connection - connection object
53-
* @return {object} - an action object.
52+
* @param connection - connection object
53+
* @return An action object.
5454
*/
55-
export function addConnection( connection ) {
55+
export function addConnection( connection: Partial< Connection > ) {
5656
return {
5757
type: ADD_CONNECTION,
5858
connection,
@@ -61,11 +61,11 @@ export function addConnection( connection ) {
6161

6262
/**
6363
* Toggle connection enable status.
64-
* @param {string} connectionId - Connection ID to switch.
64+
* @param connectionId - Connection ID to switch.
6565
*
66-
* @return {object} Switch connection enable-status action.
66+
* @return Switch connection enable-status action.
6767
*/
68-
export function toggleConnection( connectionId ) {
68+
export function toggleConnection( connectionId: string ) {
6969
return {
7070
type: TOGGLE_CONNECTION,
7171
connectionId,
@@ -74,13 +74,13 @@ export function toggleConnection( connectionId ) {
7474

7575
/**
7676
* Merge connections with fresh connections.
77-
* @param {Array} freshConnections - list of fresh connections
78-
* @return {Function} - a function to merge connections.
77+
* @param freshConnections - list of fresh connections
78+
* @return A thunk to merge connections.
7979
*/
80-
export function mergeConnections( freshConnections ) {
80+
export function mergeConnections( freshConnections: Array< Connection > ) {
8181
return function ( { dispatch, select } ) {
8282
// Combine current connections with new connections.
83-
const prevConnections = select.getConnections();
83+
const prevConnections: Array< Connection > = select.getConnections();
8484
const connections = [];
8585
const defaults = {
8686
enabled: true,
@@ -108,12 +108,12 @@ export function mergeConnections( freshConnections ) {
108108

109109
/**
110110
* Create an abort controller.
111-
* @param {AbortController} abortController - Abort controller.
112-
* @param {string} requestType - Type of abort request.
111+
* @param abortController - Abort controller.
112+
* @param requestType - Type of abort request.
113113
*
114-
* @return {object} - an action object.
114+
* @return An action object.
115115
*/
116-
export function createAbortController( abortController, requestType ) {
116+
export function createAbortController( abortController: AbortController, requestType: string ) {
117117
return {
118118
type: ADD_ABORT_CONTROLLER,
119119
requestType,
@@ -124,11 +124,11 @@ export function createAbortController( abortController, requestType ) {
124124
/**
125125
* Remove abort controllers.
126126
*
127-
* @param {string} requestType - Type of abort request.
127+
* @param requestType - Type of abort request.
128128
*
129-
* @return {object} - an action object.
129+
* @return An action object.
130130
*/
131-
export function removeAbortControllers( requestType ) {
131+
export function removeAbortControllers( requestType: string ) {
132132
return {
133133
type: REMOVE_ABORT_CONTROLLERS,
134134
requestType,
@@ -138,11 +138,11 @@ export function removeAbortControllers( requestType ) {
138138
/**
139139
* Abort a request.
140140
*
141-
* @param {string} requestType - Type of abort request.
141+
* @param requestType - Type of abort request.
142142
*
143-
* @return {Function} - a function to abort a request.
143+
* @return A thunk to abort a request.
144144
*/
145-
export function abortRequest( requestType ) {
145+
export function abortRequest( requestType: string ) {
146146
return function ( { dispatch, select } ) {
147147
const abortControllers = select.getAbortControllers( requestType );
148148

@@ -158,7 +158,7 @@ export function abortRequest( requestType ) {
158158
/**
159159
* Abort the refresh connections request.
160160
*
161-
* @return {Function} - a function to abort a request.
161+
* @return A thunk to abort a request.
162162
*/
163163
export function abortRefreshConnectionsRequest() {
164164
return abortRequest( REQUEST_TYPE_REFRESH_CONNECTIONS );
@@ -167,8 +167,8 @@ export function abortRefreshConnectionsRequest() {
167167
/**
168168
* Effect handler which will refresh the connection test results.
169169
*
170-
* @param {boolean} syncToMeta - Whether to sync the connection state to the post meta.
171-
* @return {Function} Refresh connection test results action.
170+
* @param syncToMeta - Whether to sync the connection state to the post meta.
171+
* @return A thunk to refresh connection test results.
172172
*/
173173
export function refreshConnectionTestResults( syncToMeta = false ) {
174174
return async function ( { dispatch, select } ) {
@@ -188,7 +188,10 @@ export function refreshConnectionTestResults( syncToMeta = false ) {
188188
dispatch( createAbortController( abortController, REQUEST_TYPE_REFRESH_CONNECTIONS ) );
189189

190190
// Pass the abort controller signal to the fetch request.
191-
const freshConnections = await apiFetch( { path, signal: abortController.signal } );
191+
const freshConnections = await apiFetch< Array< Connection > >( {
192+
path,
193+
signal: abortController.signal,
194+
} );
192195

193196
dispatch( mergeConnections( freshConnections ) );
194197

@@ -208,7 +211,7 @@ export function refreshConnectionTestResults( syncToMeta = false ) {
208211
/**
209212
* Syncs the connections to the post meta.
210213
*
211-
* @return {Function} Sync connections to post meta action.
214+
* @return A thunk to sync connections to post meta.
212215
*/
213216
export function syncConnectionsToPostMeta() {
214217
return function ( { registry, select } ) {
@@ -224,11 +227,11 @@ export function syncConnectionsToPostMeta() {
224227
/**
225228
* Toggles the connection enable-status.
226229
*
227-
* @param {string} connectionId - Connection ID to switch.
228-
* @param {boolean} syncToMeta - Whether to sync the connection state to the post meta.
229-
* @return {object} Switch connection enable-status action.
230+
* @param connectionId - Connection ID to switch.
231+
* @param syncToMeta - Whether to sync the connection state to the post meta.
232+
* @return A think to switch connection enable-status.
230233
*/
231-
export function toggleConnectionById( connectionId, syncToMeta = true ) {
234+
export function toggleConnectionById( connectionId: string, syncToMeta = true ) {
232235
return function ( { dispatch } ) {
233236
dispatch( toggleConnection( connectionId ) );
234237

@@ -241,11 +244,11 @@ export function toggleConnectionById( connectionId, syncToMeta = true ) {
241244
/**
242245
* Deletes a connection.
243246
*
244-
* @param {string} connectionId - Connection ID to delete.
247+
* @param connectionId - Connection ID to delete.
245248
*
246-
* @return {object} Delete connection action.
249+
* @return An action object.
247250
*/
248-
export function deleteConnection( connectionId ) {
251+
export function deleteConnection( connectionId: string ) {
249252
return {
250253
type: DELETE_CONNECTION,
251254
connectionId,
@@ -255,12 +258,12 @@ export function deleteConnection( connectionId ) {
255258
/**
256259
* Marks a connection as being deleted.
257260
*
258-
* @param {string} connectionId - Connection ID to delete.
259-
* @param {boolean} deleting - Whether the connection is being deleted.
261+
* @param connectionId - Connection ID to delete.
262+
* @param deleting - Whether the connection is being deleted.
260263
*
261-
* @return {object} Deleting connection action.
264+
* @return An action object.
262265
*/
263-
export function deletingConnection( connectionId, deleting = true ) {
266+
export function deletingConnection( connectionId: string, deleting = true ) {
264267
return {
265268
type: DELETING_CONNECTION,
266269
connectionId,
@@ -271,13 +274,19 @@ export function deletingConnection( connectionId, deleting = true ) {
271274
/**
272275
* Deletes a connection by disconnecting it.
273276
*
274-
* @param {object} args - Arguments.
275-
* @param {string | number} args.connectionId - Connection ID to delete.
276-
* @param {boolean} [args.showSuccessNotice] - Whether to show a success notice.
277+
* @param args - Arguments.
278+
* @param args.connectionId - Connection ID to delete.
279+
* @param args.showSuccessNotice - Whether to show a success notice.
277280
*
278-
* @return {boolean} Whether the connection was deleted.
281+
* @return A thunk that resolves to true if the connection was deleted, false otherwise.
279282
*/
280-
export function deleteConnectionById( { connectionId, showSuccessNotice = true } ) {
283+
export function deleteConnectionById( {
284+
connectionId,
285+
showSuccessNotice = true,
286+
}: {
287+
connectionId: string;
288+
showSuccessNotice?: boolean;
289+
} ) {
281290
return async function ( { registry, dispatch } ) {
282291
const { createErrorNotice, createSuccessNotice } = coreDispatch( globalNoticesStore );
283292

@@ -330,11 +339,15 @@ let uniqueId = 1;
330339
/**
331340
* Creates a connection.
332341
*
333-
* @param {Record<string, any>} data - The data for API call.
334-
* @param {Record<string, any>} optimisticData - Optimistic data for the connection.
335-
* @return {void}
342+
* @param data - The data for API call.
343+
* @param optimisticData - Optimistic data for the connection.
344+
*
345+
* @return A thunk to create a connection.
336346
*/
337-
export function createConnection( data, optimisticData = {} ) {
347+
export function createConnection(
348+
data: Record< string, unknown >,
349+
optimisticData: Partial< Connection > = {}
350+
) {
338351
return async function ( { registry, dispatch } ) {
339352
const { createErrorNotice, createSuccessNotice } = coreDispatch( globalNoticesStore );
340353

@@ -403,12 +416,12 @@ export function createConnection( data, optimisticData = {} ) {
403416
/**
404417
* Updates a connection.
405418
*
406-
* @param {string} connectionId - Connection ID to update.
407-
* @param {Record<string, any>} data - The data.
419+
* @param connectionId - Connection ID to update.
420+
* @param data - The data.
408421
*
409-
* @return {object} Delete connection action.
422+
* @return An action object.
410423
*/
411-
export function updateConnection( connectionId, data ) {
424+
export function updateConnection( connectionId: string, data: Partial< Connection > ) {
412425
return {
413426
type: UPDATE_CONNECTION,
414427
connectionId,
@@ -419,12 +432,12 @@ export function updateConnection( connectionId, data ) {
419432
/**
420433
* Marks a connection as being updating.
421434
*
422-
* @param {string} connectionId - Connection ID being updated.
423-
* @param {boolean} updating - Whether the connection is being updated.
435+
* @param connectionId - Connection ID being updated.
436+
* @param updating - Whether the connection is being updated.
424437
*
425-
* @return {object} Deleting connection action.
438+
* @return An action object.
426439
*/
427-
export function updatingConnection( connectionId, updating = true ) {
440+
export function updatingConnection( connectionId: string, updating = true ) {
428441
return {
429442
type: UPDATING_CONNECTION,
430443
connectionId,
@@ -435,11 +448,11 @@ export function updatingConnection( connectionId, updating = true ) {
435448
/**
436449
* Sets the reconnecting account.
437450
*
438-
* @param {import('../types').Connection} reconnectingAccount - Account being reconnected.
451+
* @param reconnectingAccount - Account being reconnected.
439452
*
440-
* @return {object} Reconnecting account action.
453+
* @return An action object.
441454
*/
442-
export function setReconnectingAccount( reconnectingAccount ) {
455+
export function setReconnectingAccount( reconnectingAccount: Connection ) {
443456
return {
444457
type: SET_RECONNECTING_ACCOUNT,
445458
reconnectingAccount,
@@ -449,11 +462,11 @@ export function setReconnectingAccount( reconnectingAccount ) {
449462
/**
450463
* Updates a connection.
451464
*
452-
* @param {string} connectionId - Connection ID to update.
453-
* @param {Record<string, any>} data - The data for API call.
454-
* @return {void}
465+
* @param connectionId - Connection ID to update.
466+
* @param data - The data for API call.
467+
* @return A thunk to update a connection.
455468
*/
456-
export function updateConnectionById( connectionId, data ) {
469+
export function updateConnectionById( connectionId: string, data: Partial< Connection > ) {
457470
return async function ( { dispatch, select } ) {
458471
const { createErrorNotice, createSuccessNotice } = coreDispatch( globalNoticesStore );
459472

@@ -501,11 +514,11 @@ export function updateConnectionById( connectionId, data ) {
501514
/**
502515
* Toggles the connections modal.
503516
*
504-
* @param {boolean} isOpen - Whether the modal is open.
517+
* @param isOpen - Whether the modal is open.
505518
*
506-
* @return {object} - An action object.
519+
* @return An action object.
507520
*/
508-
export function toggleConnectionsModal( isOpen ) {
521+
export function toggleConnectionsModal( isOpen: boolean ) {
509522
return {
510523
type: TOGGLE_CONNECTIONS_MODAL,
511524
isOpen,
@@ -515,15 +528,15 @@ export function toggleConnectionsModal( isOpen ) {
515528
/**
516529
* Opens the connections modal.
517530
*
518-
* @return {object} - An action object.
531+
* @return An action object.
519532
*/
520533
export function openConnectionsModal() {
521534
return toggleConnectionsModal( true );
522535
}
523536

524537
/**
525538
* Closes the connections modal.
526-
* @return {object} - An action object.
539+
* @return An action object.
527540
*/
528541
export function closeConnectionsModal() {
529542
return toggleConnectionsModal( false );

0 commit comments

Comments
 (0)