Skip to content

Commit

Permalink
Simplify CardPicking logic to only exist within PostLifecycle
Browse files Browse the repository at this point in the history
before this PR, the logic of which card to choose was split between post lifcycle, stream, and search stream.
they also often had their own adapters.  This is one step towards simplifying it by moving all of the selection to postlifecycle.
there is still more work to do here
  • Loading branch information
samouri committed Mar 7, 2017
1 parent 7c950d6 commit 944e235
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 222 deletions.
66 changes: 25 additions & 41 deletions client/lib/feed-stream-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,14 @@ import wpcom from 'lib/wp';

const wpcomUndoc = wpcom.undocumented();

function feedKeyMaker( post ) {
return {
feedId: post.feed_ID,
postId: post.ID
};
}

function siteKeyMaker( post ) {
return {
blogId: post.site_ID,
postId: post.ID
};
}
const keyMaker = post => ( {
blogId: post.site_ID,
feedId: post.feed_ID,
postId: post.ID || post.feed_item_ID,
} );

function mixedKeyMaker( post ) {
if ( post.feed_ID && post.feed_item_ID ) {
return {
feedId: post.feed_ID,
postId: post.feed_item_ID
};
}

return siteKeyMaker( post );
}
const recommendedKeyMaker = post =>
Object.assign( {}, keyMaker( post ), { isRecommendation: true } );

function addMetaToNextPageFetch( params ) {
params.meta = 'post,discover_original_post';
Expand Down Expand Up @@ -77,8 +61,8 @@ function getStoreForFeed( storeId ) {
};
return new FeedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: feedKeyMaker,
fetcher,
keyMaker,
onNextPageFetch: addMetaToNextPageFetch
} );
}
Expand All @@ -93,15 +77,15 @@ function getStoreForTag( storeId ) {
if ( config.isEnabled( 'reader/tags-with-elasticsearch' ) ) {
return new PagedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: siteKeyMaker,
fetcher,
keyMaker,
perPage: 5
} );
}
return new FeedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: mixedKeyMaker,
fetcher,
keyMaker,
onGapFetch: limitSiteParams,
onUpdateFetch: limitSiteParams,
dateProperty: 'tagged_on'
Expand All @@ -112,8 +96,8 @@ function getStoreForSearch( storeId ) {
const slug = storeId.substring( storeId.indexOf( ':' ) + 1 );
const stream = new PagedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: siteKeyMaker,
fetcher,
keyMaker,
perPage: 5
} );

