import React from 'react'; import Config from '../../config'; import { translate } from '../../translate/translate'; import { secondsToString } from '../../util/time'; import { sortByDate } from '../../util/sort'; import { basiliskRefresh, basiliskConnection, getDexNotaries, toggleDashboardTxInfoModal, getBasiliskTransactionsList, changeMainBasiliskAddress, displayNotariesModal, deleteCacheFile, connectNotaries, toggleViewCacheModal, fetchNewCacheData, fetchUtxoCache, restartBasiliskInstance, changeActiveAddress } from '../../actions/actionCreators'; import Store from '../../store'; import WalletsBasiliskRefresh from './walletsBasiliskRefresh'; import WalletsBasiliskConnection from './walletsBasiliskConnection'; import WalletsNotariesList from './walletsNotariesList'; import WalletsCacheData from './walletsCacheData'; import { SocketProvider } from 'socket.io-react'; import io from 'socket.io-client'; const socket = io.connect('http://127.0.0.1:' + Config.agamaPort); class WalletsData extends React.Component { constructor(props) { super(props); this.state = { basiliskActionsMenu: false, itemsPerPage: 10, activePage: 1, itemsList: null, currentAddress: null, addressSelectorOpen: false, currentStackLength: 0, totalStackLength: 0, useCache: true, }; this.updateInput = this.updateInput.bind(this); this.toggleBasiliskActionsMenu = this.toggleBasiliskActionsMenu.bind(this); this.basiliskRefreshAction = this.basiliskRefreshAction.bind(this); this.basiliskConnectionAction = this.basiliskConnectionAction.bind(this); this.getDexNotariesAction = this.getDexNotariesAction.bind(this); this.openDropMenu = this.openDropMenu.bind(this); this.removeAndFetchNewCache = this.removeAndFetchNewCache.bind(this); this._toggleViewCacheModal = this._toggleViewCacheModal.bind(this); this.toggleCacheApi = this.toggleCacheApi.bind(this); this._fetchUtxoCache = this._fetchUtxoCache.bind(this); this.restartBasiliskInstance = this.restartBasiliskInstance.bind(this); this.basiliskRefreshActionOne = this.basiliskRefreshActionOne.bind(this); socket.on('messages', msg => this.updateSocketsData(msg)); } componentDidMount() { console.log('use cache = ', this.state.useCache); } toggleCacheApi() { const _useCache = !this.state.useCache; console.log('useCache is set to', _useCache); sessionStorage.setItem('useCache', _useCache); this.setState(Object.assign({}, this.state, { useCache: _useCache, })); } restartBasiliskInstance() { Store.dispatch(restartBasiliskInstance()); } _toggleViewCacheModal() { Store.dispatch(toggleViewCacheModal(!this.props.Dashboard.displayViewCacheModal)); } updateSocketsData(data) { if (data && data.message && data.message.shepherd.iguanaAPI && data.message.shepherd.iguanaAPI.totalStackLength) { this.setState(Object.assign({}, this.state, { totalStackLength: data.message.shepherd.iguanaAPI.totalStackLength, })); } if (data && data.message && data.message.shepherd.iguanaAPI && data.message.shepherd.iguanaAPI.currentStackLength) { this.setState(Object.assign({}, this.state, { currentStackLength: data.message.shepherd.iguanaAPI.currentStackLength, })); } if (data && data.message && data.message.shepherd.method && data.message.shepherd.method === 'cache-one' && data.message.shepherd.status === 'done') { Store.dispatch(basiliskRefresh(false)); } } removeAndFetchNewCache() { Store.dispatch(deleteCacheFile({ 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'listtransactions:getbalance', 'address': this.state.currentAddress, })); } _fetchUtxoCache() { Store.dispatch(fetchUtxoCache({ 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'refresh', 'address': this.state.currentAddress, })); console.log('_fetchUtxoCache', { 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'refresh', 'address': this.state.currentAddress, }); } toggleBasiliskActionsMenu() { this.setState(Object.assign({}, this.state, { basiliskActionsMenu: !this.state.basiliskActionsMenu, })); } basiliskRefreshAction() { Store.dispatch(fetchNewCacheData({ 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'listtransactions:getbalance', })); } basiliskRefreshActionOne() { Store.dispatch(fetchNewCacheData({ 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'listtransactions:getbalance', 'address': this.props.ActiveCoin.activeAddress, })); } basiliskConnectionAction() { if (this.props.Dashboard) { Store.dispatch(basiliskConnection(!this.props.Dashboard.basiliskConnection)); Store.dispatch(connectNotaries()); } } getDexNotariesAction() { Store.dispatch(getDexNotaries(this.props.ActiveCoin.coin)); Store.dispatch(displayNotariesModal(true)); } updateInput(e) { let historyToSplit = sortByDate(this.props.ActiveCoin.txhistory); historyToSplit = historyToSplit.slice(0, e.target.value); this.setState({ [e.target.name]: e.target.value, activePage: 1, itemsList: historyToSplit, }); } toggleTxInfoModal(display, txIndex) { Store.dispatch(toggleDashboardTxInfoModal(display, txIndex)); } componentWillReceiveProps(props) { if (!this.state.currentAddress && this.props.ActiveCoin.activeAddress) { this.setState(Object.assign({}, this.state, { currentAddress: this.props.ActiveCoin.activeAddress, })); } if (this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory !== 'loading' && this.props.ActiveCoin.txhistory !== 'no data' && this.props.ActiveCoin.txhistory.length) { if (!this.state.itemsList || (this.state.itemsList && !this.state.itemsList.length) || (props.ActiveCoin.txhistory !== this.props.ActiveCoin.txhistory)) { let historyToSplit = sortByDate(this.props.ActiveCoin.txhistory); historyToSplit = historyToSplit.slice((this.state.activePage - 1) * this.state.itemsPerPage, this.state.activePage * this.state.itemsPerPage); this.setState(Object.assign({}, this.state, { itemsList: historyToSplit, })); } } if (this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory === 'no data') { this.setState(Object.assign({}, this.state, { itemsList: 'no data', })); } else if (this.props.ActiveCoin.txhistory && this.props.ActiveCoin.txhistory === 'loading') { this.setState(Object.assign({}, this.state, { itemsList: 'loading', })); } } updateCurrentPage(page) { let historyToSplit = sortByDate(this.props.ActiveCoin.txhistory); historyToSplit = historyToSplit.slice((page - 1) * this.state.itemsPerPage, page * this.state.itemsPerPage); this.setState(Object.assign({}, this.state, { activePage: page, itemsList: historyToSplit, })); } renderPaginationItems() { let items = []; for (let i=0; i < Math.ceil(this.props.ActiveCoin.txhistory.length / this.state.itemsPerPage); i++) { items.push(
  • this.updateCurrentPage(i + 1) : null}>{i + 1}
  • ); } return items; } renderPaginationItemsPerPageSelector() { if (this.props.ActiveCoin.txhistory && this.state.itemsList !== 'loading' && this.props.ActiveCoin.txhistory.length > 10) { return (
    ); } else { return null; } } renderPagination() { if (this.props.ActiveCoin.txhistory && this.state.itemsList !== 'loading' && this.props.ActiveCoin.txhistory.length > 10) { return (
    Showing {((this.state.activePage - 1) * this.state.itemsPerPage) + 1} to {this.state.activePage * this.state.itemsPerPage} of {this.props.ActiveCoin.txhistory.length} entries
    ); } else { return null; } } renderTxType(category) { if ( category === 'send' || category === 'sent' ) { return ( {translate('DASHBOARD.OUT')} ); } if ( category === 'receive' || category === 'received' ) { return ( {translate('DASHBOARD.IN')} ); } if ( category === 'generate' ) { return ( {translate('DASHBOARD.MINED')} ); } if ( category === 'immature' ) { return ( {translate('DASHBOARD.IMMATURE')} ); } if ( category === 'unknown' ) { return ( {translate('DASHBOARD.UNKNOWN')} ); } } renderTxHistoryList() { if (this.state.itemsList === 'loading') { return (
    Loading history...
    ); } else if (this.state.itemsList === 'no data') { return (
    No data
    ); } else { if (this.state.itemsList && this.state.itemsList.length && this.state.itemsList !== 'no data') { return this.state.itemsList.map((tx, index) => {this.renderTxType(tx.category || tx.type)} {tx.confirmations} {tx.amount || translate('DASHBOARD.UNKNOWN')} {secondsToString(tx.blocktime || tx.timestamp)} {tx.address} ); } } } updateAddressSelection(address, type, amount) { Store.dispatch(changeActiveAddress(address)); this.setState(Object.assign({}, this.state, { currentAddress: address, addressSelectorOpen: false, activePage: 1, })); if (this.props.ActiveCoin.mode === 'basilisk') { setTimeout(function() { Store.dispatch(changeMainBasiliskAddress(address)); Store.dispatch(getBasiliskTransactionsList(this.props.ActiveCoin.coin, address)); }.bind(this), 100); Store.dispatch(fetchNewCacheData({ 'pubkey': this.props.Dashboard.activeHandle.pubkey, 'allcoins': false, 'coin': this.props.ActiveCoin.coin, 'calls': 'listtransactions:getbalance', 'address': address, })); } } openDropMenu() { this.setState(Object.assign({}, this.state, { addressSelectorOpen: !this.state.addressSelectorOpen, })); } /*filterTable() { function myFunction() { // Declare variables var input, filter, table, tr, td, i; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); table = document.getElementById("myTable"); tr = table.getElementsByTagName("tr"); // Loop through all table rows, and hide those who don't match the search query for (i = 0; i < tr.length; i++) { td = tr[i].getElementsByTagName("td")[0]; if (td) { if (td.innerHTML.toUpperCase().indexOf(filter) > -1) { tr[i].style.display = ""; } else { tr[i].style.display = "none"; } } } } }*/ renderUseCacheToggle() { if (this.props.ActiveCoin.mode === 'basilisk') { return (
    ); } else { return null; } } renderAddressByType(type) { if (this.props.ActiveCoin.addresses && this.props.ActiveCoin.addresses[type] && this.props.ActiveCoin.addresses[type].length) { let items = []; for (let i = 0; i < this.props.ActiveCoin.addresses[type].length; i++) { const address = this.props.ActiveCoin.addresses[type][i]; let _amount = address.amount; if (this.props.ActiveCoin.mode === 'basilisk') { _amount = this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address.address] && this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address.address].getbalance.data && this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address.address].getbalance.data.balance ? this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address.address].getbalance.data.balance : 'N/A'; } items.push(
  • this.updateAddressSelection(address.address, type, _amount)}> [ {_amount} {this.props.ActiveCoin.coin} ]  {address.address}
  • ); } return items; } else { return null; } } renderAddressAmount() { if (this.props.ActiveCoin.addresses && this.props.ActiveCoin.addresses.public && this.props.ActiveCoin.addresses.public.length) { for (let i = 0; i < this.props.ActiveCoin.addresses.public.length; i++) { if (this.props.ActiveCoin.addresses.public[i].address === this.state.currentAddress) { if (this.props.ActiveCoin.addresses.public[i].amount && this.props.ActiveCoin.addresses.public[i].amount !== 'N/A') { return this.props.ActiveCoin.addresses.public[i].amount; } else { const address = this.props.ActiveCoin.addresses.public[i].address; const _amount = this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address] && this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address].getbalance.data && this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address].getbalance.data.balance ? this.props.ActiveCoin.cache[this.props.ActiveCoin.coin][address].getbalance.data.balance : 'N/A'; return _amount; } } } } else { return 0; } } renderSelectorCurrentLabel() { if (this.state.currentAddress) { return ( [ {this.renderAddressAmount()} {this.props.ActiveCoin.coin} ]  {this.state.currentAddress} ); } else { return ( - Select Transparent or Private Address - ); } } renderAddressList() { if (this.props.Dashboard && this.props.Dashboard.activeHandle && this.props.Dashboard.activeHandle[this.props.ActiveCoin.coin] && this.props.ActiveCoin.mode === 'basilisk') { return (
    ); } else { return null; } } render() { if (this.props && this.props.ActiveCoin && this.props.ActiveCoin.coin && this.props.ActiveCoin.mode !== 'native' && !this.props.ActiveCoin.send && !this.props.ActiveCoin.receive) { return (

    {translate('INDEX.TRANSACTION_HISTORY')}

    {this.renderAddressList()}
    {this.renderUseCacheToggle}
    {this.renderPaginationItemsPerPageSelector()}
    {this.renderTxHistoryList()}
    {translate('INDEX.DIRECTION')} {translate('INDEX.CONFIRMATIONS')} {translate('INDEX.AMOUNT')} {translate('INDEX.TIME')} {translate('INDEX.DEST_ADDRESS')} {translate('INDEX.TX_DETAIL')}
    {translate('INDEX.DIRECTION')} {translate('INDEX.CONFIRMATIONS')} {translate('INDEX.AMOUNT')} {translate('INDEX.TIME')} {translate('INDEX.DEST_ADDRESS')} {translate('INDEX.TX_DETAIL')}
    {this.renderPagination()}
    ); } else { return null; } } } export default WalletsData;