File size: 2,073 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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';

/**
 * Returns the updated items state after an action has been dispatched.
 * The state contains all past and upcoming billing transactions.
 */
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;
	}
);

/**
 * Returns the updated requests state after an action has been dispatched.
 * The state contains whether a request for billing transactions is in progress.
 */
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,
	//individual transactions contains transactions that are not part of the items tree.
	//TODO: if pagination is implemented, address potential data duplication between individualTransactions and items
	individualTransactions,
} );

export default withStorageKey( 'billingTransactions', combinedReducer );