more work on removeSuccessCallback (wired with withRemove) ; add options noRemoveMutation (ex for UsersEdit)

This commit is contained in:
xavcz 2016-11-13 14:12:15 +01:00
parent 78802d6790
commit dd32c36f28
8 changed files with 28 additions and 49 deletions

View file

@ -8,25 +8,6 @@ import { connect } from 'react-redux';
class CategoriesEditForm extends Component{
constructor() {
super();
this.deleteCategory = this.deleteCategory.bind(this);
}
deleteCategory() {
const category = this.props.category;
if (window.confirm(`Delete category “${category.name}”?`)) {
this.context.actions.call("categories.deleteById", category._id, (error, result) => {
if (error) {
this.props.flash(error.message, "error");
} else {
this.props.flash(`Category “${category.name}” deleted and removed from ${result} posts.`, "success");
}
this.context.closeCallback();
});
}
}
render() {
return (
@ -41,9 +22,13 @@ class CategoriesEditForm extends Component{
this.context.closeCallback();
this.props.flash("Category edited.", "success");
}}
removeSuccessCallback={({documentId, documentTitle}) => {
this.context.closeCallback();
const deleteDocumentSuccess = this.context.intl.formatMessage({id: 'categories.delete_success'}, {title: documentTitle});
this.props.flash(deleteDocumentSuccess, "success");
this.context.events.track("category deleted", {_id: documentId});
}}
/>
<hr/>
<a onClick={this.deleteCategory} className="categories-delete-link"><Telescope.components.Icon name="close"/> <FormattedMessage id="categories.delete"/></a>
</div>
)
}
@ -54,7 +39,6 @@ CategoriesEditForm.propTypes = {
}
CategoriesEditForm.contextTypes = {
actions: React.PropTypes.object,
closeCallback: React.PropTypes.func,
currentUser: React.PropTypes.object,
};

View file

@ -15,6 +15,7 @@ const CommentsEditForm = (props, context) => {
resultQuery={Comments.graphQLQueries.single}
successCallback={props.successCallback}
cancelCallback={props.cancelCallback}
removeSuccessCallback={props.removeSuccessCallback}
/>
</div>
)

View file

