Skip to content

Commit

Permalink
Remember playlist item selection as new pages are loaded, fixes u-wav…
Browse files Browse the repository at this point in the history
  • Loading branch information
goto-bus-stop committed Jun 27, 2017
1 parent 6c56a47 commit 179d985
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/components/MediaList/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import itemSelection from 'item-selection/immutable';
import Row from './Row';
import LoadingRow from './LoadingRow';

/**
* Check if two media lists are different, taking into account
* pagination. If the new media list contains items where the previous
* list doesn't, but every other item is identical, we assume
* the new list has just loaded a page that wasn't loaded in the
* previous one, and decide that the list is not really different.
*/
function didMediaChange(prev, next) {
return prev.some((item, i) => item && next[i] && item._id !== next[i]._id);
}

export default class MediaList extends React.Component {
static propTypes = {
className: PropTypes.string,
Expand All @@ -31,8 +42,12 @@ export default class MediaList extends React.Component {

componentWillReceiveProps(nextProps) {
if (nextProps.media !== this.props.media) {
const selection = this.state.selection.getIndices();
const mediaChanged = didMediaChange(this.props.media, nextProps.media);
this.setState({
selection: itemSelection(nextProps.media)
selection: mediaChanged
? itemSelection(nextProps.media)
: itemSelection(nextProps.media, selection)
});
}
}
Expand Down

0 comments on commit 179d985

Please sign in to comment.