diff --git a/app/assets/javascripts/discourse/models/topic.js.es6 b/app/assets/javascripts/discourse/models/topic.js.es6 index 054f35a67bd..a2e984ea30c 100644 --- a/app/assets/javascripts/discourse/models/topic.js.es6 +++ b/app/assets/javascripts/discourse/models/topic.js.es6 @@ -179,63 +179,61 @@ const Topic = Discourse.Model.extend({ }.property('word_count'), toggleBookmark() { + if (this.get("bookmarking")) { return; } + this.set("bookmarking", true); + const self = this, stream = this.get('postStream'), posts = Em.get(stream, 'posts'), - firstPost = posts && - posts[0] && - posts[0].get('post_number') === 1 && - posts[0], - bookmark = !self.get('bookmarked'); + firstPost = posts && posts[0] && posts[0].get('post_number') === 1 && posts[0], + bookmark = !this.get('bookmarked'), + path = bookmark ? '/bookmark' : '/remove_bookmarks'; - var path = bookmark ? '/bookmark' : '/remove_bookmarks'; - var unbookmarkedPosts = [], - bookmarkedPost; + const toggleBookmarkOnServer = function() { + return Discourse.ajax('/t/' + self.get('id') + path, { + type: 'PUT', + }).then(function() { + self.toggleProperty('bookmarked'); + if (bookmark && firstPost) { firstPost.set('bookmarked', true); } + if (!bookmark && posts) { + posts.forEach((post) => post.get('bookmarked') && post.set('bookmarked', false)); + } + }).catch(function(error) { + let showGenericError = true; + if (error && error.responseText) { + try { + bootbox.alert($.parseJSON(error.responseText).errors); + showGenericError = false; + } catch(e) { } + } - this.toggleProperty('bookmarked'); + if (showGenericError) { + bootbox.alert(I18n.t('generic_error')); + } - if (bookmark && firstPost) { - firstPost.set('bookmarked', true); - bookmarkedPost = firstPost; - } + throw error; + }).finally(function() { + self.set("bookmarking", false); + }); + }; + let unbookmarkedPosts = []; if (!bookmark && posts) { - posts.forEach(function(post){ - if(post.get('bookmarked')){ - post.set('bookmarked', false); - unbookmarkedPosts.push(post); - } - }); + posts.forEach((post) => post.get('bookmarked') && unbookmarkedPosts.push(post)); } - return Discourse.ajax('/t/' + this.get('id') + path, { - type: 'PUT', - }).catch(function(error) { - - self.toggleProperty('bookmarked'); - - if(bookmarkedPost) { - bookmarkedPost.set('bookmarked', false); - } - - unbookmarkedPosts.forEach(function(p){ - p.set('bookmarked', true); - }); - - let showGenericError = true; - if (error && error.responseText) { - try { - bootbox.alert($.parseJSON(error.responseText).errors); - showGenericError = false; - } catch(e){} - } - - if(showGenericError){ - bootbox.alert(I18n.t('generic_error')); - } - - throw error; - }); + if (unbookmarkedPosts.length > 1) { + return bootbox.confirm( + I18n.t("bookmarks.confirm_clear"), + I18n.t("no_value"), + I18n.t("yes_value"), + function (confirmed) { + if (confirmed) { return toggleBookmarkOnServer(); } + } + ); + } else { + return toggleBookmarkOnServer(); + } }, createInvite(emailOrUsername, groupNames) { diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index 81a8405c91e..5fd0fdd60b2 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -179,6 +179,7 @@ en: not_bookmarked: "you've read this post; click to bookmark it" last_read: "this is the last post you've read; click to bookmark it" remove: "Remove Bookmark" + confirm_clear: "Are you sure you want to clear all the bookmarks from this topic?" topic_count_latest: one: "{{count}} new or updated topic."