Merge pull request #1167 from poanetwork/wsa-async-load-blocks-validated
Blocks Validated page async loadpull/1170/head v1.2.1-beta
commit
4236b39a20
@ -0,0 +1,46 @@ |
|||||||
|
import { reducer, initialState } from '../../../js/pages/address/validations' |
||||||
|
|
||||||
|
describe('RECEIVED_NEW_BLOCK', () => { |
||||||
|
test('adds new block to the top of the list', () => { |
||||||
|
const state = Object.assign({}, initialState, { |
||||||
|
items: ['test 1'] |
||||||
|
}) |
||||||
|
const action = { |
||||||
|
type: 'RECEIVED_NEW_BLOCK', |
||||||
|
blockHtml: 'test 2' |
||||||
|
} |
||||||
|
const output = reducer(state, action) |
||||||
|
|
||||||
|
expect(output.items).toEqual(['test 2', 'test 1']) |
||||||
|
}) |
||||||
|
|
||||||
|
test('does nothing beyond page one', () => { |
||||||
|
const state = Object.assign({}, initialState, { |
||||||
|
beyondPageOne: true, |
||||||
|
channelDisconnected: false, |
||||||
|
items: ['test 1'] |
||||||
|
}) |
||||||
|
const action = { |
||||||
|
type: 'RECEIVED_NEW_BLOCK', |
||||||
|
blockHtml: 'test 2' |
||||||
|
} |
||||||
|
const output = reducer(state, action) |
||||||
|
|
||||||
|
expect(output.items).toEqual(['test 1']) |
||||||
|
}) |
||||||
|
|
||||||
|
test('does nothing when channel has been disconnected', () => { |
||||||
|
const state = Object.assign({}, initialState, { |
||||||
|
channelDisconnected: true, |
||||||
|
items: ['test 1'] |
||||||
|
}) |
||||||
|
const action = { |
||||||
|
type: 'RECEIVED_NEW_BLOCK', |
||||||
|
blockHtml: 'test 2' |
||||||
|
} |
||||||
|
const output = reducer(state, action) |
||||||
|
|
||||||
|
expect(output.items).toEqual(['test 1']) |
||||||
|
}) |
||||||
|
}) |
||||||
|
|
@ -0,0 +1,64 @@ |
|||||||
|
import $ from 'jquery' |
||||||
|
import _ from 'lodash' |
||||||
|
import humps from 'humps' |
||||||
|
import socket from '../../socket' |
||||||
|
import { connectElements } from '../../lib/redux_helpers.js' |
||||||
|
import { createAsyncLoadStore } from '../../lib/async_listing_load.js' |
||||||
|
|
||||||
|
export const initialState = { |
||||||
|
addressHash: null, |
||||||
|
channelDisconnected: false |
||||||
|
} |
||||||
|
|
||||||
|
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': { |
||||||
|
return Object.assign({}, state, { channelDisconnected: true }) |
||||||
|
} |
||||||
|
case 'RECEIVED_NEW_BLOCK': { |
||||||
|
if (state.channelDisconnected) return state |
||||||
|
if (state.beyondPageOne) return state |
||||||
|
|
||||||
|
return Object.assign({}, state, { |
||||||
|
items: [ |
||||||
|
action.blockHtml, |
||||||
|
...state.items |
||||||
|
] |
||||||
|
}) |
||||||
|
} |
||||||
|
default: |
||||||
|
return state |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
const elements = { |
||||||
|
'[data-selector="channel-disconnected-message"]': { |
||||||
|
render ($el, state) { |
||||||
|
if (state.channelDisconnected) $el.show() |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
if ($('[data-page="blocks-validated"]').length) { |
||||||
|
const store = createAsyncLoadStore(reducer, initialState, 'dataset.blockNumber') |
||||||
|
connectElements({ store, elements }) |
||||||
|
const addressHash = $('[data-page="address-details"]')[0].dataset.pageAddressHash |
||||||
|
store.dispatch({ |
||||||
|
type: 'PAGE_LOAD', |
||||||
|
addressHash |
||||||
|
}) |
||||||
|
|
||||||
|
const blocksChannel = socket.channel(`blocks:${addressHash}`, {}) |
||||||
|
blocksChannel.join() |
||||||
|
blocksChannel.onError(() => store.dispatch({ |
||||||
|
type: 'CHANNEL_DISCONNECTED' |
||||||
|
})) |
||||||
|
blocksChannel.on('new_block', (msg) => store.dispatch({ |
||||||
|
type: 'RECEIVED_NEW_BLOCK', |
||||||
|
blockHtml: humps.camelizeKeys(msg).blockHtml |
||||||
|
})) |
||||||
|
} |
Loading…
Reference in new issue