-
Notifications
You must be signed in to change notification settings - Fork 2k
/
follow-link.jsx
96 lines (80 loc) · 2.09 KB
/
follow-link.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* External dependencies
*/
import PropTypes from 'prop-types';
import React from 'react';
import createReactClass from 'create-react-class';
import { localize } from 'i18n-calypso';
/**
* Internal dependencies
*/
import { wpcom } from '../rest-client/wpcom';
import { bumpStat } from '../rest-client/bump-stat';
import Gridicon from './gridicons';
export const FollowLink = createReactClass( {
displayName: 'FollowLink',
propTypes: {
site: PropTypes.number,
isFollowing: PropTypes.bool,
},
followStatTypes: {
comment: 'note_commented_post',
comment_like: 'note_liked_comment',
like: 'note_liked_post',
follow: 'note_followed',
reblog: 'note_reblog_post',
},
getInitialState: function () {
return {
isFollowing: this.props.isFollowing,
};
},
toggleFollowStatus: function ( event ) {
var isFollowing = this.state.isFollowing;
var follower = wpcom().site( this.props.site ).follow();
var component = this;
var updateState = function ( error, data ) {
if ( error ) throw error;
if ( component.isMounted() ) {
component.setState( {
isFollowing: data.is_following,
} );
}
};
if ( isFollowing ) {
follower.del( updateState );
bumpStat( 'notes-click-action', 'unfollow' );
} else {
follower.add( updateState );
var stats = { 'notes-click-action': 'follow' };
stats[ 'follow_source' ] = this.followStatTypes[ this.props.noteType ];
bumpStat( stats );
}
// but optimistically update the client
this.setState( {
isFollowing: ! isFollowing,
} );
},
render: function () {
var gridicon_icon;
var link_text;
if ( this.state.isFollowing ) {
gridicon_icon = 'reader-following';
link_text = this.props.translate( 'Following', {
context: 'you are following',
} );
} else {
gridicon_icon = 'reader-follow';
link_text = this.props.translate( 'Follow', {
context: 'verb: imperative',
} );
}
return (
<button className="follow-link" onClick={ this.toggleFollowStatus }>
<Gridicon icon={ gridicon_icon } size={ 18 } />
{ link_text }
</button>
);
},
} );
export default localize( FollowLink );