Live update unfeatured

This commit is contained in:
Chi Vinh Le
2017-08-02 22:29:01 +07:00
parent da0d0349dd
commit 2e974ac82f
3 changed files with 115 additions and 35 deletions
@@ -12,33 +12,60 @@ function prepareNotificationText(text) {
}
class ModSubscription extends React.Component {
unsubscribe = null;
subscriptions = null;
componentWillMount() {
this.unsubscribe = this.props.data.subscribeToMore({
document: COMMENT_FEATURED_SUBSCRIPTION,
variables: {
assetId: this.props.data.variables.asset_id,
const configs = [
{
document: COMMENT_FEATURED_SUBSCRIPTION,
variables: {
assetId: this.props.data.variables.asset_id,
},
updateQuery: (prev, {subscriptionData: {data: {commentFeatured: {user, comment}}}}) => {
const sort = this.props.data.variables.sort;
const text = this.props.user.id === user.id
? {}
: t(
'talk-plugin-featured-comments.notify_featured',
user.username,
prepareNotificationText(comment.body),
);
const notify = {
activeQueue: this.props.activeTab,
text,
anyQueue: true,
};
return handleCommentChange(prev, comment, sort, notify);
},
},
updateQuery: (prev, {subscriptionData: {data: {commentFeatured: comment}}}) => {
const sort = this.props.data.variables.sort;
const user = comment.status_history[comment.status_history.length - 1].assigned_by;
const notify = {
activeQueue: this.props.activeTab,
text: t(
'talk-plugin-featured-comments.notify_featured',
user.username,
prepareNotificationText(comment.body)
),
anyQueue: true,
};
return handleCommentChange(prev, comment, sort, notify);
{
document: COMMENT_UNFEATURED_SUBSCRIPTION,
variables: {
assetId: this.props.data.variables.asset_id,
},
updateQuery: (prev, {subscriptionData: {data: {commentUnfeatured: {user, comment}}}}) => {
const sort = this.props.data.variables.sort;
const text = this.props.user.id === user.id
? {}
: t(
'talk-plugin-featured-comments.notify_unfeatured',
user.username,
prepareNotificationText(comment.body),
);
const notify = {
activeQueue: this.props.activeTab,
text,
anyQueue: true,
};
return handleCommentChange(prev, comment, sort, notify);
}
},
});
];
this.subscriptions = configs.map((config) => this.props.data.subscribeToMore(config));
}
componentWillUnmount() {
this.unsubscribe();
this.subscriptions.forEach((unsubscribe) => unsubscribe());
}
render() {
@@ -48,15 +75,28 @@ class ModSubscription extends React.Component {
const COMMENT_FEATURED_SUBSCRIPTION = gql`
subscription CommentFeatured($assetId: ID){
commentFeatured(asset_id: $assetId){
...${getDefinitionName(Comment.fragments.comment)}
status_history {
type
created_at
assigned_by {
id
username
}
commentFeatured(asset_id: $assetId) {
comment {
...${getDefinitionName(Comment.fragments.comment)}
}
user {
id
username
}
}
}
${Comment.fragments.comment}
`;
const COMMENT_UNFEATURED_SUBSCRIPTION = gql`
subscription CommentUnfeatured($assetId: ID){
commentUnfeatured(asset_id: $assetId){
comment {
...${getDefinitionName(Comment.fragments.comment)}
}
user {
id
username
}
}
}
@@ -64,7 +104,7 @@ const COMMENT_FEATURED_SUBSCRIPTION = gql`
`;
const mapStateToProps = (state) => ({
sortOrder: state.moderation.toJS().sortOrder,
user: state.auth.toJS().user,
});
export default connect(mapStateToProps, null)(ModSubscription);
@@ -7,6 +7,7 @@ en:
go_to_conversation: Go to conversation
tooltip_description: Comments selected by our team as worth reading
notify_featured: '{0} featured and approved comment "{1}"'
notify_unfeatured: '{0} unfeatured comment "{1}"'
es:
talk-plugin-featured-comments:
un_feature: Desmarcar
+44 -5
View File
@@ -2,16 +2,33 @@ const {check} = require('perms/utils');
module.exports = {
typeDefs: `
type CommentFeaturedData {
comment: Comment!
user: User!
}
type CommentUnfeaturedData {
comment: Comment!
user: User!
}
type Subscription {
# Subscribe to featured comments.
commentFeatured(asset_id: ID): Comment
commentFeatured(asset_id: ID): CommentFeaturedData
# Subscribe to featured comments.
commentUnfeatured(asset_id: ID): CommentUnfeaturedData
}
`,
resolvers: {
Subscription: {
commentFeatured: ({comment}) => {
return comment;
commentFeatured: ({user, comment}) => {
return {user, comment};
},
commentUnfeatured: ({user, comment}) => {
return {user, comment};
},
},
},
@@ -26,15 +43,37 @@ module.exports = {
},
},
}),
commentUnfeatured: (options, args) => ({
commentUnfeatured: {
filter: ({comment}, {user}) => {
if (args.asset_id === null) {
return check(user, ['ADMIN', 'MODERATOR']);
}
return comment.asset_id === args.asset_id;
},
},
}),
},
hooks: {
RootMutation: {
addTag: {
async post(obj, {tag: {name, id, item_type}}, {mutators: {Comment}, pubsub}, info, result) {
async post(obj, {tag: {name, id, item_type}}, {user, mutators: {Comment}, pubsub}, info, result) {
if (name === 'FEATURED' && item_type === 'COMMENTS') {
const comment = await Comment.setStatus({id: id, status: 'ACCEPTED'});
if (comment) {
pubsub.publish('commentFeatured', {comment});
pubsub.publish('commentFeatured', {comment, user});
}
return result;
}
return result;
},
},
removeTag: {
async post(obj, {tag: {name, id, item_type}}, {user, loaders: {Comments}, pubsub}, info, result) {
if (name === 'FEATURED' && item_type === 'COMMENTS') {
const comment = await Comments.get.load(id);
if (comment) {
pubsub.publish('commentUnfeatured', {comment, user});
}
return result;
}