Skip to content

Commit

Permalink
Insert new recommendations at the end of the list
Browse files Browse the repository at this point in the history
  • Loading branch information
bluefuton committed Jun 13, 2016
1 parent 2c7d464 commit f539fd8
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 64 deletions.
1 change: 0 additions & 1 deletion client/reader/start/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@
}

@keyframes slideout-bar {

0% {
transform: translateY( calc( 100vh - 100px ) );
}
Expand Down
66 changes: 11 additions & 55 deletions client/state/reader/start/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
*/
import { combineReducers } from 'redux';
import union from 'lodash/union';
import findIndex from 'lodash/findIndex';
import forEach from 'lodash/forEach';
import find from 'lodash/find';
import filter from 'lodash/filter';
import has from 'lodash/has';
import debugModule from 'debug';

/**
* Internal dependencies
Expand All @@ -21,13 +18,6 @@ import {
SERIALIZE,
DESERIALIZE,
} from 'state/action-types';
import { itemsSchema } from './schema';
import { isValidStateWithSchema } from 'state/utils';

/**
* Module variables
*/
const debug = debugModule( 'calypso:redux:reader-start-recommendations' );

/**
* Tracks all known list objects, indexed by list ID.
Expand All @@ -39,52 +29,24 @@ const debug = debugModule( 'calypso:redux:reader-start-recommendations' );
export function items( state = [], action ) {
switch ( action.type ) {
case READER_START_RECOMMENDATIONS_RECEIVE:
let updatedRecommendations = state;

// Filter out any recommendations we already have
const newRecommendations = filter( action.recommendations, ( incomingRecommendation ) => {
if ( findIndex( state, ( existingRecommendation ) => {
return existingRecommendation.ID === incomingRecommendation.ID;
} ) >= 0 ) {
return false;
}
return true;
return ! find( state, { ID: incomingRecommendation.ID } );
} );

const totalRecommendationsCount = state.length + newRecommendations.length;

forEach( newRecommendations, ( recommendation ) => {
// 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;
} );

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;
}

debug( 'Inserting recommendation ID ' + recommendation.ID + ' at position ' + childPosition );
// No new recommendations? Just return the existing ones.
if ( newRecommendations.length < 1 ) {
return state;
}

updatedRecommendations = insertItemAtArrayPosition( updatedRecommendations, recommendation, childPosition );
} );
return updatedRecommendations;
return state.concat( newRecommendations );

// Always return default state - we don't want to serialize recommendations
case SERIALIZE:
return state;
case DESERIALIZE:
if ( ! isValidStateWithSchema( state, itemsSchema ) ) {
return [];
}
return state;
return [];
}

return state;
}

Expand Down Expand Up @@ -117,18 +79,12 @@ export function recommendationsInteractedWith( state = [], action ) {

case SERIALIZE:
case DESERIALIZE:
return state;
return [];
}

return state;
}

function insertItemAtArrayPosition( array, item, position ) {
const beforeSlice = array.slice( 0, position );
const afterSlice = array.slice( position );
return beforeSlice.concat( item, afterSlice );
}

export default combineReducers( {
items,
isRequestingRecommendations,
Expand Down
3 changes: 0 additions & 3 deletions client/state/reader/start/schema.js

This file was deleted.

4 changes: 1 addition & 3 deletions client/state/reader/start/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ export function isRequestingRecommendations( state ) {
* @return {Object} Recommendation
*/
export function getRecommendationById( state, recommendationId ) {
return find( state.reader.start.items, ( item ) => {
return item.ID === recommendationId;
} );
return find( state.reader.start.items, { ID: recommendationId } );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion client/state/reader/start/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ describe( 'actions', () => {
describe( '#requestRecommendations', () => {
useNock( nock => {
nock( 'https://public-api.wordpress.com:443' )
.get( '/rest/v1.1/read/recommendations/start?meta=site%2Cpost' )
.get( '/rest/v1.1/read/recommendations/start?meta=site%2Cpost&number=20' )
.reply( 200, deepFreeze( sampleSuccessResponse ) );
} );

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 @@ -31,7 +31,7 @@ describe( 'reducer', () => {
expect( state ).to.eql( expected );
} );

it( 'should insert a new recommendation immediately after its parent', () => {
it( 'should insert a new recommendation', () => {
const original = [
{
ID: 123,
Expand Down

0 comments on commit f539fd8

Please sign in to comment.