2018-02-16 16:57:23 +00:00
|
|
|
import {
|
|
|
|
FetchTransactionDataAction,
|
|
|
|
SetTransactionDataAction,
|
2018-03-14 20:10:14 +00:00
|
|
|
AddRecentTransactionAction,
|
2018-02-16 16:57:23 +00:00
|
|
|
TransactionsAction,
|
|
|
|
TypeKeys
|
|
|
|
} from 'actions/transactions';
|
2018-03-14 20:10:14 +00:00
|
|
|
import { SavedTransaction, TransactionState } from 'types/transactions';
|
2018-02-16 16:57:23 +00:00
|
|
|
|
|
|
|
export interface State {
|
|
|
|
txData: { [txhash: string]: TransactionState };
|
2018-03-14 20:10:14 +00:00
|
|
|
recent: SavedTransaction[];
|
2018-02-16 16:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const INITIAL_STATE: State = {
|
2018-03-14 20:10:14 +00:00
|
|
|
txData: {},
|
|
|
|
recent: []
|
2018-02-16 16:57:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
function fetchTxData(state: State, action: FetchTransactionDataAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
txData: {
|
|
|
|
...state.txData,
|
|
|
|
[action.payload]: {
|
|
|
|
data: null,
|
|
|
|
receipt: null,
|
|
|
|
error: null,
|
|
|
|
isLoading: true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function setTxData(state: State, action: SetTransactionDataAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
txData: {
|
|
|
|
...state.txData,
|
|
|
|
[action.payload.txhash]: {
|
|
|
|
data: action.payload.data,
|
|
|
|
receipt: action.payload.receipt,
|
|
|
|
error: action.payload.error,
|
|
|
|
isLoading: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:10:14 +00:00
|
|
|
function resetTxData(state: State): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
txData: INITIAL_STATE.txData
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function addRecentTx(state: State, action: AddRecentTransactionAction): State {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
recent: [action.payload, ...state.recent].slice(0, 50)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-02-16 16:57:23 +00:00
|
|
|
export function transactions(state: State = INITIAL_STATE, action: TransactionsAction): State {
|
|
|
|
switch (action.type) {
|
|
|
|
case TypeKeys.TRANSACTIONS_FETCH_TRANSACTION_DATA:
|
|
|
|
return fetchTxData(state, action);
|
|
|
|
case TypeKeys.TRANSACTIONS_SET_TRANSACTION_DATA:
|
|
|
|
return setTxData(state, action);
|
2018-03-14 20:10:14 +00:00
|
|
|
case TypeKeys.TRANSACTIONS_RESET_TRANSACTION_DATA:
|
|
|
|
return resetTxData(state);
|
|
|
|
case TypeKeys.TRANSACTIONS_ADD_RECENT_TRANSACTION:
|
|
|
|
return addRecentTx(state, action);
|
2018-02-16 16:57:23 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|