2025-01-28 11:52:56 +01:00
|
|
|
/**
|
|
|
|
* Resolvers: Handle asynchronous data fetching for the store.
|
|
|
|
*
|
|
|
|
* These functions update store state with data from external sources.
|
|
|
|
* Each resolver corresponds to a specific selector (selector with same name must exist).
|
|
|
|
* Resolvers are called automatically when selectors request unavailable data.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { dispatch } from '@wordpress/data';
|
|
|
|
import { __ } from '@wordpress/i18n';
|
|
|
|
import { apiFetch } from '@wordpress/data-controls';
|
|
|
|
import { STORE_NAME, REST_PATH } from './constants';
|
|
|
|
|
|
|
|
export const resolvers = {
|
|
|
|
*getTodos() {
|
|
|
|
try {
|
|
|
|
const response = yield apiFetch( { path: REST_PATH } );
|
|
|
|
|
2025-02-03 23:06:33 +01:00
|
|
|
const { todos = [], dismissedTodos = [] } = response?.data || {};
|
2025-01-28 11:52:56 +01:00
|
|
|
|
|
|
|
yield dispatch( STORE_NAME ).setTodos( todos );
|
2025-02-03 23:06:33 +01:00
|
|
|
yield dispatch( STORE_NAME ).setDismissedTodos( dismissedTodos );
|
2025-01-28 11:52:56 +01:00
|
|
|
yield dispatch( STORE_NAME ).setIsReady( true );
|
|
|
|
} catch ( e ) {
|
|
|
|
console.error( 'Resolver error:', e );
|
|
|
|
yield dispatch( STORE_NAME ).setIsReady( false );
|
|
|
|
yield dispatch( 'core/notices' ).createErrorNotice(
|
|
|
|
__( 'Error retrieving todos.', 'woocommerce-paypal-payments' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|