FIX: Clear bookmarks didn't update the UI properly

This commit is contained in:
Robin Ward 2016-02-11 15:44:04 -05:00
parent 5693795dbf
commit a0d61ebf7f
2 changed files with 45 additions and 34 deletions

View File

@ -308,7 +308,10 @@ export default Ember.Controller.extend(SelectedPostsCount, BufferedContent, {
if (post) { if (post) {
return post.toggleBookmark().catch(popupAjaxError); return post.toggleBookmark().catch(popupAjaxError);
} else { } else {
return this.get("model").toggleBookmark(); return this.get("model").toggleBookmark().then(changedIds => {
if (!changedIds) { return; }
changedIds.forEach(id => this.appEvents.trigger('post-stream:refresh', id));
});
} }
}, },

View File

@ -225,26 +225,36 @@ const Topic = RestModel.extend({
}, },
toggleBookmark() { toggleBookmark() {
if (this.get("bookmarking")) { return; } if (this.get('bookmarking')) { return Ember.RSVP.Promise.resolve(); }
this.set("bookmarking", true); this.set("bookmarking", true);
const self = this, const stream = this.get('postStream');
stream = this.get('postStream'), const posts = Em.get(stream, 'posts');
posts = Em.get(stream, 'posts'), const firstPost = posts && posts[0] && posts[0].get('post_number') === 1 && posts[0];
firstPost = posts && posts[0] && posts[0].get('post_number') === 1 && posts[0], const bookmark = !this.get('bookmarked');
bookmark = !this.get('bookmarked'), const path = bookmark ? '/bookmark' : '/remove_bookmarks';
path = bookmark ? '/bookmark' : '/remove_bookmarks';
const toggleBookmarkOnServer = function() { const toggleBookmarkOnServer = () => {
return Discourse.ajax('/t/' + self.get('id') + path, { return Discourse.ajax(`/t/${this.get('id')}${path}`, { type: 'PUT' }).then(() => {
type: 'PUT', this.toggleProperty('bookmarked');
}).then(function() { if (bookmark && firstPost) {
self.toggleProperty('bookmarked'); firstPost.set('bookmarked', true);
if (bookmark && firstPost) { firstPost.set('bookmarked', true); } return [firstPost.id];
if (!bookmark && posts) {
posts.forEach((post) => post.get('bookmarked') && post.set('bookmarked', false));
} }
}).catch(function(error) { if (!bookmark && posts) {
const updated = [];
posts.forEach(post => {
if (post.get('bookmarked')) {
post.set('bookmarked', false);
updated.push(post.get('id'));
}
});
return updated;
}
return [];
}).catch(error => {
let showGenericError = true; let showGenericError = true;
if (error && error.responseText) { if (error && error.responseText) {
try { try {
@ -258,28 +268,26 @@ const Topic = RestModel.extend({
} }
throw error; throw error;
}).finally(function() { }).finally(() => this.set('bookmarking', false));
self.set("bookmarking", false);
});
}; };
let unbookmarkedPosts = []; const unbookmarkedPosts = [];
if (!bookmark && posts) { if (!bookmark && posts) {
posts.forEach((post) => post.get('bookmarked') && unbookmarkedPosts.push(post)); posts.forEach(post => post.get('bookmarked') && unbookmarkedPosts.push(post));
} }
if (unbookmarkedPosts.length > 1) { return new Ember.RSVP.Promise(resolve => {
return bootbox.confirm( if (unbookmarkedPosts.length > 1) {
I18n.t("bookmarks.confirm_clear"), bootbox.confirm(
I18n.t("no_value"), I18n.t("bookmarks.confirm_clear"),
I18n.t("yes_value"), I18n.t("no_value"),
function (confirmed) { I18n.t("yes_value"),
if (confirmed) { return toggleBookmarkOnServer(); } confirmed => confirmed ? toggleBookmarkOnServer().then(resolve) : resolve()
} );
); } else {
} else { toggleBookmarkOnServer().then(resolve);
return toggleBookmarkOnServer(); }
} });
}, },
createInvite(emailOrUsername, groupNames) { createInvite(emailOrUsername, groupNames) {