Commit 5d6153ce authored by Rene Saarsoo's avatar Rene Saarsoo
Browse files

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.
parent 4cdeebc7
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -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 });
    });
});

+4 −3
Original line number Diff line number Diff line
@@ -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);
+2 −1
Original line number Diff line number Diff line
@@ -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);
+8 −0
Original line number Diff line number Diff line
@@ -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);

+52 −7
Original line number Diff line number Diff line
@@ -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