Vulcan/packages/nova-subscribe/lib/components/Subscribe.jsx

77 lines
1.8 KiB
React
Raw Normal View History

2016-07-28 21:19:10 +02:00
import React, { PropTypes, Component } from 'react';
import { intlShape } from 'react-intl';
class Subscribe extends Component {
constructor(props, context) {
super(props, context);
this.onSubscribe = this.onSubscribe.bind(this);
this.isSubscribed = this.isSubscribed.bind(this);
}
2016-07-29 11:15:42 +02:00
onSubscribe(e) {
2016-08-07 18:06:42 +02:00
e.preventDefault();
2016-07-29 11:15:42 +02:00
2016-07-28 21:19:10 +02:00
const post = this.props.post;
const user = this.context.currentUser;
2016-08-07 18:06:42 +02:00
let callAction = 'posts.subscribe';
2016-07-28 21:19:10 +02:00
let isSubscribed = this.isSubscribed(post, user);
if( isSubscribed ) {
2016-08-07 18:06:42 +02:00
callAction = "posts.unsubscribe";
2016-07-28 21:19:10 +02:00
}
this.context.actions.call(callAction, post._id, (error, result) => {
2016-08-07 18:31:15 +02:00
if (error)
this.context.messages.flash(error.message, "error")
2016-07-28 21:19:10 +02:00
if (result)
this.context.events.track(callAction, {'_id': post._id});
})
}
isSubscribed(post, user) {
if (!post || !user)
return false;
return post.subscribers && post.subscribers.indexOf(user._id) != -1;
}
render() {
const post = this.props.post;
const user = this.context.currentUser;
// can't subscribe to own post (also validated on server side)
if(user && post.author === user.username) {
return null;
}
let btnTitle = "posts.subscribe";
let isSubscribed = this.isSubscribed(post, user);
if( isSubscribed ) {
btnTitle = "posts.unsubscribe";
}
return (
2016-07-29 11:15:42 +02:00
<a onClick={this.onSubscribe} >{this.context.intl.formatMessage({id: btnTitle})}</a>
2016-07-28 21:19:10 +02:00
)
}
}
Subscribe.propTypes = {
post: React.PropTypes.object.isRequired
}
Subscribe.contextTypes = {
currentUser: React.PropTypes.object,
2016-08-07 18:31:15 +02:00
messages: React.PropTypes.object,
2016-07-28 21:19:10 +02:00
actions: React.PropTypes.object,
events: React.PropTypes.object,
intl: intlShape
};
module.exports = Subscribe;
export default Subscribe;