/**
* 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_IDLE:
newState = {
...state,
status: STATUS.IDLE,
};
break;
case ACTION_TYPES.SET_EXPRESS_PAYMENT_STARTED:
status: STATUS.EXPRESS_STARTED,
case ACTION_TYPES.SET_PAYMENT_PROCESSING:
status: STATUS.PROCESSING,
case ACTION_TYPES.SET_PAYMENT_READY:
status: STATUS.READY,
case ACTION_TYPES.SET_PAYMENT_ERROR:
status: STATUS.ERROR,
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 = {
...state.availableExpressPaymentMethods,
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,
paymentMethodData:
action.paymentMethodData || state.paymentMethodData,
default:
return newState;
}
export type State = ReturnType< typeof reducer >;
export default reducer;