Merge pull request #2776 from poanetwork/ab-fetch-token-counters-async
fetch token counters asyncab-change-primary-key-for-internal-transactions
commit
46e95a16aa
@ -0,0 +1,87 @@ |
||||
import $ from 'jquery' |
||||
import omit from 'lodash/omit' |
||||
import humps from 'humps' |
||||
import { createStore, connectElements } from '../lib/redux_helpers.js' |
||||
|
||||
export const initialState = { |
||||
channelDisconnected: false, |
||||
transferCount: null, |
||||
tokenHolderCount: null |
||||
} |
||||
|
||||
export function reducer (state = initialState, action) { |
||||
switch (action.type) { |
||||
case 'PAGE_LOAD': |
||||
case 'ELEMENTS_LOAD': { |
||||
return Object.assign({}, state, omit(action, 'type')) |
||||
} |
||||
case 'CHANNEL_DISCONNECTED': { |
||||
if (state.beyondPageOne) return state |
||||
|
||||
return Object.assign({}, state, { |
||||
channelDisconnected: true |
||||
}) |
||||
} |
||||
case 'COUNTERS_FETCHED': { |
||||
return Object.assign({}, state, { |
||||
transferCount: action.transferCount, |
||||
tokenHolderCount: action.tokenHolderCount |
||||
}) |
||||
} |
||||
default: |
||||
return state |
||||
} |
||||
} |
||||
|
||||
const elements = { |
||||
'[data-page="counters"]': { |
||||
render ($el, state) { |
||||
if (state.counters) { |
||||
return $el |
||||
} |
||||
return $el |
||||
} |
||||
}, |
||||
'[token-transfer-count]': { |
||||
render ($el, state) { |
||||
if (state.transferCount) { |
||||
$el.text(state.transferCount + ' Transfers') |
||||
return $el.show() |
||||
} else { |
||||
return $el.hide() |
||||
} |
||||
} |
||||
}, |
||||
'[token-holder-count]': { |
||||
render ($el, state) { |
||||
if (state.tokenHolderCount) { |
||||
$el.text(state.tokenHolderCount + ' Addresses') |
||||
return $el.show() |
||||
} else { |
||||
return $el.hide() |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
function loadCounters (store) { |
||||
const $element = $('[data-async-counters]') |
||||
const path = $element.data().asyncCounters |
||||
function fetchCounters () { |
||||
store.dispatch({type: 'START_REQUEST'}) |
||||
$.getJSON(path) |
||||
.done(response => store.dispatch(Object.assign({type: 'COUNTERS_FETCHED'}, humps.camelizeKeys(response)))) |
||||
.fail(() => store.dispatch({type: 'REQUEST_ERROR'})) |
||||
.always(() => store.dispatch({type: 'FINISH_REQUEST'})) |
||||
} |
||||
|
||||
fetchCounters() |
||||
} |
||||
|
||||
const $tokenPage = $('[token-page]') |
||||
|
||||
if ($tokenPage.length) { |
||||
const store = createStore(reducer) |
||||
connectElements({ store, elements }) |
||||
loadCounters(store) |
||||
} |
@ -0,0 +1,36 @@ |
||||
defmodule BlockScoutWeb.Tokens.TokenControllerTest do |
||||
use BlockScoutWeb.ConnCase, async: true |
||||
|
||||
describe "GET show/2" do |
||||
test "redirects to transfers page", %{conn: conn} do |
||||
contract_address = insert(:address) |
||||
|
||||
conn = get(conn, token_path(conn, :show, contract_address.hash)) |
||||
|
||||
assert conn.status == 302 |
||||
end |
||||
end |
||||
|
||||
describe "GET token_counters/2" do |
||||
test "returns token counters", %{conn: conn} do |
||||
contract_address = insert(:address) |
||||
|
||||
insert(:token, contract_address: contract_address) |
||||
|
||||
token_id = 10 |
||||
|
||||
insert(:token_transfer, |
||||
from_address: contract_address, |
||||
token_contract_address: contract_address, |
||||
token_id: token_id |
||||
) |
||||
|
||||
conn = get(conn, "/token_counters", %{"id" => to_string(contract_address.hash)}) |
||||
|
||||
assert conn.status == 200 |
||||
{:ok, response} = Jason.decode(conn.resp_body) |
||||
|
||||
assert %{"token_holder_count" => 0, "transfer_count" => 1} == response |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue