This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/discourse/controllers/user-activity-bookmarks-with-reminders.js
Martin Brennan c07dd0d22a
FEATURE: Add lazy loading to user bookmarks list (#9317)
This is so users with huge amount of bookmarks do not have to wait a long time to see results.

* Add a bookmark list and list serializer to server-side to be able to handle paging and load more URL
* Use load-more component to load more bookmark items, 20 at a time in user activity
* Change the way current user is loaded for bookmark ember models because it was breaking/losing resolvedTimezone when loading more items
2020-04-01 14:09:07 +10:00

86 lines
2.0 KiB
JavaScript

import Controller from "@ember/controller";
import { Promise } from "rsvp";
import { inject } from "@ember/controller";
import discourseComputed from "discourse-common/utils/decorators";
import Bookmark from "discourse/models/bookmark";
export default Controller.extend({
application: inject(),
user: inject(),
content: null,
loading: false,
noResultsHelp: null,
loadItems() {
this.setProperties({
content: [],
loading: true,
noResultsHelp: null
});
return this.model
.loadItems()
.then(response => {
this.processLoadResponse(response);
})
.catch(() => {
this.set("noResultsHelp", I18n.t("bookmarks.list_permission_denied"));
})
.finally(() =>
this.setProperties({
loaded: true,
loading: false
})
);
},
@discourseComputed("loaded", "content.length")
noContent(loaded, contentLength) {
return loaded && contentLength === 0;
},
processLoadResponse(response) {
response = response.user_bookmark_list;
if (response && response.no_results_help) {
this.set("noResultsHelp", response.no_results_help);
}
this.model.more_bookmarks_url = response.more_bookmarks_url;
if (response && response.bookmarks) {
let bookmarks = [];
response.bookmarks.forEach(bookmark => {
bookmarks.push(Bookmark.create(bookmark));
});
this.content.pushObjects(bookmarks);
}
},
actions: {
removeBookmark(bookmark) {
return bookmark.destroy().then(() => this.loadItems());
},
loadMore() {
if (this.loadingMore) {
return Promise.resolve();
}
this.set("loadingMore", true);
return this.model
.loadMore()
.then(response => this.processLoadResponse(response))
.catch(() => {
this.set("noResultsHelp", I18n.t("bookmarks.list_permission_denied"));
})
.finally(() =>
this.setProperties({
loadingMore: false
})
);
}
}
});