parent
569a1cc59f
commit
17f5c9a742
@ -0,0 +1,14 @@ |
|||||||
|
import { reducer, initialState } from '../../js/pages/transaction' |
||||||
|
|
||||||
|
test('RECEIVED_UPDATED_CONFIRMATIONS', () => { |
||||||
|
const state = initialState |
||||||
|
const action = { |
||||||
|
type: 'RECEIVED_UPDATED_CONFIRMATIONS', |
||||||
|
msg: { |
||||||
|
confirmations: 5 |
||||||
|
} |
||||||
|
} |
||||||
|
const output = reducer(state, action) |
||||||
|
|
||||||
|
expect(output.confirmations).toBe(5) |
||||||
|
}) |
@ -0,0 +1,38 @@ |
|||||||
|
import $ from 'jquery' |
||||||
|
import numeral from 'numeral' |
||||||
|
import 'numeral/locales' |
||||||
|
import socket from '../socket' |
||||||
|
import router from '../router' |
||||||
|
import { initRedux } from '../utils' |
||||||
|
|
||||||
|
export const initialState = {confirmations: null} |
||||||
|
|
||||||
|
export function reducer (state = initialState, action) { |
||||||
|
switch (action.type) { |
||||||
|
case 'RECEIVED_UPDATED_CONFIRMATIONS': { |
||||||
|
return Object.assign({}, state, { |
||||||
|
confirmations: action.msg.confirmations |
||||||
|
}) |
||||||
|
} |
||||||
|
default: |
||||||
|
return state |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
router.when('/transactions/:transactionHash').then((params) => initRedux(reducer, { |
||||||
|
main (store) { |
||||||
|
const { transactionHash, locale } = params |
||||||
|
const channel = socket.channel(`transactions:${transactionHash}`, {}) |
||||||
|
numeral.locale(locale) |
||||||
|
channel.join() |
||||||
|
.receive('ok', resp => { console.log('Joined successfully', `transactions:${transactionHash}`, resp) }) |
||||||
|
.receive('error', resp => { console.log('Unable to join', `transactions:${transactionHash}`, resp) }) |
||||||
|
channel.on('confirmations', (msg) => store.dispatch({ type: 'RECEIVED_UPDATED_CONFIRMATIONS', msg })) |
||||||
|
}, |
||||||
|
render (state, oldState) { |
||||||
|
const $blockConfirmations = $('[data-selector="block_confirmations"]') |
||||||
|
if (oldState.confirmations !== state.confirmations) { |
||||||
|
$blockConfirmations.empty().append(numeral(msg.confirmations).format()) |
||||||
|
} |
||||||
|
} |
||||||
|
})) |
@ -0,0 +1,32 @@ |
|||||||
|
defmodule ExplorerWeb.TransactionChannel do |
||||||
|
@moduledoc """ |
||||||
|
Establishes pub/sub channel for transaction page live updates. |
||||||
|
""" |
||||||
|
use ExplorerWeb, :channel |
||||||
|
|
||||||
|
alias ExplorerWeb.TransactionView |
||||||
|
alias Phoenix.View |
||||||
|
|
||||||
|
intercept(["confirmations"]) |
||||||
|
|
||||||
|
def join("transactions:" <> _transaction_hash, _params, socket) do |
||||||
|
{:ok, %{}, socket} |
||||||
|
end |
||||||
|
|
||||||
|
def handle_out("confirmations", %{max_block_number: max_block_number, transaction: transaction}, socket) do |
||||||
|
Gettext.put_locale(ExplorerWeb.Gettext, socket.assigns.locale) |
||||||
|
|
||||||
|
rendered = |
||||||
|
View.render_to_string( |
||||||
|
TransactionView, |
||||||
|
"_confirmations.html", |
||||||
|
locale: socket.assigns.locale, |
||||||
|
max_block_number: max_block_number, |
||||||
|
transaction: transaction |
||||||
|
) |
||||||
|
|
||||||
|
push(socket, "confirmations", %{confirmations: rendered}) |
||||||
|
|
||||||
|
{:noreply, socket} |
||||||
|
end |
||||||
|
end |
@ -0,0 +1 @@ |
|||||||
|
(<%= gettext "%{confirmations} block confirmations", confirmations: confirmations(@transaction, max_block_number: @max_block_number) %>) |
@ -0,0 +1,31 @@ |
|||||||
|
defmodule ExplorerWeb.AddressTransactionTest do |
||||||
|
use ExplorerWeb.ChannelCase |
||||||
|
|
||||||
|
describe "transactions channel tests" do |
||||||
|
test "subscribed user can receive block confirmations event" do |
||||||
|
channel = "transactions" |
||||||
|
@endpoint.subscribe(channel) |
||||||
|
|
||||||
|
block = insert(:block, number: 1) |
||||||
|
|
||||||
|
transaction = |
||||||
|
:transaction |
||||||
|
|> insert() |
||||||
|
|> with_block(block) |
||||||
|
|
||||||
|
ExplorerWeb.Endpoint.broadcast(channel, "confirmations", %{max_block_number: 3, transaction: transaction}) |
||||||
|
|
||||||
|
receive do |
||||||
|
%Phoenix.Socket.Broadcast{ |
||||||
|
event: "confirmations", |
||||||
|
topic: ^channel, |
||||||
|
payload: %{max_block_number: 3, transaction: ^transaction} |
||||||
|
} -> |
||||||
|
assert true |
||||||
|
after |
||||||
|
5_000 -> |
||||||
|
assert false, "Expected message received nothing." |
||||||
|
end |
||||||
|
end |
||||||
|
end |
||||||
|
end |
Loading…
Reference in new issue