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 #2716 from LiskHQ/2510-implement-load-new-blocks-b…
- Loading branch information
Showing
11 changed files
with
216 additions
and
16 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { PrimaryButton } from '../../toolbox/buttons/button'; | ||
import Icon from '../../toolbox/icon'; | ||
import useServiceSocketUpdates from '../../../hooks/useServiceSocketUpdates'; | ||
import styles from './loadLatestButton.css'; | ||
|
||
const LoadLatestButton = ({ children, onClick, event }) => { | ||
const [isUpdateAvailable, hideUpdateButton] = useServiceSocketUpdates(event); | ||
const [hasUpdatedRecently, didUpdateRecently] = useState(false); | ||
|
||
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; |
24 changes: 24 additions & 0 deletions
24
src/components/shared/loadLatestButton/loadLatestButton.css
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,24 @@ | ||
@keyframes slideDown { | ||
0% { | ||
transform: translateX(-50%) translateY(0px); | ||
} | ||
|
||
100% { | ||
transform: translateX(-50%) translateY(100px); | ||
} | ||
} | ||
|
||
.button { | ||
animation: slideDown var(--animation-speed-fast) ease-in; | ||
animation-fill-mode: forwards; | ||
position: absolute; | ||
z-index: 2; | ||
width: 200px; | ||
transform: translateX(-50%); | ||
left: 50%; | ||
|
||
& .icon { | ||
vertical-align: bottom; | ||
padding-right: 12px; | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
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 |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { act } from 'react-dom/test-utils'; | ||
import React from 'react'; | ||
import { mount } from 'enzyme'; | ||
import LoadLatestButton from '.'; | ||
import liskService from '../../../utils/api/lsk/liskService'; | ||
|
||
jest.mock('../../../utils/api/lsk/liskService'); | ||
|
||
describe('LoadLatestButton', () => { | ||
let onSocketEvent; | ||
const props = { | ||
onClick: jest.fn(), | ||
event: 'test.event', | ||
children: 'Test load button', | ||
}; | ||
const render = () => { | ||
liskService.listenToBlockchainEvents.mockImplementation(({ callback }) => { | ||
onSocketEvent = callback; | ||
}); | ||
const wrapper = mount(<LoadLatestButton {...props} />); | ||
return wrapper; | ||
}; | ||
|
||
it('renders empty by default', () => { | ||
const wrapper = render(); | ||
expect(wrapper).toBeEmptyRender(); | ||
}); | ||
|
||
it('shows button on websocket event and hides the button on click', () => { | ||
jest.useFakeTimers(); | ||
|
||
const wrapper = render(); | ||
expect(wrapper).toBeEmptyRender(); | ||
|
||
act(() => { onSocketEvent(); }); | ||
wrapper.update(); | ||
expect(wrapper).toContainExactlyOneMatchingElement('button'); | ||
expect(wrapper).toHaveText(props.children); | ||
|
||
wrapper.find('button').simulate('click'); | ||
expect(props.onClick).toHaveBeenCalledWith(); | ||
act(() => { | ||
jest.runOnlyPendingTimers(); | ||
}); | ||
expect(wrapper).toBeEmptyRender(); | ||
}); | ||
|
||
it('clears the timeout before unmounting', () => { | ||
jest.useFakeTimers(); | ||
const wrapper = render(); | ||
wrapper.unmount(); | ||
expect(clearTimeout).toHaveBeenCalledTimes(1); | ||
}); | ||
}); |
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 |
---|---|---|
|
@@ -120,6 +120,7 @@ | |
margin: 0; | ||
padding: 16px 20px; | ||
width: 100%; | ||
z-index: 3; | ||
|
||
& h1, | ||
& h2 { | ||
|
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 |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useSelector } from 'react-redux'; | ||
import { useState, useEffect } from 'react'; | ||
import liskService from '../utils/api/lsk/liskService'; | ||
|
||
/** | ||
* | ||
* @param {object} event - Sock event data fired by Lisk Service | ||
* @returns {array} - [boolean, function] | ||
*/ | ||
const useServiceSocketUpdates = (event) => { | ||
const networkConfig = useSelector(state => state.network); | ||
const [isUpdateAvailable, setUpdateAvailable] = useState(false); | ||
const reset = () => setUpdateAvailable(false); | ||
|
||
useEffect(() => { | ||
const cleanUp = liskService.listenToBlockchainEvents({ | ||
networkConfig, | ||
event, | ||
callback: () => setUpdateAvailable(true), | ||
}); | ||
|
||
return cleanUp; | ||
}, [networkConfig.name]); | ||
|
||
return [isUpdateAvailable, reset]; | ||
}; | ||
|
||
export default useServiceSocketUpdates; |
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