diff --git a/web/app/components/input/input.jsx b/web/app/components/input/input.jsx index 32c0b437..402d41f6 100644 --- a/web/app/components/input/input.jsx +++ b/web/app/components/input/input.jsx @@ -95,38 +95,24 @@ export default class Input extends Component { this.setState({ isFieldDisabled: true, isErrorShown: false }); - if (mods.mode === 'edit') { - api.edit({ text, id }) - .then(comment => { - if (this.props.onSubmit) { - this.props.onSubmit(comment); - } + const request = mods.mode === 'edit' + ? api.edit({ text, id }) + : api.send({ text, ...(pid ? { pid } : {}) }); - this.fieldNode.value = ''; - this.fieldNode.style.height = ''; - this.setState({ preview: null }); - }) - .catch(() => { - this.setState({ isErrorShown: true }); - }) - .finally(() => this.setState({ isFieldDisabled: false })); - } else { - api.send({ text, ...(pid ? { pid } : {}) }) - .then(({ id }) => { - // TODO: maybe we should run onsubmit before send; like in optimistic ui - if (this.props.onSubmit) { - this.props.onSubmit({ text, id, pid }); - } + request + .then(comment => { + if (this.props.onSubmit) { + this.props.onSubmit(comment); + } - this.fieldNode.value = ''; - this.fieldNode.style.height = ''; - this.setState({ preview: null }); - }) - .catch(() => { - this.setState({ isErrorShown: true }); - }) - .finally(() => this.setState({ isFieldDisabled: false })); - } + this.fieldNode.value = ''; + this.fieldNode.style.height = ''; + this.setState({ preview: null }); + }) + .catch(() => { + this.setState({ isErrorShown: true }); + }) + .finally(() => this.setState({ isFieldDisabled: false })); } getPreview() { diff --git a/web/app/components/root/root.jsx b/web/app/components/root/root.jsx index 0bcd3859..56ccaddc 100644 --- a/web/app/components/root/root.jsx +++ b/web/app/components/root/root.jsx @@ -33,7 +33,7 @@ export default class Root extends Component { sort, }; - this.addComment = this.addComment.bind(this); + this.replaceComment = this.replaceComment.bind(this); this.onSignIn = this.onSignIn.bind(this); this.onSignOut = this.onSignOut.bind(this); this.onBlockedUsersShow = this.onBlockedUsersShow.bind(this); @@ -157,17 +157,7 @@ export default class Root extends Component { this.setState({ wasSomeoneUnblocked: true }); } - addComment(data) { - store.addComment(data); - this.setState({ comments: store.get('comments') }); - - api.getComment({ id: data.id }).then(comment => { - store.replaceComment(comment); - this.setState({ comments: store.get('comments') }); - }); - } - - editComment(comment) { + replaceComment(comment) { store.replaceComment(comment); this.setState({ comments: store.get('comments') }); } @@ -209,7 +199,7 @@ export default class Root extends Component { ) } @@ -239,8 +229,8 @@ export default class Root extends Component { mix="root__thread" mods={{ level: 0 }} data={thread} - onReply={this.addComment} - onEdit={this.editComment} + onReply={this.replaceComment} + onEdit={this.replaceComment} /> )) }