import { fetchNervosDao , fetchNervosDaoTransactions , fetchNervosDaoTransactionsByHash , fetchNervosDaoTransactionsByAddress , fetchNervosDaoDepositors , } from '../http/fetcher' ; import { AppDispatch } from '../../contexts/reducer' ; import { PageActions } from '../../contexts/actions' ; import { addPrefixForHash } from '../../utils/string' ; const handleNervosDAOStatus = ( dispatch , status ) => { dispatch ( { type : PageActions . UpdateNervosDaoStatus , payload : { status , } , } ) ; } ; const handleTransactionsStatus = ( dispatch , transactionsStatus ) => { dispatch ( { type : PageActions . UpdateNervosDaoTransactionsStatus , payload : { transactionsStatus , } , } ) ; } ; const handleTransactionsResponse = ( dispatch , transactions , total ) => { dispatch ( { type : PageActions . UpdateNervosDaoTransactions , payload : { transactions , } , } ) ; dispatch ( { type : PageActions . UpdateNervosDaoTransactionsTotal , payload : { total , } , } ) ; } ; export const getNervosDao = ( dispatch ) => { handleNervosDAOStatus ( dispatch , 'InProgress' ) ; fetchNervosDao ( ) . then ( ( wrapper ) => { if ( wrapper ) { dispatch ( { type : PageActions . UpdateNervosDao , payload : { nervosDao : wrapper . attributes , } , } ) ; } else { handleNervosDAOStatus ( dispatch , 'Error' ) ; } } ) . catch ( ( ) => { handleNervosDAOStatus ( dispatch , 'Error' ) ; } ) ; } ; export const getNervosDaoTransactions = ( dispatch , page , pageSize ) => { handleNervosDAOStatus ( dispatch , 'InProgress' ) ; fetchNervosDaoTransactions ( page , pageSize ) . then ( ( response ) => { const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; handleTransactionsResponse ( dispatch , data . map ( ( wrapper ) => wrapper . attributes ) , meta === undefined ? 1 : meta . total ) ; handleNervosDAOStatus ( dispatch , 'OK' ) ; } ) . catch ( ( ) => { handleNervosDAOStatus ( dispatch , 'Error' ) ; } ) ; } ; export const searchNervosDaoTransactions = ( keyword , dispatch ) => { handleTransactionsStatus ( dispatch , 'InProgress' ) ; if ( keyword . startsWith ( 'ckt' ) || keyword . startsWith ( 'ckb' ) ) { fetchNervosDaoTransactionsByAddress ( keyword ) . then ( ( response ) => { handleTransactionsStatus ( dispatch , 'OK' ) ; const { data , meta } = response as Response . Response < Response . Wrapper < State . Transaction > [ ] > ; handleTransactionsResponse ( dispatch , data . map ( ( wrapper ) => wrapper . attributes ) , meta === undefined ? 1 : meta . total ) ; } ) . catch ( ( ) => { handleTransactionsStatus ( dispatch , 'Error' ) ; } ) ; } else { fetchNervosDaoTransactionsByHash ( addPrefixForHash ( keyword ) ) . then ( ( wrapper ) => { handleTransactionsStatus ( dispatch , 'OK' ) ; const { attributes } = wrapper as Response . Wrapper < State . Transaction > ; handleTransactionsResponse ( dispatch , [ attributes ] , 1 ) ; } ) . catch ( ( ) => { handleTransactionsStatus ( dispatch , 'Error' ) ; } ) ; } } ; export const getNervosDaoDepositors = ( dispatch ) => { fetchNervosDaoDepositors ( ) . then ( ( response ) => { const { data } = response as Response . Response < Response . Wrapper < State . NervosDaoDepositor > [ ] > ; dispatch ( { type : PageActions . UpdateNervosDaoDepositors , payload : { depositors : data . map ( ( wrapper ) => wrapper . attributes ) , } , } ) ; } ) ; } ;