This repository has been archived by the owner on Apr 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
Migrate global loading bar to React - Closes #494 #538
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e8fe44b
Add global loading bar
slaweet cad3526
Fix some issues of loadingBar component
slaweet 6b7598a
Add actions, reducers and utils for loading bar
slaweet bbeb24d
Show loading bar on each api request
slaweet 7b5f081
Add unit tests for loadingBar
slaweet 0b3b485
Cover the case when loading is undefined
slaweet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import actionTypes from '../constants/actions'; | ||
|
||
/** | ||
* An action to dispatch loadingStarted | ||
* | ||
*/ | ||
export const loadingStarted = data => ({ | ||
data, | ||
type: actionTypes.loadingStarted, | ||
}); | ||
|
||
/** | ||
* An action to dispatch loadingFinished | ||
* | ||
*/ | ||
export const loadingFinished = data => ({ | ||
data, | ||
type: actionTypes.loadingFinished, | ||
}); |
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,33 @@ | ||
import { expect } from 'chai'; | ||
import actionTypes from '../constants/actions'; | ||
import { | ||
loadingStarted, | ||
loadingFinished, | ||
} from './loading'; | ||
|
||
|
||
describe('actions: loading', () => { | ||
describe('loadingStarted', () => { | ||
it('should create an action to show loading bar', () => { | ||
const data = 'test'; | ||
|
||
const expectedAction = { | ||
data, | ||
type: actionTypes.loadingStarted, | ||
}; | ||
expect(loadingStarted(data)).to.be.deep.equal(expectedAction); | ||
}); | ||
}); | ||
|
||
describe('loadingFinished', () => { | ||
it('should create an action to hide loading bar', () => { | ||
const data = 'test'; | ||
|
||
const expectedAction = { | ||
data, | ||
type: actionTypes.loadingFinished, | ||
}; | ||
expect(loadingFinished(data)).to.be.deep.equal(expectedAction); | ||
}); | ||
}); | ||
}); |
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,10 @@ | ||
|
||
import { connect } from 'react-redux'; | ||
import LoadingBar from './loadingBar'; | ||
|
||
const mapStateToProps = state => ({ | ||
loading: state.loading, | ||
}); | ||
|
||
export default connect(mapStateToProps)(LoadingBar); | ||
|
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,19 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { mount } from 'enzyme'; | ||
import { Provider } from 'react-redux'; | ||
import LoadingBar from './'; | ||
import store from '../../store'; | ||
|
||
|
||
describe('LoadingBar Container', () => { | ||
let wrapper; | ||
|
||
beforeEach(() => { | ||
wrapper = mount(<Provider store={store}><LoadingBar /></Provider>); | ||
}); | ||
|
||
it('should render Send', () => { | ||
expect(wrapper.find('LoadingBar')).to.have.lengthOf(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.fixedAtTop { | ||
position: fixed; | ||
top: -11px; | ||
right: 0; | ||
width: 100vw; | ||
z-index: 201; | ||
} |
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,14 @@ | ||
import React from 'react'; | ||
import ProgressBar from 'react-toolbox/lib/progress_bar'; | ||
import styles from './loadingBar.css'; | ||
|
||
const LoadingBar = props => ( | ||
<div className={styles.fixedAtTop}> | ||
{props.loading && props.loading.length ? | ||
<ProgressBar type="linear" mode="indeterminate" /> : | ||
null | ||
} | ||
</div> | ||
); | ||
|
||
export default LoadingBar; |
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,17 @@ | ||
import React from 'react'; | ||
import { expect } from 'chai'; | ||
import { mount } from 'enzyme'; | ||
import LoadingBar from './loadingBar'; | ||
|
||
|
||
describe('LoadingBar Container', () => { | ||
it('should show ProgresBar if props.loading.length is not 0', () => { | ||
const wrapper = mount(<LoadingBar loading={['test']} />); | ||
expect(wrapper.find('ProgressBar')).to.have.lengthOf(1); | ||
}); | ||
|
||
it('should not show ProgresBar if props.loading.length is 0', () => { | ||
const wrapper = mount(<LoadingBar loading={[]} />); | ||
expect(wrapper.find('ProgressBar')).to.have.lengthOf(0); | ||
}); | ||
}); |
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 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,19 @@ | ||
import actionTypes from '../../constants/actions'; | ||
|
||
/** | ||
* | ||
* @param {Array} state | ||
* @param {Object} action | ||
*/ | ||
const dialog = (state = [], action) => { | ||
switch (action.type) { | ||
case actionTypes.loadingStarted: | ||
return [...state, action.data]; | ||
case actionTypes.loadingFinished: | ||
return state.filter(item => item !== action.data); | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
export default dialog; |
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 chai, { expect } from 'chai'; | ||
import sinonChai from 'sinon-chai'; | ||
import loading from './loading'; | ||
import actionTypes from '../../constants/actions'; | ||
|
||
|
||
chai.use(sinonChai); | ||
|
||
describe('Reducer: loading(state, action)', () => { | ||
let state; | ||
|
||
beforeEach(() => { | ||
state = ['test1', 'test2']; | ||
}); | ||
|
||
it('should return loading array with the new loading if action.type = actionTypes.loadingStarted', () => { | ||
const action = { | ||
type: actionTypes.loadingStarted, | ||
data: 'test3', | ||
}; | ||
const changedState = loading(state, action); | ||
expect(changedState).to.deep.equal([...state, action.data]); | ||
}); | ||
|
||
it('should return loading array without action.data if action.type = actionTypes.loadingFinished', () => { | ||
const action = { | ||
type: actionTypes.loadingFinished, | ||
data: 'test1', | ||
}; | ||
const changedState = loading(state, action); | ||
expect(changedState).to.deep.equal(['test2']); | ||
}); | ||
}); | ||
|
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,12 +1,16 @@ | ||
import { loadingStarted, loadingFinished } from '../../utils/loading'; | ||
|
||
/* eslint-disable */ | ||
export const requestToActivePeer = (activePeer, path, urlParams) => | ||
new Promise((resolve, reject) => { | ||
loadingStarted(path); | ||
activePeer.sendRequest(path, urlParams, (data) => { | ||
if (data.success) { | ||
resolve(data); | ||
} else { | ||
reject(data); | ||
} | ||
loadingFinished(path); | ||
}); | ||
}); | ||
/* eslint-enable */ |
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,7 @@ | ||
import { loadingStarted as loadingStartedAction, loadingFinished as loadingFinishedAction } from '../actions/loading'; | ||
import store from '../store'; | ||
|
||
|
||
export const loadingStarted = data => store.dispatch(loadingStartedAction(data)); | ||
|
||
export const loadingFinished = data => store.dispatch(loadingFinishedAction(data)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use loading actions inside our component instead of using them inside our api utils