From 763a99ec30b6a4acfd844261e49e906099726541 Mon Sep 17 00:00:00 2001 From: Arthur Schiwon Date: Wed, 19 Oct 2016 13:56:49 +0200 Subject: [PATCH] consolidate error handling Signed-off-by: Arthur Schiwon --- apps/comments/js/commentstabview.js | 68 +++++++++++++++++------------ 1 file changed, 41 insertions(+), 27 deletions(-) diff --git a/apps/comments/js/commentstabview.js b/apps/comments/js/commentstabview.js index 828321a6f5..e2cefbfd11 100644 --- a/apps/comments/js/commentstabview.js +++ b/apps/comments/js/commentstabview.js @@ -386,6 +386,15 @@ this.nextPage(); }, + /** + * takes care of updating comment elements after submit (either new + * comment or edit). + * + * @param {OC.Backbone.Model} model + * @param {jQuery} $form + * @param {string|undefined} commentId + * @private + */ _onSubmitSuccess: function(model, $form, commentId) { var self = this; var $submit = $form.find('.submit'); @@ -396,32 +405,25 @@ success: function(model) { $submit.removeClass('hidden'); $loading.addClass('hidden'); + var $target; + if(!_.isUndefined(commentId)) { var $row = $form.closest('.comment'); - $row.data('commentEl') - .removeClass('hidden') - .find('.message') - .html(self._formatMessage(model.get('message'), model.get('mentions'))) - .find('.avatar') - .each(function () { $(this).avatar(); }); + $target = $row.data('commentEl'); + $target.removeClass('hidden'); $row.remove(); } else { - var $row = $form.closest('.comments'); - $('.commentsTabView .comments').find('li:first') - .find('.message') - .html(self._formatMessage(model.get('message'), model.get('mentions'))) - .find('.avatar') - .each(function () { $(this).avatar(); }); + $target = $('.commentsTabView .comments').find('li:first'); $textArea.val('').prop('disabled', false); } + $target.find('.message') + .html(self._formatMessage(model.get('message'), model.get('mentions'))) + .find('.avatar') + .each(function () { $(this).avatar(); }); }, error: function () { - $submit.removeClass('hidden'); - $loading.addClass('hidden'); - $textArea.prop('disabled', false); - - OC.Notification.showTemporary(t('comments', 'Error occurred while updating comment with id {id}', {id: commentId})); + self._onSubmitError($form, commentId); } }); }, @@ -455,11 +457,7 @@ self._onSubmitSuccess(model, $form, commentId); }, error: function() { - $submit.removeClass('hidden'); - $loading.addClass('hidden'); - $textArea.prop('disabled', false); - - OC.Notification.showTemporary(t('comments', 'Error occurred while updating comment with id {id}', {id: commentId})); + self._onSubmitError($form, commentId); } }); } else { @@ -478,11 +476,7 @@ self._onSubmitSuccess(model, $form); }, error: function() { - $submit.removeClass('hidden'); - $loading.addClass('hidden'); - $textArea.prop('disabled', false); - - OC.Notification.showTemporary(t('comments', 'Error occurred while posting comment')); + self._onSubmitError($form); } }); } @@ -490,6 +484,26 @@ return false; }, + /** + * takes care of updating the UI after an error on submit (either new + * comment or edit). + * + * @param {jQuery} $form + * @param {string|undefined} commentId + * @private + */ + _onSubmitError: function($form, commentId) { + $form.find('.submit').removeClass('hidden'); + $form.find('.submitLoading').addClass('hidden'); + $form.find('.message').prop('disabled', false); + + if(!_.isUndefined(commentId)) { + OC.Notification.showTemporary(t('comments', 'Error occurred while updating comment with id {id}', {id: commentId})); + } else { + OC.Notification.showTemporary(t('comments', 'Error occurred while posting comment')); + } + }, + /** * Returns whether the given message is long and needs * collapsing