Expand All @@ -137,8 +121,8 @@ function getStoreForList( storeId ) {

return new FeedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: mixedKeyMaker,
fetcher,
keyMaker,
onGapFetch: limitSiteParams,
onUpdateFetch: limitSiteParams
} );
Expand All @@ -153,8 +137,8 @@ function getStoreForSite( storeId ) {

return new FeedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: siteKeyMaker,
fetcher,
keyMaker,
onGapFetch: limitSiteParams,
onUpdateFetch: limitSiteParams
} );
Expand All @@ -168,8 +152,8 @@ function getStoreForFeatured( storeId ) {

return new FeedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: siteKeyMaker,
fetcher,
keyMaker,
onGapFetch: limitSiteParams,
onUpdateFetch: limitSiteParams
} );
Expand All @@ -179,7 +163,7 @@ function getStoreForRecommendedPosts( storeId ) {
const stream = new PagedStream( {
id: storeId,
fetcher: fetcher,
keyMaker: siteKeyMaker,
keyMaker: recommendedKeyMaker,
perPage: 6,
} );

Expand Down Expand Up @@ -245,21 +229,21 @@ export default function feedStoreFactory( storeId ) {
store = new FeedStream( {
id: storeId,
fetcher: wpcomUndoc.readFollowing.bind( wpcomUndoc ),
keyMaker: feedKeyMaker,
keyMaker,
onNextPageFetch: addMetaToNextPageFetch
} );
} else if ( storeId === 'a8c' ) {
store = new FeedStream( {
id: storeId,
fetcher: wpcomUndoc.readA8C.bind( wpcomUndoc ),
keyMaker: feedKeyMaker,
keyMaker,
onNextPageFetch: addMetaToNextPageFetch
} );
} else if ( storeId === 'likes' ) {
store = new FeedStream( {
id: storeId,
fetcher: wpcomUndoc.readLiked.bind( wpcomUndoc ),
keyMaker: siteKeyMaker,
keyMaker,
onGapFetch: limitSiteParamsForLikes,
onUpdateFetch: limitSiteParamsForLikes,
dateProperty: 'date_liked'
Expand Down
92 changes: 5 additions & 87 deletions client/reader/search-stream/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,88 +14,10 @@ import Stream from 'reader/stream';
import EmptyContent from './empty';
import HeaderBack from 'reader/header-back';
import SearchInput from 'components/search';
import SiteStore from 'lib/reader-site-store';
import FeedStore from 'lib/feed-store';
import { recordTrackForPost, recordAction } from 'reader/stats';
import SuggestionProvider from './suggestion-provider';
import Suggestion from './suggestion';
import ReaderPostCard from 'blocks/reader-post-card';
import { RelatedPostCard } from 'blocks/reader-related-card-v2';
import {
EMPTY_SEARCH_RECOMMENDATIONS,
SEARCH_RESULTS,
} from 'reader/follow-button/follow-sources';

function RecommendedPosts( { post, site } ) {
function handlePostClick() {
recordTrackForPost( 'calypso_reader_recommended_post_clicked', post, {
recommendation_source: 'empty-search',
} );
recordAction( 'search_page_rec_post_click' );
}

function handleSiteClick() {
recordTrackForPost( 'calypso_reader_recommended_site_clicked', post, {
recommendation_source: 'empty-search',
} );
recordAction( 'search_page_rec_site_click' );
}

if ( ! site ) {
site = { title: post.site_name, };
}

return (
<div className="search-stream__recommendation-list-item" key={ post.global_ID }>
<RelatedPostCard post={ post } site={ site }
onSiteClick={ handleSiteClick } onPostClick={ handlePostClick } followSource={ EMPTY_SEARCH_RECOMMENDATIONS } />
</div>
);
}

const SearchCardAdapter = ( isRecommendations ) => class extends Component {
state = this.getStateFromStores();

getStateFromStores( props = this.props ) {
return {
site: SiteStore.get( props.post.site_ID ),
feed: props.post.feed_ID ? FeedStore.get( props.post.feed_ID ) : null
};
}

componentWillReceiveProps( nextProps ) {
this.setState( this.getStateFromStores( nextProps ) );
}

onCardClick = ( post ) => {
recordTrackForPost( 'calypso_reader_searchcard_clicked', post );
this.props.handleClick();
}

onCommentClick = () => {
this.props.handleClick( { comments: true } );
}

render() {
let CardComponent;

if ( isRecommendations ) {
CardComponent = RecommendedPosts;
} else {
CardComponent = ReaderPostCard;
}

return <CardComponent
post={ this.props.post }
site={ this.props.site }
feed={ this.props.feed }
onClick={ this.onCardClick }
followSource={ SEARCH_RESULTS }
onCommentClick={ this.onCommentClick }
showPrimaryFollowButton={ this.props.showPrimaryFollowButtonOnCards }
/>;
}
};
import { SEARCH_RESULTS, } from 'reader/follow-button/follow-sources';

class SearchStream extends Component {

Expand Down Expand Up @@ -143,11 +65,6 @@ class SearchStream extends Component {
window.scrollTo( 0, 0 );
}

cardFactory = () => {
const isRecommendations = ! this.props.query;
return SearchCardAdapter( isRecommendations );
}

handleStreamMounted = ( ref ) => {
this.streamRef = ref;
}
Expand Down Expand Up @@ -186,7 +103,7 @@ class SearchStream extends Component {
}

render() {
const { store, query, suggestions } = this.props;
const { query, suggestions } = this.props;
const emptyContent = <EmptyContent query={ query } />;

let searchPlaceholderText = this.props.searchPlaceholderText;
Expand All @@ -201,11 +118,12 @@ class SearchStream extends Component {
'%s ‹ Reader', { args: this.state.title || this.props.translate( 'Search' ) }
);
return (
<Stream { ...this.props } store={ store }
<Stream
{ ...this.props }
followSource={ SEARCH_RESULTS }
listName={ this.props.translate( 'Search' ) }
emptyContent={ emptyContent }
showFollowInHeader={ true }
cardFactory={ this.cardFactory }
placeholderFactory={ this.placeholderFactory }
className="search-stream" >
{ this.props.showBack && <HeaderBack /> }
Expand Down
Loading

0 comments on commit 944e235

Please sign in to comment.