import { AxiosError } from 'axios' ; import { AppDispatch } from '../../contexts/reducer' ; import { AppActions , PageActions } from '../../contexts/actions' ; import initAddress from '../../contexts/states/address' ; import { fetchAddressInfo , fetchTipBlockNumber , fetchTransactionsByAddress } from '../http/fetcher' ; const handleAddressStatus = ( dispatch , addressStatus ) => { dispatch ( { type : PageActions . UpdateAddressStatus , payload : { addressStatus , } , } ) ; } ; const handleTransactionStatus = ( dispatch , transactionsStatus ) => { dispatch ( { type : PageActions . UpdateAddressTransactionsStatus , payload : { transactionsStatus , } , } ) ; } ; export const getAddressInformation = ( hash , dispatch ) => { handleAddressStatus ( dispatch , 'InProgress' ) ; fetchAddressInfo ( hash ) . then ( ( wrapper ) => { let { address } = initAddress ; if ( wrapper ) { address = { ... wrapper . attributes , type : wrapper . type === 'lock_hash' ? 'LockHash' : 'Address' , } ; } dispatch ( { type : PageActions . UpdateAddress , payload : { address , } , } ) ; handleAddressStatus ( dispatch , 'OK' ) ; } ) . catch ( ( error ) => { dispatch ( { type : PageActions . UpdateAddress , payload : { address : initAddress . address , } , } ) ; const isEmptyAddress = error && error . response && error . response . status === 404 ; handleAddressStatus ( dispatch , isEmptyAddress ? 'OK' : 'Error' ) ; } ) ; } ; export const getTransactionsByAddress = ( hash , page , size , dispatch ) => { handleTransactionStatus ( dispatch , 'InProgress' ) ; fetchTransactionsByAddress ( hash , page , size ) . then ( response => { const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; dispatch ( { type : PageActions . UpdateAddressTransactions , payload : { transactions : data . map ( ( wrapper ) => wrapper . attributes ) || [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateAddressTotal , payload : { total : meta ? meta . total : 0 , } , } ) ; handleTransactionStatus ( dispatch , 'OK' ) ; } ) . catch ( error => { dispatch ( { type : PageActions . UpdateAddressTransactions , payload : { transactions : [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateAddressTotal , payload : { total : 0 , } , } ) ; const isEmptyAddress = error && error . response && error . response . status === 404 ; handleTransactionStatus ( dispatch , isEmptyAddress ? 'OK' : 'Error' ) ; } ) ; } ; export const getTipBlockNumber = ( dispatch ) => { fetchTipBlockNumber ( ) . then ( ( wrapper ) => { if ( wrapper ) { dispatch ( { type : AppActions . UpdateTipBlockNumber , payload : { tipBlockNumber : parseInt ( wrapper . attributes . tipBlockNumber , 10 ) , } , } ) ; } } ) ; } ;