/ common / features / transactions / reducer.ts
reducer.ts
 1  import * as types from './types';
 2  
 3  export const INITIAL_STATE: types.TransactionsState = {
 4    txData: {},
 5    recent: []
 6  };
 7  
 8  function fetchTxData(
 9    state: types.TransactionsState,
10    action: types.FetchTransactionDataAction
11  ): types.TransactionsState {
12    return {
13      ...state,
14      txData: {
15        ...state.txData,
16        [action.payload]: {
17          data: null,
18          receipt: null,
19          error: null,
20          isLoading: true
21        }
22      }
23    };
24  }
25  
26  function setTxData(
27    state: types.TransactionsState,
28    action: types.SetTransactionDataAction
29  ): types.TransactionsState {
30    return {
31      ...state,
32      txData: {
33        ...state.txData,
34        [action.payload.txhash]: {
35          data: action.payload.data,
36          receipt: action.payload.receipt,
37          error: action.payload.error,
38          isLoading: false
39        }
40      }
41    };
42  }
43  
44  function resetTxData(state: types.TransactionsState): types.TransactionsState {
45    return {
46      ...state,
47      txData: INITIAL_STATE.txData
48    };
49  }
50  
51  function addRecentTx(
52    state: types.TransactionsState,
53    action: types.AddRecentTransactionAction
54  ): types.TransactionsState {
55    return {
56      ...state,
57      recent: [action.payload, ...state.recent].slice(0, 50)
58    };
59  }
60  
61  export function transactionsReducer(
62    state: types.TransactionsState = INITIAL_STATE,
63    action: types.TransactionsAction
64  ): types.TransactionsState {
65    switch (action.type) {
66      case types.TransactionsActions.FETCH_TRANSACTION_DATA:
67        return fetchTxData(state, action);
68      case types.TransactionsActions.SET_TRANSACTION_DATA:
69        return setTxData(state, action);
70      case types.TransactionsActions.RESET_TRANSACTION_DATA:
71        return resetTxData(state);
72      case types.TransactionsActions.ADD_RECENT_TRANSACTION:
73        return addRecentTx(state, action);
74      default:
75        return state;
76    }
77  }