-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3645 from LiskHQ/3643-new-block-and-tx-buttons
Fix new block and new transaction buttons - Closes #3643
- Loading branch information
Showing
9 changed files
with
104 additions
and
108 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,8 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { PrimaryButton } from '@toolbox/buttons'; | ||
import Icon from '@toolbox/icon'; | ||
import useServiceSocketUpdates from '../../../hooks/useServiceSocketUpdates'; | ||
import styles from './loadLatestButton.css'; | ||
import { connect } from 'react-redux'; | ||
import LoadLatestButton from './loadLatestButton'; | ||
|
||
const LoadLatestButton = ({ children, onClick, event }) => { | ||
const [isUpdateAvailable, hideUpdateButton] = useServiceSocketUpdates(event); | ||
const [hasUpdatedRecently, didUpdateRecently] = useState(false); | ||
const mapStateToProps = state => ({ | ||
latestBlocks: state.blocks.latestBlocks, | ||
}); | ||
|
||
const handleClick = () => { | ||
hideUpdateButton(); | ||
const timer = setTimeout(() => { | ||
didUpdateRecently(false); | ||
}, 10000); | ||
didUpdateRecently(timer); | ||
onClick(); | ||
}; | ||
|
||
// willUnmount | ||
useEffect(() => () => clearTimeout(hasUpdatedRecently), []); | ||
|
||
return (isUpdateAvailable && hasUpdatedRecently === false | ||
? ( | ||
<PrimaryButton onClick={handleClick} className={styles.button}> | ||
<Icon name="arrowUpCircle" className={styles.icon} /> | ||
{children} | ||
</PrimaryButton> | ||
) | ||
: null); | ||
}; | ||
|
||
export default LoadLatestButton; | ||
export default connect(mapStateToProps)(LoadLatestButton); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/components/shared/loadLatestButton/loadLatestButton.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { PrimaryButton } from '@toolbox/buttons'; | ||
import Icon from '@toolbox/icon'; | ||
import styles from './loadLatestButton.css'; | ||
|
||
const shouldShow = { | ||
block: (updateHeight, latestBlocks) => ( | ||
latestBlocks.length > 0 | ||
&& updateHeight > 0 | ||
&& latestBlocks[0].height > (updateHeight + 2) | ||
), | ||
transaction: (updateHeight, latestBlocks) => ( | ||
latestBlocks.length > 0 | ||
&& updateHeight > 0 | ||
&& latestBlocks[0].height > updateHeight | ||
&& latestBlocks[0].numberOfTransactions > 0 | ||
), | ||
}; | ||
|
||
const LoadLatestButton = ({ | ||
children, onClick, entity, latestBlocks, | ||
}) => { | ||
const [updateHeight, setUpdateHeight] = useState( | ||
latestBlocks.length ? latestBlocks[0].height : 0, | ||
); | ||
|
||
const handleClick = () => { | ||
setUpdateHeight(latestBlocks[0].height); | ||
onClick(); | ||
}; | ||
|
||
useEffect(() => { | ||
if (latestBlocks.length > 0 && updateHeight === 0) { | ||
setUpdateHeight(latestBlocks[0].height); | ||
} | ||
}, [latestBlocks.length]); | ||
|
||
return shouldShow[entity] && shouldShow[entity](updateHeight, latestBlocks) | ||
? ( | ||
<PrimaryButton onClick={handleClick} className={styles.button}> | ||
<Icon name="refresh" className={styles.icon} /> | ||
<span>{children}</span> | ||
</PrimaryButton> | ||
) | ||
: null; | ||
}; | ||
|
||
LoadLatestButton.propTypes = { | ||
entity: PropTypes.oneOf(['block', 'transaction']), | ||
onClick: PropTypes.func.isRequired, | ||
}; | ||
|
||
export default LoadLatestButton; |
63 changes: 22 additions & 41 deletions
63
src/components/shared/loadLatestButton/loadLatestButton.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,47 @@ | ||
import { act } from 'react-dom/test-utils'; | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import io from 'socket.io-client'; | ||
import { subscribeConnections } from '@api/ws'; | ||
import LoadLatestButton from '.'; | ||
|
||
jest.mock('socket.io-client'); | ||
|
||
const on = (ev, callback) => { | ||
setTimeout(() => { | ||
callback(ev); | ||
}, 1); | ||
}; | ||
const close = jest.fn(); | ||
io.mockImplementation(() => ({ on, close })); | ||
import LoadLatestButton from './loadLatestButton'; | ||
|
||
describe('LoadLatestButton', () => { | ||
const props = { | ||
onClick: jest.fn(), | ||
event: 'test.event', | ||
entity: 'block', | ||
children: 'Test load button', | ||
}; | ||
const render = () => { | ||
const wrapper = mount(<LoadLatestButton {...props} />); | ||
return wrapper; | ||
latestBlocks: [ | ||
{ height: 11111111 }, | ||
], | ||
}; | ||
|
||
it('renders empty by default', () => { | ||
const wrapper = render(); | ||
it('renders accept transaction and block for entity and render empty by default', () => { | ||
const blockProps = { ...props, entity: 'block' }; | ||
let wrapper = mount(<LoadLatestButton {...blockProps} />); | ||
expect(wrapper).toBeEmptyRender(); | ||
}); | ||
|
||
it('shows button on websocket event and hides the button on click', () => { | ||
jest.useFakeTimers(); | ||
const transactionProps = { ...props, entity: 'transaction' }; | ||
wrapper = mount(<LoadLatestButton {...transactionProps} />); | ||
expect(wrapper).toBeEmptyRender(); | ||
}); | ||
|
||
const wrapper = render(); | ||
it('shows button when there is a new block', () => { | ||
const wrapper = mount(<LoadLatestButton {...props} />); | ||
expect(wrapper).toBeEmptyRender(); | ||
wrapper.setProps({ | ||
latestBlocks: [ | ||
{ height: 11111114 }, | ||
], | ||
}); | ||
act(() => { | ||
jest.runOnlyPendingTimers(); | ||
wrapper.update(); | ||
}); | ||
wrapper.update(); | ||
expect(wrapper).toContainExactlyOneMatchingElement('button'); | ||
expect(wrapper).toHaveText(props.children); | ||
|
||
wrapper.find('button').simulate('click'); | ||
wrapper.find('button').at(0).simulate('click'); | ||
expect(props.onClick).toHaveBeenCalledWith(); | ||
act(() => { | ||
jest.runOnlyPendingTimers(); | ||
wrapper.update(); | ||
}); | ||
expect(wrapper).toBeEmptyRender(); | ||
expect(subscribeConnections[props.event]).toBeDefined(); | ||
}); | ||
|
||
it('clears the timeout before unmounting', () => { | ||
jest.useFakeTimers(); | ||
const wrapper = render(); | ||
expect(subscribeConnections[props.event]).toBeDefined(); | ||
expect(close).toHaveBeenCalledTimes(0); | ||
|
||
wrapper.unmount(); | ||
expect(clearTimeout).toHaveBeenCalledTimes(1); | ||
expect(close).toHaveBeenCalledTimes(1); | ||
expect(subscribeConnections[props.event]).not.toBeDefined(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.