/**
* External dependencies
*/
import type { Reducer } from 'redux';
import { objectHasProp } from '@woocommerce/types';
* Internal dependencies
import {
defaultPaymentMethodDataState,
PaymentMethodDataState,
} from './default-state';
import { ACTION_TYPES } from './action-types';
const reducer: Reducer< PaymentMethodDataState > = (
state = defaultPaymentMethodDataState,
action
) => {
let newState = state;
switch ( action.type ) {
case ACTION_TYPES.SET_SHOULD_SAVE_PAYMENT_METHOD:
newState = {
...state,
shouldSavePaymentMethod: action.shouldSavePaymentMethod,
};
break;
case ACTION_TYPES.SET_PAYMENT_METHOD_DATA:
paymentMethodData: action.paymentMethodData,
case ACTION_TYPES.SET_PAYMENT_STATUS:
currentStatus: {
// When the status is changed to pristine, we need to reset the currentStatus properties
// to their default initial values
...( action.status?.isPristine === true
? defaultPaymentMethodDataState.currentStatus
: state.currentStatus ),
...action.status,
isFinished:
action.status.hasError ||
action.status.hasFailed ||
action.status.isSuccessful,
},
paymentMethodData:
action.paymentMethodData || state.paymentMethodData,
case ACTION_TYPES.REMOVE_AVAILABLE_PAYMENT_METHOD:
const previousAvailablePaymentMethods = {
...state.availablePaymentMethods,
delete previousAvailablePaymentMethods[ action.name ];
availablePaymentMethods: {
...previousAvailablePaymentMethods,
case ACTION_TYPES.REMOVE_AVAILABLE_EXPRESS_PAYMENT_METHOD:
const previousAvailableExpressPaymentMethods = {
delete previousAvailableExpressPaymentMethods[ action.name ];
availableExpressPaymentMethods: {
...previousAvailableExpressPaymentMethods,
case ACTION_TYPES.SET_PAYMENT_METHODS_INITIALIZED:
paymentMethodsInitialized: action.initialized,
case ACTION_TYPES.SET_EXPRESS_PAYMENT_METHODS_INITIALIZED:
expressPaymentMethodsInitialized: action.initialized,
case ACTION_TYPES.SET_AVAILABLE_PAYMENT_METHODS:
availablePaymentMethods: action.paymentMethods,
case ACTION_TYPES.SET_AVAILABLE_EXPRESS_PAYMENT_METHODS:
availableExpressPaymentMethods: action.paymentMethods,
case ACTION_TYPES.SET_ACTIVE_PAYMENT_METHOD:
const activeSavedToken =
typeof state.paymentMethodData === 'object' &&
objectHasProp( action.paymentMethodData, 'token' )
? action.paymentMethodData.token + ''
: '';
activeSavedToken,
activePaymentMethod: action.activePaymentMethod,
default:
return newState;
}
export type State = ReturnType< typeof reducer >;
export default reducer;