|
|
import { withStorageKey } from '@automattic/state-utils'; |
|
|
import { |
|
|
BILLING_TRANSACTIONS_RECEIVE, |
|
|
BILLING_TRANSACTIONS_REQUEST, |
|
|
BILLING_TRANSACTIONS_REQUEST_FAILURE, |
|
|
BILLING_TRANSACTIONS_REQUEST_SUCCESS, |
|
|
} from 'calypso/state/action-types'; |
|
|
import { combineReducers, withSchemaValidation } from 'calypso/state/utils'; |
|
|
import individualTransactions from './individual-transactions/reducer'; |
|
|
import { billingTransactionsSchema } from './schema'; |
|
|
import type { BillingTransactionsState, BillingTransactionsStateItems } from './types'; |
|
|
import type { AnyAction } from 'redux'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const items = withSchemaValidation( |
|
|
billingTransactionsSchema, |
|
|
( state: BillingTransactionsState[ 'items' ] = {}, action: AnyAction ) => { |
|
|
switch ( action.type ) { |
|
|
case BILLING_TRANSACTIONS_RECEIVE: { |
|
|
const { past, upcoming } = action; |
|
|
const update: BillingTransactionsStateItems = { ...state }; |
|
|
if ( past ) { |
|
|
update.past = past; |
|
|
} |
|
|
if ( upcoming ) { |
|
|
update.upcoming = upcoming; |
|
|
} |
|
|
return update; |
|
|
} |
|
|
} |
|
|
|
|
|
return state; |
|
|
} |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const requesting = ( |
|
|
state: BillingTransactionsState[ 'requesting' ] = false, |
|
|
action: AnyAction |
|
|
) => { |
|
|
switch ( action.type ) { |
|
|
case BILLING_TRANSACTIONS_REQUEST: |
|
|
return true; |
|
|
case BILLING_TRANSACTIONS_REQUEST_FAILURE: |
|
|
return false; |
|
|
case BILLING_TRANSACTIONS_REQUEST_SUCCESS: |
|
|
return false; |
|
|
} |
|
|
|
|
|
return state; |
|
|
}; |
|
|
|
|
|
const combinedReducer = combineReducers( { |
|
|
items, |
|
|
requesting, |
|
|
|
|
|
|
|
|
individualTransactions, |
|
|
} ); |
|
|
|
|
|
export default withStorageKey( 'billingTransactions', combinedReducer ); |
|
|
|