This repository has been archived by the owner on Oct 1, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
follow-tag-button.js
130 lines (110 loc) · 4.06 KB
/
follow-tag-button.js
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
This file is a part of libertysoil.org website
Copyright (C) 2016 Loki Education (Social Enterprise)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import React, { PropTypes } from 'react';
import ga from 'react-google-analytics';
import { omit } from 'lodash';
import { CurrentUser as CurrentUserPropType } from '../prop-types/users';
// Statuses for the progress indication.
const STATUS_NOT_TOUCHED = 'STATUS_NOT_TOUCHED';
const STATUS_FOLLOWING = 'STATUS_FOLLOWING';
const STATUS_JUST_FOLLOWED = 'STATUS_JUST_FOLLOWED';
const STATUS_UNFOLLOWING = 'STATUS_UNFOLLOWING';
const STATUS_JUST_UNFOLLOWED = 'STATUS_JUST_UNFOLLOWED';
/**
* Universal component to use in the TagPage, SchoolPage components.
*/
export default class FollowTagButton extends React.Component {
static displayName = 'FollowButton';
static propTypes = {
current_user: CurrentUserPropType,
followed_tags: PropTypes.shape({}),
tag: PropTypes.string,
triggers: PropTypes.shape({
followTag: PropTypes.func.isRequired,
unfollowTag: PropTypes.func.isRequired
})
};
constructor(props) {
super(props);
this.state = {
status: STATUS_NOT_TOUCHED
};
}
componentWillReceiveProps(nextProps) {
if (this.props.tag !== nextProps.tag) {
this.setState({
status: STATUS_NOT_TOUCHED
});
}
}
_followTag = (event) => {
event.preventDefault();
this.setState({
status: STATUS_FOLLOWING
});
this.props.triggers.followTag(this.props.tag).then(() => {
this.setState({
status: STATUS_JUST_FOLLOWED
});
ga('send', 'event', 'Tags', 'Follow', this.props.tag);
});
};
_unfollowTag = (event) => {
event.preventDefault();
this.setState({
status: STATUS_UNFOLLOWING
});
this.props.triggers.unfollowTag(this.props.tag).then(() => {
this.setState({
status: STATUS_JUST_UNFOLLOWED
});
});
};
render() {
const {
className = '',
current_user,
followed_tags,
...props
} = this.props;
const status = this.state.status;
if (!current_user) {
return null;
}
const buttonProps = omit(props, Object.keys(FollowTagButton.propTypes));
// If followTag or unfollowTag was performed
if (status !== STATUS_NOT_TOUCHED) {
switch (status) {
case STATUS_FOLLOWING:
return <button {...buttonProps} className={`button button-green ${className}`} type="button">Following...</button>;
case STATUS_UNFOLLOWING:
return <button {...buttonProps} className={`button button-green ${className}`} type="button">Unfollowing...</button>;
case STATUS_JUST_FOLLOWED:
return <button {...buttonProps} className={`button button-yellow ${className}`} type="button" onClick={this._unfollowTag}>Followed!</button>;
case STATUS_JUST_UNFOLLOWED:
return <button {...buttonProps} className={`button button-green ${className}`} type="button" onClick={this._followTag}>Unfollowed!</button>;
default:
return null;
}
} else {
const isFollowed = !!followed_tags[this.props.tag];
if (isFollowed) {
return <button {...buttonProps} className={`button button-yellow ${className}`} type="button" onClick={this._unfollowTag}>Following</button>;
} else { // eslint-disable-line no-else-return
return <button {...buttonProps} className={`button button-red ${className}`} type="button" onClick={this._followTag}>Follow</button>;
}
}
}
}