Settings UI: Move Todos config data to a dedicated REST endpoint. Add payment method - todo state sync.

This commit is contained in:
Daniel Dudzic 2025-01-28 11:52:56 +01:00
parent f5be7481fa
commit 611dc0ae38
No known key found for this signature in database
GPG key ID: 31B40D33E3465483
20 changed files with 691 additions and 26 deletions

View file

@ -0,0 +1,67 @@
import { subscribe, select, dispatch } from '@wordpress/data';
const TODO_TRIGGERS = {
'ppcp-applepay': 'enable_apple_pay',
'ppcp-googlepay': 'enable_google_pay',
'ppcp-axo-gateway': 'enable_fastlane',
'ppcp-card-button-gateway': 'enable_credit_debit_cards',
};
/**
* Initialize todo synchronization
*/
export const initTodoSync = () => {
let previousPaymentState = null;
let isProcessing = false;
subscribe( () => {
if ( isProcessing ) {
return;
}
isProcessing = true;
try {
const paymentState = select( 'wc/paypal/payment' ).persistentData();
const todosState = select( 'wc/paypal/todos' ).getTodos();
// Skip if states haven't been initialized yet
if ( ! paymentState || ! todosState || ! previousPaymentState ) {
previousPaymentState = paymentState;
return;
}
Object.entries( TODO_TRIGGERS ).forEach(
( [ paymentMethod, todoId ] ) => {
const wasEnabled =
previousPaymentState[ paymentMethod ]?.enabled;
const isEnabled = paymentState[ paymentMethod ]?.enabled;
if ( wasEnabled !== isEnabled ) {
const todoToUpdate = todosState.find(
( todo ) => todo.id === todoId
);
if ( todoToUpdate ) {
const updatedTodos = todosState.map( ( todo ) =>
todo.id === todoId
? { ...todo, isCompleted: isEnabled }
: todo
);
dispatch( 'wc/paypal/todos' ).setTodos(
updatedTodos
);
}
}
}
);
previousPaymentState = paymentState;
} catch ( error ) {
console.error( 'Error in todo sync:', error );
} finally {
isProcessing = false;
}
} );
};