♻️ Simplify Redux controllers

This commit is contained in:
Philipp Stracker 2024-12-17 15:05:34 +01:00
parent 11599102f4
commit a77b209e3e
No known key found for this signature in database
2 changed files with 21 additions and 43 deletions

View file

@ -187,7 +187,13 @@ export const connectViaIdAndSecret = function* () {
* @return {Action} The action.
*/
export const refreshMerchantData = function* () {
return yield { type: ACTION_TYPES.DO_REFRESH_MERCHANT };
const result = yield { type: ACTION_TYPES.DO_REFRESH_MERCHANT };
if ( result.success && result.merchant ) {
yield hydrate( result );
}
return result;
};
/**
@ -201,7 +207,9 @@ export const refreshFeatureStatuses = function* () {
const result = yield { type: ACTION_TYPES.DO_REFRESH_FEATURES };
if ( result && result.success ) {
return yield dispatch( STORE_NAME ).refreshMerchantData();
// TODO: Review if we can get the updated feature details in the result.data instead of
// doing a second refreshMerchantData() request.
yield refreshMerchantData();
}
return result;

View file

@ -7,11 +7,9 @@
* @file
*/
import { dispatch } from '@wordpress/data';
import apiFetch from '@wordpress/api-fetch';
import {
STORE_NAME,
REST_PERSIST_PATH,
REST_MANUAL_CONNECTION_PATH,
REST_CONNECTION_URL_PATH,
@ -23,7 +21,7 @@ import ACTION_TYPES from './action-types';
export const controls = {
async [ ACTION_TYPES.DO_PERSIST_DATA ]( { data } ) {
try {
return await apiFetch( {
await apiFetch( {
path: REST_PERSIST_PATH,
method: 'POST',
data,
@ -34,10 +32,8 @@ export const controls = {
},
async [ ACTION_TYPES.DO_SANDBOX_LOGIN ]() {
let result = null;
try {
result = await apiFetch( {
return apiFetch( {
path: REST_CONNECTION_URL_PATH,
method: 'POST',
data: {
@ -46,20 +42,16 @@ export const controls = {
},
} );
} catch ( e ) {
result = {
return {
success: false,
error: e,
};
}
return result;
},
async [ ACTION_TYPES.DO_PRODUCTION_LOGIN ]( { products } ) {
let result = null;
try {
result = await apiFetch( {
return apiFetch( {
path: REST_CONNECTION_URL_PATH,
method: 'POST',
data: {
@ -68,13 +60,11 @@ export const controls = {
},
} );
} catch ( e ) {
result = {
return {
success: false,
error: e,
};
}
return result;
},
async [ ACTION_TYPES.DO_MANUAL_CONNECTION ]( {
@ -82,10 +72,8 @@ export const controls = {
clientSecret,
useSandbox,
} ) {
let result = null;
try {
result = await apiFetch( {
return await apiFetch( {
path: REST_MANUAL_CONNECTION_PATH,
method: 'POST',
data: {
@ -95,54 +83,36 @@ export const controls = {
},
} );
} catch ( e ) {
result = {
return {
success: false,
error: e,
};
}
return result;
},
async [ ACTION_TYPES.DO_REFRESH_MERCHANT ]() {
let result = null;
try {
result = await apiFetch( { path: REST_HYDRATE_MERCHANT_PATH } );
if ( result.success && result.merchant ) {
await dispatch( STORE_NAME ).hydrate( result );
}
return await apiFetch( { path: REST_HYDRATE_MERCHANT_PATH } );
} catch ( e ) {
result = {
return {
success: false,
error: e,
};
}
return result;
},
async [ ACTION_TYPES.DO_REFRESH_FEATURES ]() {
let result = null;
try {
result = await apiFetch( {
return await apiFetch( {
path: REST_REFRESH_FEATURES_PATH,
method: 'POST',
} );
if ( result.success ) {
result = await dispatch( STORE_NAME ).refreshMerchantData();
}
} catch ( e ) {
result = {
return {
success: false,
error: e,
message: e.message,
};
}
return result;
},
};