woocommerce/plugins/woocommerce-blocks/assets/js/data/payment-methods/actions.ts

154 lines
4.3 KiB
TypeScript
Raw Normal View History

Feature: Data Store Migration - Payments (https://github.com/woocommerce/woocommerce-blocks/pull/6619) * Move paymentMethodDataProvider into a data store (https://github.com/woocommerce/woocommerce-blocks/pull/6208) * Fix typographical error on LegacyRegisterExpressPaymentMethodFunction type * Add default state for PaymentMethod data store * Add preliminary action types * Add preliminary action dispatchers * Create payment method data store * Add preliminary reducers for payment method data store * Add preliminary selectors for payment method data store * Add reducers/actions for registering payment methods * Export payment method data store key * Add test for payment method data reducers * Add shouldSavePaymentMethod selector * Add store key as constant * Add more action types for registering and initializing payment methods * Get active payment method from data store instead of from context * Add registered methods to default state of payment method data store * Dispatch name of registered payment method to payment method data store * Remove setShouldSavePayment from payment method dispatcher and types * Get payment methods from registry instead of payment context * Add available payment methods to store * Add function to check whether payment methods are allowed to be used * Add selector to check if payments are initialised * Remove resolvers and add controls to payment method data store * Change type of payment requirements to string[] * Turn addRegistered and addRegisteredExpress into generators This is so we can check each payment method's validity before adding it to the list of available payment methods * Add action type for setting express payments as initialized * Only select from available methods in payment method options * Remove argument from addRegisteredPaymentMethod in payment method registry * Rename folder and store name to not contain the word data * Add selectors for express payment methods and their initialisation * Delete controls again in favour of thunks * Rename payment-method-data to payment-methdods * Create new setDefaultPaymentMethod function This will set the payment method when the cart loads. * Add CustomerPaymentMethodConfiguration type * Make getAvailableExpressPaymentMethods return correct data * Check express methods and normal methods when cart changes * Add action for setting active express payment methods * Handle express methods in checkPaymentMethodCanPay * Hide express payments area if none are available * Add selector for paymentMethodData * Add customer data to default state and add selector for it * Add setPaymentStatus action and reducer case * Set the default payment method when one isn't selected * Correct types on getCustomerPaymentMethods * Set status in data store alongside context status * Comment out active gateway selection - remove later * Set status in express payment methods in data store * Use correct key in payment method data context * Directly check payment methods from the list in blocks-registry * Add semicolon to import statement * Fix payment method data state call * Get paymentMethodData from store not context * Fix linting issues Co-authored-by: Saad Tarhi <saad.trh@gmail.com> Co-authored-by: Alex Florisca <alex.florisca@automattic.com> * Rebase on the update/checkout-data-store branch & Fix failed payments (https://github.com/woocommerce/woocommerce-blocks/pull/6587) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Fix typographical error on LegacyRegisterExpressPaymentMethodFunction type * Add default state for PaymentMethod data store * Add preliminary action types * Add preliminary action dispatchers * Create payment method data store * Add preliminary reducers for payment method data store * Add preliminary selectors for payment method data store * Add reducers/actions for registering payment methods * Export payment method data store key * Add test for payment method data reducers * Add shouldSavePaymentMethod selector * Add store key as constant * Add more action types for registering and initializing payment methods * Get active payment method from data store instead of from context * Add registered methods to default state of payment method data store * Dispatch name of registered payment method to payment method data store * Remove setShouldSavePayment from payment method dispatcher and types * Get payment methods from registry instead of payment context * Add available payment methods to store * Add function to check whether payment methods are allowed to be used * Add selector to check if payments are initialised * Remove resolvers and add controls to payment method data store * Change type of payment requirements to string[] * Turn addRegistered and addRegisteredExpress into generators This is so we can check each payment method's validity before adding it to the list of available payment methods * Add action type for setting express payments as initialized * Only select from available methods in payment method options * Remove argument from addRegisteredPaymentMethod in payment method registry * Rename folder and store name to not contain the word data * Add selectors for express payment methods and their initialisation * Delete controls again in favour of thunks * Rename payment-method-data to payment-methdods * Create new setDefaultPaymentMethod function This will set the payment method when the cart loads. * Add CustomerPaymentMethodConfiguration type * Make getAvailableExpressPaymentMethods return correct data * Check express methods and normal methods when cart changes * Add action for setting active express payment methods * Handle express methods in checkPaymentMethodCanPay * Hide express payments area if none are available * Add selector for paymentMethodData * Add customer data to default state and add selector for it * Add setPaymentStatus action and reducer case * Set the default payment method when one isn't selected * Correct types on getCustomerPaymentMethods * Set status in data store alongside context status * Comment out active gateway selection - remove later * Set status in express payment methods in data store * Directly check payment methods from the list in blocks-registry * Add semicolon to import statement * Fix payment method data state call * Get paymentMethodData from store not context * Add addPaymentMethodData action/reducer case * Update payment method on payment success * Add 'getCurrentStatus' selector * Remove the temporary solution For getting payment method data into the data store * Prevent the 'success' context action from being dispatched * Update the "setPaymentStatus" data store action Accept status as an object instead of string * Fix the "currentStatus" reducer state update value * Get payment data into data store * Set the correct payment status to data store * Get the success status of payment from data store * Use store data in the payment dispatchers Replace the React useReducers action in the payment dispatchers file with the payment method data store * Get payment status from data store * Use data store for the payment error status * Use payment data store failed status * Use payment data store for the isFinished status Co-authored-by: Alex Florisca <alex.florisca@automattic.com> Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com> * Fix merge conflict error * Set & get the provider's state from our data store instead of React's useReducer (https://github.com/woocommerce/woocommerce-blocks/pull/6588) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Fix typographical error on LegacyRegisterExpressPaymentMethodFunction type * Add default state for PaymentMethod data store * Add preliminary action types * Add preliminary action dispatchers * Create payment method data store * Add preliminary reducers for payment method data store * Add preliminary selectors for payment method data store * Add reducers/actions for registering payment methods * Export payment method data store key * Add test for payment method data reducers * Add shouldSavePaymentMethod selector * Add store key as constant * Add more action types for registering and initializing payment methods * Get active payment method from data store instead of from context * Add registered methods to default state of payment method data store * Dispatch name of registered payment method to payment method data store * Remove setShouldSavePayment from payment method dispatcher and types * Get payment methods from registry instead of payment context * Add available payment methods to store * Add function to check whether payment methods are allowed to be used * Add selector to check if payments are initialised * Remove resolvers and add controls to payment method data store * Change type of payment requirements to string[] * Turn addRegistered and addRegisteredExpress into generators This is so we can check each payment method's validity before adding it to the list of available payment methods * Add action type for setting express payments as initialized * Only select from available methods in payment method options * Remove argument from addRegisteredPaymentMethod in payment method registry * Rename folder and store name to not contain the word data * Add selectors for express payment methods and their initialisation * Delete controls again in favour of thunks * Rename payment-method-data to payment-methdods * Create new setDefaultPaymentMethod function This will set the payment method when the cart loads. * Add CustomerPaymentMethodConfiguration type * Make getAvailableExpressPaymentMethods return correct data * Check express methods and normal methods when cart changes * Add action for setting active express payment methods * Handle express methods in checkPaymentMethodCanPay * Hide express payments area if none are available * Add selector for paymentMethodData * Add customer data to default state and add selector for it * Add setPaymentStatus action and reducer case * Set the default payment method when one isn't selected * Correct types on getCustomerPaymentMethods * Set status in data store alongside context status * Comment out active gateway selection - remove later * Set status in express payment methods in data store * Directly check payment methods from the list in blocks-registry * Add semicolon to import statement * Fix payment method data state call * Get paymentMethodData from store not context * Add addPaymentMethodData action/reducer case * Update payment method on payment success * Add 'getCurrentStatus' selector * Remove the temporary solution For getting payment method data into the data store * Prevent the 'success' context action from being dispatched * Update the "setPaymentStatus" data store action Accept status as an object instead of string * Fix the "currentStatus" reducer state update value * Get payment data into data store * Set the correct payment status to data store * Get the success status of payment from data store * Use store data in the payment dispatchers Replace the React useReducers action in the payment dispatchers file with the payment method data store * Get payment status from data store * Use data store for the payment error status * Use payment data store failed status * Use payment data store for the isFinished status * Update the setPaymentStatus argument * Set up setRegisteredPaymentMethods in data store * Use the data store version of the registeredPaymentMethods * Fix the default state type of the registeredPaymentMethods * Set up setRegisteredExpressPaymentMethod in data store * Use the data version of the registred express payment methods * Set the correct action type for removing payment methods * Fix default state express payment methods type * Use the store data version of activePaymentMethod * Use setActivePaymentMethod in the dispatchers file And refactor code * Update payment status arguments for express payment methods * Use paymentMethodData from the data store * Use payment method's errorMessage from data store * Update paymentMethods list in data store reducer * Replace remaining payment context data with data store * Clean up payment method context file Co-authored-by: Alex Florisca <alex.florisca@automattic.com> Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com> * Get payment method data directly from the data store instead of the usePaymentMethodDataContext hook (https://github.com/woocommerce/woocommerce-blocks/pull/6589) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Fix typographical error on LegacyRegisterExpressPaymentMethodFunction type * Add default state for PaymentMethod data store * Add preliminary action types * Add preliminary action dispatchers * Create payment method data store * Add preliminary reducers for payment method data store * Add preliminary selectors for payment method data store * Add reducers/actions for registering payment methods * Export payment method data store key * Add test for payment method data reducers * Add shouldSavePaymentMethod selector * Add store key as constant * Add more action types for registering and initializing payment methods * Get active payment method from data store instead of from context * Add registered methods to default state of payment method data store * Dispatch name of registered payment method to payment method data store * Remove setShouldSavePayment from payment method dispatcher and types * Get payment methods from registry instead of payment context * Add available payment methods to store * Add function to check whether payment methods are allowed to be used * Add selector to check if payments are initialised * Remove resolvers and add controls to payment method data store * Change type of payment requirements to string[] * Turn addRegistered and addRegisteredExpress into generators This is so we can check each payment method's validity before adding it to the list of available payment methods * Add action type for setting express payments as initialized * Only select from available methods in payment method options * Remove argument from addRegisteredPaymentMethod in payment method registry * Rename folder and store name to not contain the word data * Add selectors for express payment methods and their initialisation * Delete controls again in favour of thunks * Rename payment-method-data to payment-methdods * Create new setDefaultPaymentMethod function This will set the payment method when the cart loads. * Add CustomerPaymentMethodConfiguration type * Make getAvailableExpressPaymentMethods return correct data * Check express methods and normal methods when cart changes * Add action for setting active express payment methods * Handle express methods in checkPaymentMethodCanPay * Hide express payments area if none are available * Add selector for paymentMethodData * Add customer data to default state and add selector for it * Add setPaymentStatus action and reducer case * Set the default payment method when one isn't selected * Correct types on getCustomerPaymentMethods * Set status in data store alongside context status * Comment out active gateway selection - remove later * Set status in express payment methods in data store * Directly check payment methods from the list in blocks-registry * Add semicolon to import statement * Fix payment method data state call * Get paymentMethodData from store not context * Add addPaymentMethodData action/reducer case * Update payment method on payment success * Add 'getCurrentStatus' selector * Remove the temporary solution For getting payment method data into the data store * Prevent the 'success' context action from being dispatched * Update the "setPaymentStatus" data store action Accept status as an object instead of string * Fix the "currentStatus" reducer state update value * Get payment data into data store * Set the correct payment status to data store * Get the success status of payment from data store * Use store data in the payment dispatchers Replace the React useReducers action in the payment dispatchers file with the payment method data store * Get payment status from data store * Use data store for the payment error status * Use payment data store failed status * Use payment data store for the isFinished status * Update the setPaymentStatus argument * Set up setRegisteredPaymentMethods in data store * Use the data store version of the registeredPaymentMethods * Fix the default state type of the registeredPaymentMethods * Set up setRegisteredExpressPaymentMethod in data store * Use the data version of the registred express payment methods * Set the correct action type for removing payment methods * Fix default state express payment methods type * Use the store data version of activePaymentMethod * Use setActivePaymentMethod in the dispatchers file And refactor code * Update payment status arguments for express payment methods * Use paymentMethodData from the data store * Use payment method's errorMessage from data store * Update paymentMethods list in data store reducer * Replace remaining payment context data with data store * Clean up payment method context file * Get payment method state from data store in the checkout submit hook * Copy types.ts file into the payment data store folder * Fix isExpressPaymentMethodActive selector * Move the entire currentStatus into the data store * Replace the payment context state with the data store * Fix getActiveSavedToken & clean up the context file * Use the accutrate name of the "createErrorNotice" * Update the payment method data store key import * Diable unused state from the context Co-authored-by: Alex Florisca <alex.florisca@automattic.com> Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com> * Refactor the payment method data store & context (https://github.com/woocommerce/woocommerce-blocks/pull/6607) * Add checkout data store * wip on checkout data store * CheckoutContext now uses the checkout store * Investigated and removed setting the redirectUrl on the default state * update extension and address hooks to use checkout data store * use checkout data store in checkout-processor and use-checkout-button * trim useCheckoutContext from use-payment-method-interface && use-store-cart-item-quantity * Remove useCheckoutContext from shipping provider * Remove isCalculating from state * Removed useCheckoutContext from lots of places * Remove useCheckoutContext from checkout-payment-block * Remove useCheckoutContext in checkout-shipping-methods-block and checkout-shipping-address-block * add isCart selector and action and update the checkoutstate context * Fixed redirectUrl bug by using thunks * Remove dispatchActions from checkout-state * Change SET_HAS_ERROR action to be neater * Thomas' feedback * Tidy up * Oops, deleted things I shouldn't have * Typescript * Fix types * Fix tests * Remove isCart * Update docs and remove unecessary getRedirectUrl() selector * set correct type for preloadedCheckoutData * Remove duplicate Address type * Fix missing addresses from type-defs index * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Update docs/block-client-apis/checkout/checkout-api.md Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> * Revert feedback changes * REvert feedback formatting * Update docs formatting * Fix typographical error on LegacyRegisterExpressPaymentMethodFunction type * Add default state for PaymentMethod data store * Add preliminary action types * Add preliminary action dispatchers * Create payment method data store * Add preliminary reducers for payment method data store * Add preliminary selectors for payment method data store * Add reducers/actions for registering payment methods * Export payment method data store key * Add test for payment method data reducers * Add shouldSavePaymentMethod selector * Add store key as constant * Add more action types for registering and initializing payment methods * Get active payment method from data store instead of from context * Add registered methods to default state of payment method data store * Dispatch name of registered payment method to payment method data store * Remove setShouldSavePayment from payment method dispatcher and types * Get payment methods from registry instead of payment context * Add available payment methods to store * Add function to check whether payment methods are allowed to be used * Add selector to check if payments are initialised * Remove resolvers and add controls to payment method data store * Change type of payment requirements to string[] * Turn addRegistered and addRegisteredExpress into generators This is so we can check each payment method's validity before adding it to the list of available payment methods * Add action type for setting express payments as initialized * Only select from available methods in payment method options * Remove argument from addRegisteredPaymentMethod in payment method registry * Rename folder and store name to not contain the word data * Add selectors for express payment methods and their initialisation * Delete controls again in favour of thunks * Rename payment-method-data to payment-methdods * Create new setDefaultPaymentMethod function This will set the payment method when the cart loads. * Add CustomerPaymentMethodConfiguration type * Make getAvailableExpressPaymentMethods return correct data * Check express methods and normal methods when cart changes * Add action for setting active express payment methods * Handle express methods in checkPaymentMethodCanPay * Hide express payments area if none are available * Add selector for paymentMethodData * Add customer data to default state and add selector for it * Add setPaymentStatus action and reducer case * Set the default payment method when one isn't selected * Correct types on getCustomerPaymentMethods * Set status in data store alongside context status * Comment out active gateway selection - remove later * Set status in express payment methods in data store * Directly check payment methods from the list in blocks-registry * Add semicolon to import statement * Fix payment method data state call * Get paymentMethodData from store not context * Add addPaymentMethodData action/reducer case * Update payment method on payment success * Add 'getCurrentStatus' selector * Remove the temporary solution For getting payment method data into the data store * Prevent the 'success' context action from being dispatched * Update the "setPaymentStatus" data store action Accept status as an object instead of string * Fix the "currentStatus" reducer state update value * Get payment data into data store * Set the correct payment status to data store * Get the success status of payment from data store * Use store data in the payment dispatchers Replace the React useReducers action in the payment dispatchers file with the payment method data store * Get payment status from data store * Use data store for the payment error status * Use payment data store failed status * Use payment data store for the isFinished status * Update the setPaymentStatus argument * Set up setRegisteredPaymentMethods in data store * Use the data store version of the registeredPaymentMethods * Fix the default state type of the registeredPaymentMethods * Set up setRegisteredExpressPaymentMethod in data store * Use the data version of the registred express payment methods * Set the correct action type for removing payment methods * Fix default state express payment methods type * Use the store data version of activePaymentMethod * Use setActivePaymentMethod in the dispatchers file And refactor code * Update payment status arguments for express payment methods * Use paymentMethodData from the data store * Use payment method's errorMessage from data store * Update paymentMethods list in data store reducer * Replace remaining payment context data with data store * Clean up payment method context file * Get payment method state from data store in the checkout submit hook * Copy types.ts file into the payment data store folder * Fix isExpressPaymentMethodActive selector * Move the entire currentStatus into the data store * Replace the payment context state with the data store * Fix getActiveSavedToken & clean up the context file * Use the accutrate name of the "createErrorNotice" * Update the payment method data store key import * Diable unused state from the context * Get enabled customer payment methods using data store selector * Remove remaining useReducer action from the dispatchers file * Update types and remove unused vars * Remove the payment method dispatchers hook * Refactor & clean up (remove unused files) * Remove commented line from payment methods types * Move event emitter into thunks Co-authored-by: Alex Florisca <alex.florisca@automattic.com> Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com> * Remove checkout-state after merge conflicts * Fix linting errors * Move types to types.ts * Move default states into respective store folders * Fix types and add comment * Move setExpressPaymentError to payment-methods store * fix express payment methods not showing up * Check if payment method is active from the state * Add comments * Remove commented out code in payment method data context * Display an error in the check-payment-methods directly from data store * Remove use-emit-response hook and move utils in event-emit/utils.ts * Use correct action property to remove payment methods * Fix formatting * Only try to initialize payment methods when cart is done loading * Add function to order payment methods from server * Add payment methods in the correct order * Prevent adding registered payment methods before cart is ready * Ensure payment methods get removed from state when deregistered * Reorder setting default payment methods to add customer methods first * Get customer methods from store not context * Remove error from payment-method state and associated selectors * Remove use-payment-method-registration and update the payment method state to remove the duplicated registeredPaymentMethods * Remove errorMessage from payment-methods store * Rename customerPaymentMethods -> savedPaymentMethods * Order payment methods when validating * Refactor payment-methods.js * Fix "Payment methods not set in editor" woocommerce/woocommerce-blocks#6655 bug We never get to load the payment methods object in the editor mode because there are no cart totals to load. * Initialize payment methods when available payments are loaded * Remove duplicate code * Fix data store state mutation anti-pattern A Redux rule is to never mutate the state in a reducer to avoid any unexpected results * Set availablePaymentMethods to the paymentMethods object Instead of its keys. We can get the keys using "Object.keys". * Use the available ordered payment methods The `getPaymentMethods` & `getExpressPaymentMethods` may include unordored & unavailable payment methods. * Get the correct value from the emit event response Co-authored-by: Thomas Roberts <5656702+opr@users.noreply.github.com> Co-authored-by: Saad Tarhi <saad.trh@gmail.com> Co-authored-by: Thomas Roberts <thomas.roberts@automattic.com>
2022-07-08 05:53:24 +00:00
/**
* External dependencies
*/
import { select as wpDataSelect } from '@wordpress/data';
import {
PaymentMethods,
ExpressPaymentMethods,
} from '@woocommerce/type-defs/payments';
/**
* Internal dependencies
*/
import { ACTION_TYPES } from './action-types';
import { checkPaymentMethodsCanPay } from './check-payment-methods';
import { setDefaultPaymentMethod } from './set-default-payment-method';
import { PaymentStatus } from './types';
import { CART_STORE_KEY } from '../cart';
// `Thunks are functions that can be dispatched, similar to actions creators
export * from './thunks';
export const setPaymentStatus = (
status: PaymentStatus,
paymentMethodData?: Record< string, unknown >
) => ( {
type: ACTION_TYPES.SET_PAYMENT_STATUS,
status,
paymentMethodData,
} );
export const setPaymentMethodsInitialized = ( initialized: boolean ) => {
return async ( { select, dispatch } ) => {
// If the currently selected method is not in this new list, then we need to select a new one, or select a default.
const methods = select.getAvailablePaymentMethods();
if ( initialized ) {
await setDefaultPaymentMethod( methods );
}
dispatch( {
type: ACTION_TYPES.SET_PAYMENT_METHODS_INITIALIZED,
initialized,
} );
};
};
export const setExpressPaymentMethodsInitialized = (
initialized: boolean
) => ( {
type: ACTION_TYPES.SET_EXPRESS_PAYMENT_METHODS_INITIALIZED,
initialized,
} );
export const setShouldSavePaymentMethod = (
shouldSavePaymentMethod: boolean
) => ( {
type: ACTION_TYPES.SET_SHOULD_SAVE_PAYMENT_METHOD,
shouldSavePaymentMethod,
} );
export const setActivePaymentMethod = (
activePaymentMethod: string,
paymentMethodData: Record< string, unknown > = {}
) => ( {
type: ACTION_TYPES.SET_ACTIVE_PAYMENT_METHOD,
activePaymentMethod,
paymentMethodData,
} );
export const setPaymentMethodData = (
paymentMethodData: Record< string, unknown > = {}
) => ( {
type: ACTION_TYPES.SET_PAYMENT_METHOD_DATA,
paymentMethodData,
} );
/**
* Set the available payment methods.
* An available payment method is one that has been validated and can make a payment.
*/
export const setAvailablePaymentMethods = (
paymentMethods: PaymentMethods
) => {
return async ( { dispatch } ) => {
// If the currently selected method is not in this new list, then we need to select a new one, or select a default.
await setDefaultPaymentMethod( paymentMethods );
dispatch( {
type: ACTION_TYPES.SET_AVAILABLE_PAYMENT_METHODS,
paymentMethods,
} );
};
};
/**
* Set the available express payment methods.
* An available payment method is one that has been validated and can make a payment.
*/
export const setAvailableExpressPaymentMethods = (
paymentMethods: ExpressPaymentMethods
) => ( {
type: ACTION_TYPES.SET_AVAILABLE_EXPRESS_PAYMENT_METHODS,
paymentMethods,
} );
/**
* Remove a payment method name from the available payment methods.
* This is called when a payment method is removed from the registry.
*/
export const removeAvailablePaymentMethod = ( name: string ) => ( {
type: ACTION_TYPES.REMOVE_AVAILABLE_PAYMENT_METHOD,
name,
} );
/**
* Remove an express payment method name from the available payment methods.
* This is called when an express payment method is removed from the registry.
*/
export const removeRegisteredExpressPaymentMethod = ( name: string ) => ( {
type: ACTION_TYPES.REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD,
name,
} );
/**
* Checks the payment methods held in the registry can make a payment
* and updates the available payment methods in the store.
*/
export function updateAvailablePaymentMethods() {
return async ( { dispatch } ) => {
const registered = await checkPaymentMethodsCanPay();
const cartTotalsLoaded =
wpDataSelect( CART_STORE_KEY ).hasFinishedResolution(
'getCartTotals'
);
if ( registered && cartTotalsLoaded ) {
dispatch( setPaymentMethodsInitialized( true ) );
}
};
}
/**
* Checks the express payment methods held in the registry can make a payment
* and updates the available express payment methods in the store.
*/
export function updateAvailableExpressPaymentMethods() {
return async ( { dispatch } ) => {
const registered = await checkPaymentMethodsCanPay( true );
const cartTotalsLoaded =
wpDataSelect( CART_STORE_KEY ).hasFinishedResolution(
'getCartTotals'
);
if ( registered && cartTotalsLoaded ) {
dispatch( setExpressPaymentMethodsInitialized( true ) );
}
};
}