/**
* External dependencies
*/
import type { Reducer } from 'redux';
import { objectHasProp, PaymentResult } from '@woocommerce/types';
* Internal dependencies
import { defaultPaymentState, PaymentState } from './default-state';
import { ACTION_TYPES } from './action-types';
import { STATUS } from './constants';
const reducer: Reducer< PaymentState > = (
state = defaultPaymentState,
action
) => {
let newState = state;
switch ( action.type ) {
case ACTION_TYPES.SET_PAYMENT_PRISTINE:
newState = {
...state,
status: STATUS.PRISTINE,
};
break;
case ACTION_TYPES.SET_PAYMENT_STARTED:
status: STATUS.STARTED,
case ACTION_TYPES.SET_PAYMENT_PROCESSING:
status: STATUS.PROCESSING,
case ACTION_TYPES.SET_PAYMENT_FAILED:
status: STATUS.FAILED,
case ACTION_TYPES.SET_PAYMENT_ERROR:
status: STATUS.ERROR,
case ACTION_TYPES.SET_PAYMENT_SUCCESS:
status: STATUS.SUCCESS,
case ACTION_TYPES.SET_SHOULD_SAVE_PAYMENT_METHOD:
shouldSavePaymentMethod: action.shouldSavePaymentMethod,
case ACTION_TYPES.SET_PAYMENT_METHOD_DATA:
paymentMethodData: action.paymentMethodData,
case ACTION_TYPES.SET_PAYMENT_RESULT:
paymentResult: action.data as PaymentResult,
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,
incompatiblePaymentMethods: Object.fromEntries(
Object.entries( state.incompatiblePaymentMethods ).filter(
( [ k ] ) => {
return ! ( k in 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,
paymentMethodData:
action.paymentMethodData || state.paymentMethodData,
default:
return newState;
export type State = ReturnType< typeof reducer >;
export default reducer;