Skip to content

Commit

Permalink
Reader: Connect email subs component (Automattic#13288)
Browse files Browse the repository at this point in the history
* Reader: Connect email subs component

* Two fixes: hide time selector when disabled, also pick up on deliveryFrequency changes in state

* address comment by @fraying re. loading state

* improve style a bit
  • Loading branch information
samouri authored Apr 21, 2017
1 parent 130f0c9 commit 563ea5c
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 32 deletions.
119 changes: 91 additions & 28 deletions client/blocks/reader-subscription-list-item/email-settings.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* External dependencies
*/
import React, { PropTypes, Component } from 'react';
import { noop } from 'lodash';
import { connect } from 'react-redux';
import { find, get } from 'lodash';

/**
* Internal dependencies
Expand All @@ -13,17 +14,33 @@ import Popover from 'components/popover/index';
import SegmentedControl from 'components/segmented-control';
import ControlItem from 'components/segmented-control/item';
import FormToggle from 'components/forms/form-toggle';
import { getReaderFollows } from 'state/selectors';
import {
subscribeToNewPostEmail,
updateNewPostEmailSubscription,
unsubscribeToNewPostEmail,
subscribeToNewCommentEmail,
unsubscribeToNewCommentEmail,
} from 'state/reader/follows/actions';

class ReaderEmailSubscriptionSettingsPopout extends Component {
static displayName = 'ReaderEmailSubscriptionSettingsPopout';
static propTypes = {
feedId: PropTypes.number,
siteId: PropTypes.number,
deliveryFrequency: PropTypes.string,
};

state = {
showPopover: false,
selected: this.props.deliveryFrequency,
};

componentWillReceiveProps( nextProps ) {
if ( nextProps.deliveryFrequency !== this.props.deliveryFrequency ) {
this.setState( { selected: nextProps.deliveryFrequency } );
}
}

togglePopoverVisibility = () => {
this.setState( { showPopover: ! this.state.showPopover } );
}
Expand All @@ -37,11 +54,33 @@ class ReaderEmailSubscriptionSettingsPopout extends Component {
}

setSelected = text => () => {
const { siteId } = this.props;
this.setState( { selected: text } );
this.props.updateNewPostEmailSubscription( siteId, text );
}

toggleNewPostEmail = () => {
const toggleSubscription = this.props.notifyOnNewPosts
? this.props.unsubscribeToNewPostEmail
: this.props.subscribeToNewPostEmail;

toggleSubscription( this.props.siteId );
}

toggleNewCommentEmail = () => {
const toggleSubscription = this.props.notifyOnNewComments
? this.props.unsubscribeToNewCommentEmail
: this.props.subscribeToNewCommentEmail;

toggleSubscription( this.props.siteId );
}

render() {
const { translate } = this.props;
const { translate, notifyOnNewComments, notifyOnNewPosts } = this.props;

if ( ! this.props.siteId ) {
return null;
}

return (
<div>
Expand All @@ -68,35 +107,37 @@ class ReaderEmailSubscriptionSettingsPopout extends Component {
<div className="reader-subscription-list-item__email-popout-toggle">
{ translate( 'New posts' ) }
<FormToggle
onChange={ noop /* fire off dispatch */ }
checked={ true /* get from selector*/ }
onChange={ this.toggleNewPostEmail }
checked={ notifyOnNewPosts }
/>
</div>
<SegmentedControl>
<ControlItem
selected={ this.state.selected === 'instant' }
onClick={ this.setSelected( 'instant' ) }
>
{ translate( 'Instant' ) }
</ControlItem>
<ControlItem
selected={ this.state.selected === 'daily' }
onClick={ this.setSelected( 'daily' ) }
>
{ translate( 'Daily' ) }
</ControlItem>
<ControlItem
selected={ this.state.selected === 'weekly' }
onClick={ this.setSelected( 'weekly' ) }
>
{ translate( 'Weekly' ) }
</ControlItem>
</SegmentedControl>
{ notifyOnNewPosts && (
<SegmentedControl>
<ControlItem
selected={ this.state.selected === 'instantly' }
onClick={ this.setSelected( 'instantly' ) }
>
{ translate( 'Instantly' ) }
</ControlItem>
<ControlItem
selected={ this.state.selected === 'daily' }
onClick={ this.setSelected( 'daily' ) }
>
{ translate( 'Daily' ) }
</ControlItem>
<ControlItem
selected={ this.state.selected === 'weekly' }
onClick={ this.setSelected( 'weekly' ) }
>
{ translate( 'Weekly' ) }
</ControlItem>
</SegmentedControl>
) }
<div className="reader-subscription-list-item__email-popout-toggle">
New comments
<FormToggle
onChange={ noop /* fire off dispatch */ }
checked={ false /* get from selector*/ }
onChange={ this.toggleNewCommentEmail }
checked={ notifyOnNewComments }
/>
</div>
</div>
Expand All @@ -106,5 +147,27 @@ class ReaderEmailSubscriptionSettingsPopout extends Component {
}
}

