import { fetchTransactionsByBlockHash , fetchBlock , fetchBlocks , fetchBlockList } from '../http/fetcher' ; import { AppDispatch } from '../../contexts/reducer' ; import { PageActions } from '../../contexts/actions' ; const handleStatus = ( dispatch , status ) => { dispatch ( { type : PageActions . UpdateBlockStatus , payload : { status , } , } ) ; } ; export const getBlockTransactions = ( hash , page , size , dispatch ) => { fetchTransactionsByBlockHash ( hash , page , size ) . then ( response => { handleStatus ( dispatch , 'OK' ) ; const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; dispatch ( { type : PageActions . UpdateBlockTransactions , payload : { transactions : data . map ( ( wrapper ) => wrapper . attributes ) , } , } ) ; if ( meta ) { dispatch ( { type : PageActions . UpdateBlockTotal , payload : { total : meta . total , } , } ) ; } } ) . catch ( ( ) => { handleStatus ( dispatch , 'Error' ) ; } ) ; } ; export const getBlock = ( blockParam , page , size , dispatch ) => { handleStatus ( dispatch , 'InProgress' ) ; fetchBlock ( blockParam ) . then ( ( wrapper ) => { if ( wrapper ) { const block = wrapper . attributes ; dispatch ( { type : PageActions . UpdateBlock , payload : { block , } , } ) ; getBlockTransactions ( block . blockHash , page , size , dispatch ) ; } else { handleStatus ( dispatch , 'OK' ) ; } } ) . catch ( ( ) => { handleStatus ( dispatch , 'Error' ) ; } ) ; } ; export const getLatestBlocks = ( dispatch ) => { fetchBlocks ( ) . then ( ( wrappers ) => { if ( wrappers ) { const blocks = wrappers . map ( ( wrapper ) => wrapper . attributes ) ; dispatch ( { type : PageActions . UpdateHomeBlocks , payload : { homeBlocks : blocks , } , } ) ; } } ) ; } ; export const getBlocks = ( page , size , dispatch ) => { fetchBlockList ( page , size ) . then ( response => { const { data , meta } = response as Response . Response < Response . Wrapper < State . Block > [ ] > ; if ( meta ) { dispatch ( { type : PageActions . UpdateBlockListTotal , payload : { total : meta . total , } , } ) ; } if ( data ) { const blocks = data . map ( ( wrapper ) => wrapper . attributes ) ; dispatch ( { type : PageActions . UpdateBlockList , payload : { blocks , } , } ) ; } } ) ; } ;