Skip to content

Commit

Permalink
Insert new recommendations at the correct position
Browse files Browse the repository at this point in the history
  • Loading branch information
bluefuton committed Jun 6, 2016
1 parent bfe1083 commit abee2bb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
15 changes: 11 additions & 4 deletions client/state/reader/start/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ export function items( state = [], action ) {
const totalRecommendationsCount = state.length + newRecommendations.length;

forEach( newRecommendations, ( recommendation ) => {
// We want to insert the new recommendation immediately after its parent, if there is one
// Find the parent recommendation, if there is one.
let parentPosition = findIndex( state, ( item ) => {
if ( ! has( item, 'recommended_site_ID' ) ) {
return false;
}
return item.recommended_site_ID === recommendation.origin_site_ID && item.recommended_post_ID === recommendation.origin_post_ID;
} );

if ( parentPosition < 0 ) {
parentPosition = totalRecommendationsCount - 1;
let childPosition;
if ( parentPosition >= 0 ) {
// Insert the new recommendation immediately after the parent, if we found one.
childPosition = parentPosition + 1;
} else {
// No parent recommendation? Insert it at the end.
childPosition = totalRecommendationsCount - 1;
}

updatedRecommendations = insertItemAtArrayPosition( updatedRecommendations, recommendation, parentPosition );
debug( 'Inserting recommendation ID ' + recommendation.ID + ' at position ' + childPosition );

updatedRecommendations = insertItemAtArrayPosition( updatedRecommendations, recommendation, childPosition );
} );
return updatedRecommendations;

Expand Down
2 changes: 1 addition & 1 deletion client/state/reader/start/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe( 'reducer', () => {
type: READER_START_RECOMMENDATIONS_RECEIVE,
recommendations: [ newRecommendation ]
} );
expect( state[1] ).to.eql( newRecommendation );
expect( state[2] ).to.eql( newRecommendation );
} );
} );
} );

0 comments on commit abee2bb

Please sign in to comment.