Skip to content
This repository has been archived by the owner on Apr 15, 2019. It is now read-only.

Commit

Permalink
Fix search and remove unnecessary setStates
Browse files Browse the repository at this point in the history
  • Loading branch information
reyraa committed Sep 17, 2017
1 parent 287594f commit 56f8bb6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
30 changes: 11 additions & 19 deletions src/components/voting/voting.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,8 @@ const Table = themr(TABLE, TableTheme)(tableFactory(TableHead, VotingRow));
class Voting extends React.Component {
constructor() {
super();
this.state = {
delegates: [],
selected: [],
length: 1,
notFound: '',
};
this.freezeLoading = false;
this.isInitial = true;
this.offset = -1;
this.query = '';
}
Expand All @@ -35,6 +30,7 @@ class Voting extends React.Component {
setTimeout(() => {
this.freezeLoading = false;
this.offset = nextProps.delegates.length;
this.isInitial = false;
}, 5);
}
}
Expand All @@ -60,15 +56,9 @@ class Voting extends React.Component {
*/
search(query) {
this.query = query;
this.setState({
offset: 0,
delegates: [],
length: 1,
});
this.offset = 0;
this.freezeLoading = false;
setTimeout(() => {
this.loadDelegates(this.query);
}, 1);
this.loadDelegates(query, true);
}

/**
Expand All @@ -79,13 +69,13 @@ class Voting extends React.Component {
* should replace the old delegates list
* @param {Number} limit - The maximum number of results
*/
loadDelegates(search = '', refresh) {
loadDelegates(q = '', refresh) {
this.freezeLoading = true;
this.offset = refresh ? -1 : this.offset;
this.props.delegatesFetched({
activePeer: this.props.activePeer,
offset: this.offset > -1 ? this.offset : 0,
q: search || '',
q,
refresh,
});
}
Expand All @@ -101,7 +91,6 @@ class Voting extends React.Component {
}

render() {
// .log(this.props.votes.cc001);
return (
<div className="box noPaddingBox">
<Header
Expand Down Expand Up @@ -129,10 +118,13 @@ class Voting extends React.Component {
))}
</Table>
</div>
{this.state.notFound}
{
(!this.isInitial && this.props.delegates.length === 0) &&
<div className='hasPaddingRow empty-message'>No delegates found</div>
}
<Waypoint bottomOffset='-80%'
scrollableAncestor={window}
key={this.state.delegates.length}
key={this.props.delegates.length}
onEnter={this.loadMore.bind(this)}></Waypoint>
</div>
);
Expand Down
3 changes: 2 additions & 1 deletion src/components/voting/voting.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ describe('Voting', () => {

it('should define search method to reload delegates based on given query', () => {
const clock = sinon.useFakeTimers();
props.delegatesFetched.reset();
wrapper.instance().search('query');
clock.tick(2);
expect(props.delegatesFetched).to.be.calledWith({
activePeer: props.activePeer,
offset: 0,
q: 'query',
refresh: undefined,
refresh: true,
});
clock.restore();
});
Expand Down

0 comments on commit 56f8bb6

Please sign in to comment.