From 5d6153ce59ece90f1c195a30ad8427ca51bc6522 Mon Sep 17 00:00:00 2001 From: Rene Saarsoo Date: Wed, 3 Oct 2012 11:45:16 -0700 Subject: [PATCH] Implement the adding of new comment. The POST /comments API call now returns the newly added comment as a result in addition to just returning an ID. --- comments/app.js | 4 +- comments/lib/request.js | 7 +-- template/app/view/comments/Expander.js | 3 +- template/app/view/comments/Form.js | 8 +++ template/app/view/comments/ListWithForm.js | 59 +++++++++++++++++++--- 5 files changed, 68 insertions(+), 13 deletions(-) diff --git a/comments/app.js b/comments/app.js index e71eb150..087a814e 100644 --- a/comments/app.js +++ b/comments/app.js @@ -155,8 +155,8 @@ app.get('/auth/:sdk/:version/comments', Auth.hasStartKey, function(req, res) { // Adds new comment app.post('/auth/:sdk/:version/comments', Auth.isLoggedIn, function(req, res) { - new Request(req).addComment(req.body.target, req.body.comment, req.body.url, function(comment_id) { - res.json({ id: comment_id, success: true }); + new Request(req).addComment(req.body.target, req.body.comment, req.body.url, function(comment) { + res.json({ id: comment._id, comment: comment, success: true }); }); }); diff --git a/comments/lib/request.js b/comments/lib/request.js index 9c2582bc..1eaf3771 100644 --- a/comments/lib/request.js +++ b/comments/lib/request.js @@ -124,11 +124,12 @@ Request.prototype = { this.db.comments().add(comment, function(err, comment_id) { if (this.isModerator()) { this.markRead(comment_id, function() { - callback(comment_id); - }); + this.setCommentsTableOptions(); + this.getComment(comment_id, callback); + }.bind(this)); } else { - callback(comment_id); + this.getComment(comment_id, callback); } this.sendEmailUpdates(comment_id, threadUrl); diff --git a/template/app/view/comments/Expander.js b/template/app/view/comments/Expander.js index efc20f2a..fcbedde0 100644 --- a/template/app/view/comments/Expander.js +++ b/template/app/view/comments/Expander.js @@ -97,11 +97,12 @@ Ext.define('Docs.view.comments.Expander', { }, loadComments: function(div) { + var target = [this.type, this.className, this.memberId]; this.list = new Docs.view.comments.ListWithForm({ + target: target, renderTo: div }); - var target = [this.type, this.className, this.memberId]; Docs.Comments.load(target, function(comments) { this.list.load(comments); }, this); diff --git a/template/app/view/comments/Form.js b/template/app/view/comments/Form.js index dc191a8f..37c1e05f 100644 --- a/template/app/view/comments/Form.js +++ b/template/app/view/comments/Form.js @@ -142,6 +142,14 @@ Ext.define('Docs.view.comments.Form', { this.callParent(arguments); }, + /** + * Sets the text inside editor. + * @param {String} value + */ + setValue: function(value) { + this.codeMirror.setValue(value); + }, + afterRender: function() { this.callParent(arguments); diff --git a/template/app/view/comments/ListWithForm.js b/template/app/view/comments/ListWithForm.js index 63c17c85..0ffaf359 100644 --- a/template/app/view/comments/ListWithForm.js +++ b/template/app/view/comments/ListWithForm.js @@ -8,23 +8,68 @@ Ext.define('Docs.view.comments.ListWithForm', { requires: [ 'Docs.view.comments.List', 'Docs.view.comments.Form', + 'Docs.Comments', 'Docs.Auth' ], + /** + * @cfg {String[]} target + * The target of the comments (used for posting new comment). + */ + initComponent: function() { this.items = [ - { - xtype: 'commentsList' - }, - { - xtype: 'commentsForm', - user: Docs.Auth.getUser() - } + this.list = new Docs.view.comments.List({ + }), + this.form = new Docs.view.comments.Form({ + user: Docs.Auth.getUser(), + listeners: { + submit: this.postComment, + scope: this + } + }) ]; this.callParent(arguments); }, + postComment: function(content) { + Docs.Comments.request("ajax", { + url: '/comments', + method: 'POST', + params: { + target: Ext.JSON.encode(this.target), + comment: content, + url: this.buildUrl(this.target) + }, + callback: function(options, success, response) { + var data = Ext.JSON.decode(response.responseText); + if (success && data.success && data.id) { + this.form.setValue(''); + this.list.load([data.comment], true); + } + else { + Ext.Msg.alert('Error', data.reason || "There was an error submitting your request"); + } + }, + scope: this + }); + }, + + buildUrl: function(target) { + if (target[0] == 'video') { + var hash = '#!/video/' + target[1]; + } + else if (target[0] == 'guide') { + var hash = '#!/guide/' + target[1]; + } + else if (target[2] != '') { + var hash = '#!/api/' + target[1] + '-' + target[2]; + } + + return "http://" + window.location.host + window.location.pathname + hash; + }, + /** * Loads array of comments into the view. * @param {Object[]} comments -- GitLab