export default localize( ReaderEmailSubscriptionSettingsPopout );
const mapStateToProps = ( state, ownProps ) => {
const follow = find( getReaderFollows( state ), { blog_ID: ownProps.siteId } );

const deliveryMethods = get( follow, [ 'delivery_methods', 'email' ], {} );
const { send_posts, post_delivery_frequency, send_comments } = deliveryMethods;

return {
notifyOnNewComments: !! send_comments,
notifyOnNewPosts: !! send_posts,
deliveryFrequency: post_delivery_frequency,
};
};

export default connect(
mapStateToProps,
{
subscribeToNewPostEmail,
unsubscribeToNewPostEmail,
updateNewPostEmailSubscription,
subscribeToNewCommentEmail,
unsubscribeToNewCommentEmail,
}
)( localize( ReaderEmailSubscriptionSettingsPopout ) );

3 changes: 2 additions & 1 deletion client/blocks/reader-subscription-list-item/index.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function ReaderSubscriptionListItem( {
className = '',
translate,
followSource,
isEmailBlocked,
} ) {
const siteTitle = getSiteName( { feed, site } );
const siteAuthor = site && site.owner;
Expand Down Expand Up @@ -69,7 +70,7 @@ function ReaderSubscriptionListItem( {
</div>
<div className="reader-subscription-list-item__options">
<FollowButton siteUrl={ siteUrl } followSource={ followSource } />
{ isFollowing && <EmailSettings /> }
{ isFollowing && ! isEmailBlocked && <EmailSettings siteId={ siteId } /> }
</div>
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { localize } from 'i18n-calypso';
*/
import connectSite from 'lib/reader-connect-site';
import SubscriptionListItem from 'blocks/reader-subscription-list-item/';
import userSettings from 'lib/user-settings';

export default localize( connectSite(
( { feed, site, translate, url, feedId, siteId } ) => (
Expand All @@ -19,6 +20,7 @@ export default localize( connectSite(
site={ site }
feed={ feed }
url={ url }
isEmailBlocked={ userSettings.getSetting( 'subscription_delivery_email_blocked' ) }
/>
)
) );
4 changes: 2 additions & 2 deletions client/reader/following-manage/index.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External Dependencies
*/
import React, { Component } from 'react';
import React, { Component, PropTypes } from 'react';
import { connect } from 'react-redux';
import { trim, debounce } from 'lodash';
import { localize } from 'i18n-calypso';
Expand All @@ -23,7 +23,7 @@ import SitesWindowScroller from './sites-window-scroller';

class FollowingManage extends Component {
static propTypes = {
query: React.PropTypes.string,
query: PropTypes.string,
};
state = { width: 800 };

Expand Down
2 changes: 1 addition & 1 deletion client/reader/following-manage/subscriptions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import FollowingManageSearchFollowed from './search-followed';
class FollowingManageSubscriptions extends Component {
static propTypes = {
follows: PropTypes.array.isRequired,
}
};

render() {
const { follows, width, translate } = this.props;
Expand Down

0 comments on commit 563ea5c

Please sign in to comment.