@ -11,7 +11,7 @@ class CommentsItem extends Component{
constructor() {
super();
['showReply', 'replyCancelCallback', 'replySuccessCallback', 'showEdit', 'editCancelCallback', 'editSuccessCallback', 'deleteComment'].forEach(methodName => {this[methodName] = this[methodName].bind(this)});
['showReply', 'replyCancelCallback', 'replySuccessCallback', 'showEdit', 'editCancelCallback', 'editSuccessCallback', 'removeSuccessCallback'].forEach(methodName => {this[methodName] = this[methodName].bind(this)});
this.state = {
showReply: false,
showEdit: false
@ -46,19 +46,10 @@ class CommentsItem extends Component{
this.setState({showEdit: false});
}
deleteComment() {
const comment = this.props.comment;
const deleteConfirmMessage = this.context.intl.formatMessage({id: "comments.delete_confirm"}, {body: Telescope.utils.trimWords(comment.body, 20)});
const deleteSuccessMessage = this.context.intl.formatMessage({id: "comments.delete_success"}, {body: Telescope.utils.trimWords(comment.body, 20)});
if (window.confirm(deleteConfirmMessage)) {
this.context.actions.call('comments.deleteById', comment._id, (error, result) => {
this.props.flash(deleteSuccessMessage, "success");
this.context.events.track("comment deleted", {'_id': comment._id});
});
}
removeSuccessCallback({documentId}) {
const deleteDocumentSuccess = this.context.intl.formatMessage({id: 'comments.delete_success'});
this.props.flash(deleteDocumentSuccess, "success");
this.context.events.track("comment deleted", {_id: documentId});
}
renderComment() {
@ -80,7 +71,7 @@ class CommentsItem extends Component{
postId={this.props.comment.postId}
parentComment={this.props.comment}
successCallback={this.replySuccessCallback}
cancelCallback={this.replyCancelCallback}
cancelCallback={this.replyCancelCallback}
type="reply"
/>
</div>
@ -94,6 +85,7 @@ class CommentsItem extends Component{
comment={this.props.comment}
successCallback={this.editSuccessCallback}
cancelCallback={this.editCancelCallback}
removeSuccessCallback={this.removeSuccessCallback}
/>
)
}
@ -111,7 +103,6 @@ class CommentsItem extends Component{
<Telescope.components.CanDo action="comments.edit" document={this.props.comment}>
<div>
<a className="comment-edit" onClick={this.showEdit}><FormattedMessage id="comments.edit"/></a>
<a className="comment-delete" onClick={this.deleteComment}><FormattedMessage id="comments.delete"/></a>
</div>
</Telescope.components.CanDo>
</div>
@ -130,7 +121,6 @@ CommentsItem.propTypes = {
CommentsItem.contextTypes = {
currentUser: React.PropTypes.object,
actions: React.PropTypes.object,
events: React.PropTypes.object,
intl: intlShape
};

View file

@ -44,7 +44,7 @@ class PostsEditForm extends Component {
const deleteDocumentSuccess = this.context.intl.formatMessage({id: 'posts.delete_success'}, {title: documentTitle});
this.props.flash(deleteDocumentSuccess, "success");
this.context.events.track("post deleted", {'_id': documentId});
this.context.events.track("post deleted", {_id: documentId});
}}
/>
</div>

View file

@ -34,6 +34,7 @@ const UsersEditForm = (props, context) => {
successCallback={(user)=>{
props.flash(context.intl.formatMessage({id: "users.edit_success"}, {name: Users.getDisplayName(user)}), 'success')
}}
noRemoveMutation={true}
/>
</div>
</Telescope.components.CanDo>

View file

@ -419,6 +419,7 @@ class FormWrapper extends Component{
render() {
const fieldGroups = this.getFieldGroups();
const collectionName = this.props.collection._name;
return (
<div className={"document-"+this.getFormType()}>
@ -438,8 +439,8 @@ class FormWrapper extends Component{
this.props.removeMutation
? <div>
<hr/>
<a onClick={this.deleteDocument} className="delete-post-link">
<Telescope.components.Icon name="close"/> <FormattedMessage id="posts.delete"/>
<a onClick={this.deleteDocument} className={`${collectionName}-delete-link`}>
<Telescope.components.Icon name="close"/> <FormattedMessage id={`${collectionName}.delete`}/>
</a>
</div>
: null

View file

@ -24,7 +24,7 @@ export default function withEdit(WrappedComponent, options) {
const collectionName = collection._name;
const ComponentWithMutation = graphql(gql`
const ComponentWithEdit = graphql(gql`
mutation ${mutationName}($documentId: String, $set: ${collectionName}Input, $unset: ${collectionName}Unset) {
${mutationName}(documentId: $documentId, set: $set, unset: $unset) {
${fragment ? `...${fragment[0].name.value}` : resultQuery}
@ -41,10 +41,10 @@ export default function withEdit(WrappedComponent, options) {
}),
})(WrappedComponent);
// add the remove mutation to the component with the edit mutation
const ComponentWithRemoveAndEdit = withRemove(ComponentWithMutation);
return <ComponentWithRemoveAndEdit {...this.props} />
// add the remove mutation by default unless it's explicitly specified not to do it
const ComponentToRender = this.props.noRemoveMutation ? ComponentWithEdit : withRemove(ComponentWithEdit);
return <ComponentToRender {...this.props} />
}
}

View file

@ -47,8 +47,8 @@ Telescope.strings.en = {
"comments.reply": "Reply",
"comments.edit": "Edit",
"comments.delete": "Delete",
"comments.delete_confirm": "Delete comment “{body}”?",
"comments.delete_success": "Comment “{body}” deleted.",
"comments.delete_confirm": "Delete this comment?",
"comments.delete_success": "Comment deleted.",
"comments.please_log_in": "Please log in to comment.",
"comments.parentCommentId": "Parent Comment ID",
"comments.topLevelCommentId": "Top Level Comment ID",
@ -99,6 +99,8 @@ Telescope.strings.en = {
"categories.subscribed": "You have subscribed to “{name}” posts.",
"categories.unsubscribed": "You have unsubscribed from “{name}” posts.",
"categories.subscribed_categories" : "Categories subscribed to",
"categories.delete_confirm": "Delete category “{title}”?",
"categories.delete_success": "Category deleted.",
"settings": "Settings",
"settings.json_message": "Note: settings already provided in your <code>settings.json</code> file will be disabled.",