-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFollowButton.js
More file actions
95 lines (87 loc) · 2.52 KB
/
FollowButton.js
File metadata and controls
95 lines (87 loc) · 2.52 KB
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
import React, { PropTypes } from 'react';
import Relay from 'react-relay';
import OddshotButton from './OddshotButton';
import FlatButton from './FlatButton';
import FollowSubjectMutation from './FollowSubjectMutation';
import FollowStreamerMutation from './FollowStreamerMutation';
import './FollowButton.scss';
const FollowButton = React.createClass({
propTypes: {
actor: PropTypes.object.isRequired,
color: PropTypes.string,
className: PropTypes.string,
flat: PropTypes.bool,
icon: PropTypes.string,
item: PropTypes.object.isRequired,
},
contextTypes: {
isLoggedIn: PropTypes.func.isRequired,
setLoginRequired: PropTypes.func.isRequired,
},
getDefaultProps() {
return {
flat: false,
};
},
onClick(event) {
if (!this.context.isLoggedIn()) {
this.context.setLoginRequired(true);
} else {
const actor = this.props.actor;
const item = this.props.item;
const Mutation = item.__typename === 'Subject' ? FollowSubjectMutation : FollowStreamerMutation;
const mutationProps = {
actor,
item,
follow: !item.isViewerFollowing,
};
Relay.Store.commitUpdate(new Mutation(mutationProps), {
onFailure: this.onSubmitFailure,
onSuccess: this.onSubmitSuccess,
});
}
event.stopPropagation();
event.preventDefault();
},
onSubmitSuccess() {
// TODO
},
onSubmitFailure() {
// TODO
},
render() {
const Button = this.props.flat ? FlatButton : OddshotButton;
const isFollowing = this.props.item.isViewerFollowing;
const icon = this.props.icon || (isFollowing ? 'minus' : 'plus');
const className = ['FollowButton', this.props.className];
return (
<Button color="lilac" icon={icon} {...this.props} className={className} onClick={this.onClick}>
{this.props.children || (isFollowing ? 'Unfollow' : 'Follow')}
</Button>
);
},
});
export default Relay.createContainer(FollowButton, {
fragments: {
actor: () => Relay.QL`
fragment on User {
${FollowStreamerMutation.getFragment('actor')},
${FollowSubjectMutation.getFragment('actor')},
}
`,
item: () => Relay.QL`
fragment on Followed {
... on Node { # workaround for relay issue #782
... on Streamer {
${FollowStreamerMutation.getFragment('item')},
},
... on Subject {
${FollowSubjectMutation.getFragment('item')},
},
},
__typename,
isViewerFollowing,
}
`,
},
});