2024-11-18 18:51:11 +01:00
|
|
|
/**
|
|
|
|
* Controls: Implement side effects, typically asynchronous operations.
|
|
|
|
*
|
2024-11-20 16:48:50 +01:00
|
|
|
* Controls use ACTION_TYPES keys as identifiers.
|
2024-11-18 18:51:11 +01:00
|
|
|
* They are triggered by corresponding actions and handle external interactions.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
2024-11-18 16:31:05 +01:00
|
|
|
import { select } from '@wordpress/data';
|
2024-11-20 16:48:50 +01:00
|
|
|
import apiFetch from '@wordpress/api-fetch';
|
2024-11-18 16:31:05 +01:00
|
|
|
|
2024-11-20 16:48:50 +01:00
|
|
|
import {
|
|
|
|
STORE_NAME,
|
|
|
|
REST_PERSIST_PATH,
|
|
|
|
REST_MANUAL_CONNECTION_PATH,
|
|
|
|
} from './constants';
|
2024-11-18 16:31:05 +01:00
|
|
|
import ACTION_TYPES from './action-types';
|
|
|
|
|
|
|
|
export const controls = {
|
2024-11-20 16:48:50 +01:00
|
|
|
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
|
|
|
|
console.log( 'Do PERSIST: ', data );
|
2024-11-18 16:31:05 +01:00
|
|
|
try {
|
|
|
|
await apiFetch( {
|
2024-11-20 16:48:50 +01:00
|
|
|
path: REST_PERSIST_PATH,
|
|
|
|
method: 'POST',
|
2024-11-18 16:31:05 +01:00
|
|
|
data,
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
console.error( 'Error saving progress.', e );
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-11-20 16:48:50 +01:00
|
|
|
async [ ACTION_TYPES.DO_MANUAL_CONNECTION ]( {
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
useSandbox,
|
|
|
|
} ) {
|
2024-11-18 16:31:05 +01:00
|
|
|
let result = null;
|
|
|
|
|
|
|
|
try {
|
|
|
|
result = await apiFetch( {
|
2024-11-20 16:48:50 +01:00
|
|
|
path: REST_MANUAL_CONNECTION_PATH,
|
2024-11-18 16:31:05 +01:00
|
|
|
method: 'POST',
|
|
|
|
data: {
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
useSandbox,
|
|
|
|
},
|
|
|
|
} );
|
|
|
|
} catch ( e ) {
|
|
|
|
result = {
|
|
|
|
success: false,
|
|
|
|
error: e,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
};
|