FEATURE: ask confirmation when clearing more than 1 bookmark

This commit is contained in:
Régis Hanol
2015-03-16 22:06:11 +01:00
parent 4720d0c12b
commit 6be645e1ca
2 changed files with 46 additions and 47 deletions

View File

@ -179,63 +179,61 @@ const Topic = Discourse.Model.extend({
}.property('word_count'), }.property('word_count'),
toggleBookmark() { toggleBookmark() {
if (this.get("bookmarking")) { return; }
this.set("bookmarking", true);
const self = this, const self = this,
stream = this.get('postStream'), stream = this.get('postStream'),
posts = Em.get(stream, 'posts'), posts = Em.get(stream, 'posts'),
firstPost = posts && firstPost = posts && posts[0] && posts[0].get('post_number') === 1 && posts[0],
posts[0] && bookmark = !this.get('bookmarked'),
posts[0].get('post_number') === 1 && path = bookmark ? '/bookmark' : '/remove_bookmarks';
posts[0],
bookmark = !self.get('bookmarked');
var path = bookmark ? '/bookmark' : '/remove_bookmarks'; const toggleBookmarkOnServer = function() {
var unbookmarkedPosts = [], return Discourse.ajax('/t/' + self.get('id') + path, {
bookmarkedPost; 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) { throw error;
firstPost.set('bookmarked', true); }).finally(function() {
bookmarkedPost = firstPost; self.set("bookmarking", false);
} });
};
let unbookmarkedPosts = [];
if (!bookmark && posts) { if (!bookmark && posts) {
posts.forEach(function(post){ posts.forEach((post) => post.get('bookmarked') && unbookmarkedPosts.push(post));
if(post.get('bookmarked')){
post.set('bookmarked', false);
unbookmarkedPosts.push(post);
}
});
} }
return Discourse.ajax('/t/' + this.get('id') + path, { if (unbookmarkedPosts.length > 1) {
type: 'PUT', return bootbox.confirm(
}).catch(function(error) { I18n.t("bookmarks.confirm_clear"),
I18n.t("no_value"),
self.toggleProperty('bookmarked'); I18n.t("yes_value"),
function (confirmed) {
if(bookmarkedPost) { if (confirmed) { return toggleBookmarkOnServer(); }
bookmarkedPost.set('bookmarked', false); }
} );
} else {
unbookmarkedPosts.forEach(function(p){ return toggleBookmarkOnServer();
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;
});
}, },
createInvite(emailOrUsername, groupNames) { createInvite(emailOrUsername, groupNames) {

View File

@ -179,6 +179,7 @@ en:
not_bookmarked: "you've read this post; click to bookmark it" 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" last_read: "this is the last post you've read; click to bookmark it"
remove: "Remove Bookmark" remove: "Remove Bookmark"
confirm_clear: "Are you sure you want to clear all the bookmarks from this topic?"
topic_count_latest: topic_count_latest:
one: "{{count}} new or updated topic." one: "{{count}} new or updated topic."