You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
116 lines
4.3 KiB
116 lines
4.3 KiB
7 years ago
|
import $ from 'jquery'
|
||
7 years ago
|
import humps from 'humps'
|
||
7 years ago
|
import numeral from 'numeral'
|
||
|
import 'numeral/locales'
|
||
7 years ago
|
import socket from '../socket'
|
||
7 years ago
|
import router from '../router'
|
||
7 years ago
|
import { batchChannel, initRedux } from '../utils'
|
||
6 years ago
|
import { updateAllAges } from '../lib/from_now'
|
||
7 years ago
|
|
||
7 years ago
|
const BATCH_THRESHOLD = 10
|
||
|
|
||
7 years ago
|
export const initialState = {
|
||
7 years ago
|
addressHash: null,
|
||
6 years ago
|
batchCountAccumulator: 0,
|
||
7 years ago
|
beyondPageOne: null,
|
||
|
channelDisconnected: false,
|
||
6 years ago
|
filter: null,
|
||
7 years ago
|
newTransactions: [],
|
||
6 years ago
|
balance: null,
|
||
6 years ago
|
transactionCount: null
|
||
7 years ago
|
}
|
||
7 years ago
|
|
||
7 years ago
|
export function reducer (state = initialState, action) {
|
||
|
switch (action.type) {
|
||
|
case 'PAGE_LOAD': {
|
||
|
return Object.assign({}, state, {
|
||
|
addressHash: action.params.addressHash,
|
||
6 years ago
|
beyondPageOne: !!action.params.blockNumber,
|
||
7 years ago
|
filter: action.params.filter,
|
||
6 years ago
|
transactionCount: numeral(action.transactionCount).value()
|
||
7 years ago
|
})
|
||
|
}
|
||
|
case 'CHANNEL_DISCONNECTED': {
|
||
|
if (state.beyondPageOne) return state
|
||
7 years ago
|
|
||
7 years ago
|
return Object.assign({}, state, {
|
||
|
channelDisconnected: true,
|
||
|
batchCountAccumulator: 0
|
||
|
})
|
||
|
}
|
||
6 years ago
|
case 'RECEIVED_UPDATED_BALANCE': {
|
||
7 years ago
|
return Object.assign({}, state, {
|
||
6 years ago
|
balance: action.msg.balance
|
||
7 years ago
|
})
|
||
|
}
|
||
7 years ago
|
case 'RECEIVED_NEW_TRANSACTION_BATCH': {
|
||
|
if (state.channelDisconnected || state.beyondPageOne) return state
|
||
7 years ago
|
|
||
7 years ago
|
const incomingTransactions = humps.camelizeKeys(action.msgs)
|
||
|
.filter(({toAddressHash, fromAddressHash}) => (
|
||
|
!state.filter ||
|
||
|
(state.filter === 'to' && toAddressHash === state.addressHash) ||
|
||
|
(state.filter === 'from' && fromAddressHash === state.addressHash)
|
||
|
))
|
||
|
|
||
|
if (!state.batchCountAccumulator && action.msgs.length < BATCH_THRESHOLD) {
|
||
|
return Object.assign({}, state, {
|
||
|
newTransactions: [
|
||
|
...state.newTransactions,
|
||
|
...incomingTransactions.map(({transactionHtml}) => transactionHtml)
|
||
6 years ago
|
],
|
||
|
transactionCount: state.transactionCount + action.msgs.length
|
||
7 years ago
|
})
|
||
|
} else {
|
||
|
return Object.assign({}, state, {
|
||
6 years ago
|
batchCountAccumulator: state.batchCountAccumulator + action.msgs.length,
|
||
|
transactionCount: state.transactionCount + action.msgs.length
|
||
7 years ago
|
})
|
||
|
}
|
||
|
}
|
||
|
default:
|
||
|
return state
|
||
|
}
|
||
|
}
|
||
|
|
||
|
router.when('/addresses/:addressHash').then((params) => initRedux(reducer, {
|
||
|
main (store) {
|
||
|
const { addressHash, blockNumber, locale } = params
|
||
|
const channel = socket.channel(`addresses:${addressHash}`, {})
|
||
|
numeral.locale(locale)
|
||
6 years ago
|
store.dispatch({
|
||
|
type: 'PAGE_LOAD',
|
||
|
params,
|
||
|
transactionCount: $('[data-selector="transaction-count"]').text()
|
||
|
})
|
||
7 years ago
|
channel.join()
|
||
|
channel.onError(() => store.dispatch({ type: 'CHANNEL_DISCONNECTED' }))
|
||
6 years ago
|
channel.on('balance', (msg) => store.dispatch({ type: 'RECEIVED_UPDATED_BALANCE', msg }))
|
||
7 years ago
|
if (!blockNumber) channel.on('transaction', batchChannel((msgs) => store.dispatch({ type: 'RECEIVED_NEW_TRANSACTION_BATCH', msgs })))
|
||
|
},
|
||
|
render (state, oldState) {
|
||
6 years ago
|
const $channelBatching = $('[data-selector="channel-batching-message"]')
|
||
|
const $channelBatchingCount = $('[data-selector="channel-batching-count"]')
|
||
7 years ago
|
const $channelDisconnected = $('[data-selector="channel-disconnected-message"]')
|
||
|
const $emptyTransactionsList = $('[data-selector="empty-transactions-list"]')
|
||
6 years ago
|
const $balance = $('[data-selector="balance-card"]')
|
||
6 years ago
|
const $transactionCount = $('[data-selector="transaction-count"]')
|
||
7 years ago
|
const $transactionsList = $('[data-selector="transactions-list"]')
|
||
7 years ago
|
|
||
7 years ago
|
if ($emptyTransactionsList.length && state.newTransactions.length) window.location.reload()
|
||
|
if (state.channelDisconnected) $channelDisconnected.show()
|
||
6 years ago
|
if (oldState.balance !== state.balance) $balance.empty().append(state.balance)
|
||
6 years ago
|
if (oldState.transactionCount !== state.transactionCount) $transactionCount.empty().append(numeral(state.transactionCount).format())
|
||
7 years ago
|
if (state.batchCountAccumulator) {
|
||
|
$channelBatching.show()
|
||
|
$channelBatchingCount[0].innerHTML = numeral(state.batchCountAccumulator).format()
|
||
|
} else {
|
||
|
$channelBatching.hide()
|
||
|
}
|
||
|
if (oldState.newTransactions !== state.newTransactions && $transactionsList.length) {
|
||
|
$transactionsList.prepend(state.newTransactions.slice(oldState.newTransactions.length).reverse().join(''))
|
||
6 years ago
|
updateAllAges()
|
||
7 years ago
|
}
|
||
7 years ago
|
}
|
||
7 years ago
|
}))
|