From dd32c36f28986169dcd0617da07c2deb94432a76 Mon Sep 17 00:00:00 2001 From: xavcz Date: Sun, 13 Nov 2016 14:12:15 +0100 Subject: [PATCH] more work on removeSuccessCallback (wired with withRemove) ; add options noRemoveMutation (ex for UsersEdit) --- .../lib/categories/CategoriesEditForm.jsx | 28 ++++--------------- .../lib/comments/CommentsEditForm.jsx | 1 + .../lib/comments/CommentsItem.jsx | 24 +++++----------- .../lib/posts/PostsEditForm.jsx | 2 +- .../lib/users/UsersEditForm.jsx | 1 + packages/nova-forms/lib/FormWrapper.jsx | 5 ++-- packages/nova-forms/lib/withEdit.jsx | 10 +++---- packages/nova-i18n-en-us/lib/en_US.js | 6 ++-- 8 files changed, 28 insertions(+), 49 deletions(-) diff --git a/packages/nova-base-components/lib/categories/CategoriesEditForm.jsx b/packages/nova-base-components/lib/categories/CategoriesEditForm.jsx index b037f3a19..9300dbd16 100644 --- a/packages/nova-base-components/lib/categories/CategoriesEditForm.jsx +++ b/packages/nova-base-components/lib/categories/CategoriesEditForm.jsx @@ -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}); + }} /> -
- ) } @@ -54,7 +39,6 @@ CategoriesEditForm.propTypes = { } CategoriesEditForm.contextTypes = { - actions: React.PropTypes.object, closeCallback: React.PropTypes.func, currentUser: React.PropTypes.object, }; diff --git a/packages/nova-base-components/lib/comments/CommentsEditForm.jsx b/packages/nova-base-components/lib/comments/CommentsEditForm.jsx index b9b1305c1..cfbefea04 100644 --- a/packages/nova-base-components/lib/comments/CommentsEditForm.jsx +++ b/packages/nova-base-components/lib/comments/CommentsEditForm.jsx @@ -15,6 +15,7 @@ const CommentsEditForm = (props, context) => { resultQuery={Comments.graphQLQueries.single} successCallback={props.successCallback} cancelCallback={props.cancelCallback} + removeSuccessCallback={props.removeSuccessCallback} /> ) diff --git a/packages/nova-base-components/lib/comments/CommentsItem.jsx b/packages/nova-base-components/lib/comments/CommentsItem.jsx index 3e90aed8e..76fb57c62 100644 --- a/packages/nova-base-components/lib/comments/CommentsItem.jsx +++ b/packages/nova-base-components/lib/comments/CommentsItem.jsx @@ -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" /> @@ -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{
-
@@ -130,7 +121,6 @@ CommentsItem.propTypes = { CommentsItem.contextTypes = { currentUser: React.PropTypes.object, - actions: React.PropTypes.object, events: React.PropTypes.object, intl: intlShape }; diff --git a/packages/nova-base-components/lib/posts/PostsEditForm.jsx b/packages/nova-base-components/lib/posts/PostsEditForm.jsx index 81de15770..3c103e04e 100644 --- a/packages/nova-base-components/lib/posts/PostsEditForm.jsx +++ b/packages/nova-base-components/lib/posts/PostsEditForm.jsx @@ -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}); }} /> diff --git a/packages/nova-base-components/lib/users/UsersEditForm.jsx b/packages/nova-base-components/lib/users/UsersEditForm.jsx index d94f01b0e..bd97c1b31 100644 --- a/packages/nova-base-components/lib/users/UsersEditForm.jsx +++ b/packages/nova-base-components/lib/users/UsersEditForm.jsx @@ -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} /> diff --git a/packages/nova-forms/lib/FormWrapper.jsx b/packages/nova-forms/lib/FormWrapper.jsx index d8b80e4dc..f7b5225ee 100644 --- a/packages/nova-forms/lib/FormWrapper.jsx +++ b/packages/nova-forms/lib/FormWrapper.jsx @@ -419,6 +419,7 @@ class FormWrapper extends Component{ render() { const fieldGroups = this.getFieldGroups(); + const collectionName = this.props.collection._name; return (
@@ -438,8 +439,8 @@ class FormWrapper extends Component{ this.props.removeMutation ?

- - + +
: null diff --git a/packages/nova-forms/lib/withEdit.jsx b/packages/nova-forms/lib/withEdit.jsx index a2cbdfbec..22bed896a 100644 --- a/packages/nova-forms/lib/withEdit.jsx +++ b/packages/nova-forms/lib/withEdit.jsx @@ -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 + // add the remove mutation by default unless it's explicitly specified not to do it + const ComponentToRender = this.props.noRemoveMutation ? ComponentWithEdit : withRemove(ComponentWithEdit); + + return } } diff --git a/packages/nova-i18n-en-us/lib/en_US.js b/packages/nova-i18n-en-us/lib/en_US.js index 43a933270..cf0ba3c8b 100644 --- a/packages/nova-i18n-en-us/lib/en_US.js +++ b/packages/nova-i18n-en-us/lib/en_US.js @@ -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 settings.json file will be disabled.",