2021-02-04 19:15:23 +01:00
|
|
|
import { useEffect, useCallback } from 'react';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { getContractMethodData as getContractMethodDataAction } from '../store/actions';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
import { getKnownMethodData } from '../selectors/selectors';
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Access known method data and attempt to resolve unknown method data
|
|
|
|
*
|
|
|
|
* encapsulates an effect that will fetch methodData when the component mounts,
|
|
|
|
* and subsequently anytime the provided data attribute changes. Note that
|
|
|
|
* the getContractMethodData action handles over-fetching prevention, first checking
|
|
|
|
* if the data is in the store and returning it directly. While using this hook
|
|
|
|
* in multiple places in a tree for the same data will create extra event ticks and
|
|
|
|
* hit the action more frequently, it should only ever result in a single store update
|
2020-11-10 18:30:41 +01:00
|
|
|
* @param {string} data - the transaction data to find method data for
|
2020-05-26 22:49:11 +02:00
|
|
|
* @return {Object} contract method data
|
|
|
|
*/
|
2020-11-03 00:41:28 +01:00
|
|
|
export function useMethodData(data) {
|
2021-02-04 19:15:23 +01:00
|
|
|
const dispatch = useDispatch();
|
2020-11-03 00:41:28 +01:00
|
|
|
const knownMethodData = useSelector((state) =>
|
|
|
|
getKnownMethodData(state, data),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-11-03 00:41:28 +01:00
|
|
|
const getContractMethodData = useCallback(
|
|
|
|
(methodData) => dispatch(getContractMethodDataAction(methodData)),
|
|
|
|
[dispatch],
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-05-26 22:49:11 +02:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (data) {
|
2021-02-04 19:15:23 +01:00
|
|
|
getContractMethodData(data);
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
}, [getContractMethodData, data]);
|
|
|
|
return knownMethodData;
|
2020-05-26 22:49:11 +02:00
|
|
|
}
|