import { fetchSimpleUDT , fetchSimpleUDTTransactions , fetchSimpleUDTTransactionsWithAddress } from '../http/fetcher' ; import { AppDispatch } from '../../contexts/reducer' ; import { PageActions } from '../../contexts/actions' ; const handleStatus = ( dispatch , status ) => { dispatch ( { type : PageActions . UpdateUDTStatus , payload : { status , } , } ) ; } ; const handleFilterStatus = ( dispatch , filterStatus ) => { dispatch ( { type : PageActions . UpdateUDTFilterStatus , payload : { filterStatus , } , } ) ; } ; export const getSimpleUDT = ( typeHash , dispatch ) => { handleStatus ( dispatch , 'InProgress' ) ; fetchSimpleUDT ( typeHash ) . then ( ( wrapper ) => { if ( wrapper ) { dispatch ( { type : PageActions . UpdateUDT , payload : { udt : wrapper . attributes , } , } ) ; handleStatus ( dispatch , 'OK' ) ; } else { handleStatus ( dispatch , 'Error' ) ; } } ) . catch ( ( ) => { handleStatus ( dispatch , 'Error' ) ; } ) ; } ; export const getSimpleUDTTransactions = ( typeHash , page , size , dispatch ) => { handleStatus ( dispatch , 'InProgress' ) ; fetchSimpleUDTTransactions ( typeHash , page , size ) . then ( ( response ) => { const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; dispatch ( { type : PageActions . UpdateUDTTransactions , payload : { transactions : data . map ( ( wrapper ) => wrapper . attributes ) || [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateUDTTransactionsTotal , payload : { total : meta ? meta . total : 0 , } , } ) ; handleStatus ( dispatch , 'OK' ) ; } ) . catch ( ( ) => { dispatch ( { type : PageActions . UpdateUDTTransactions , payload : { transactions : [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateUDTTransactionsTotal , payload : { total : 0 , } , } ) ; handleStatus ( dispatch , 'Error' ) ; } ) ; } ; export const getUDTTransactionsWithAddress = ( address , typeHash , page , size , dispatch ) => { handleFilterStatus ( dispatch , 'InProgress' ) ; fetchSimpleUDTTransactionsWithAddress ( address , typeHash , page , size ) . then ( ( response ) => { handleFilterStatus ( dispatch , 'OK' ) ; const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; dispatch ( { type : PageActions . UpdateUDTTransactions , payload : { transactions : data . map ( ( wrapper ) => wrapper . attributes ) || [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateUDTTransactionsTotal , payload : { total : meta ? meta . total : 0 , } , } ) ; } ) . catch ( ( ) => { handleFilterStatus ( dispatch , 'Error' ) ; dispatch ( { type : PageActions . UpdateUDTTransactions , payload : { transactions : [ ] , } , } ) ; dispatch ( { type : PageActions . UpdateUDTTransactionsTotal , payload : { total : 0 , } , } ) ; } ) ; } ;