A platform for community discussion. Free, open, simple.
FIX: Infinite scrolling on user notification pages; modernize controller (#35677)
Sérgio Saquetim0a6c7a5f286e
diff --git a/frontend/discourse/app/controllers/user-notifications.gjs b/frontend/discourse/app/controllers/user-notifications.jssimilarity index 71%rename from frontend/discourse/app/controllers/user-notifications.gjsrename to frontend/discourse/app/controllers/user-notifications.jsindex b7d53db3dc5..25d68443335 100644--- a/frontend/discourse/app/controllers/user-notifications.gjs+++ b/frontend/discourse/app/controllers/user-notifications.js@@ -1,12 +1,13 @@import { tracked } from "@glimmer/tracking";import Controller from "@ember/controller";import { action } from "@ember/object";+import { getOwner } from "@ember/owner";import { service } from "@ember/service";import { htmlSafe } from "@ember/template";+import curryComponent from "ember-curry-component";import DismissNotificationConfirmationModal from "discourse/components/modal/dismiss-notification-confirmation";import RelativeDate from "discourse/components/relative-date";import { ajax } from "discourse/lib/ajax";-import discourseComputed from "discourse/lib/decorators";import getURL from "discourse/lib/get-url";import { iconHTML } from "discourse/lib/icon-library";import UserMenuNotificationItem from "discourse/lib/user-menu/notification-item";@@ -28,19 +29,11 @@ export default class UserNotificationsController extends Controller {queryParams = ["filter"];- get listContainerClassNames() {- return `user-notifications-list ${- this.siteSettings.show_user_menu_avatars ? "show-avatars" : ""- }`;- }-- @discourseComputed("filter")- isFiltered() {+ get isFiltered() {return this.filter && this.filter !== "all";}- @discourseComputed("model.content.@each")- items() {+ get items() {return this.model.content.map((notification) => {const props = {appEvents: this.appEvents,@@ -48,33 +41,33 @@ export default class UserNotificationsController extends Controller {siteSettings: this.siteSettings,site: this.site,notification,- endComponent: <template>- <RelativeDate @date={{notification.created_at}} />- </template>,+ endComponent: curryComponent(+ RelativeDate,+ { date: notification.created_at },+ getOwner(this)+ ),};return new UserMenuNotificationItem(props);});}- @discourseComputed("[email protected]")- allNotificationsRead() {- return !this.get("model.content").some(+ get allNotificationsRead() {+ return !this.model.content.some((notification) => !notification.get("read"));}- @discourseComputed("isFiltered", "model.content.length", "loading")- doesNotHaveNotifications(isFiltered, contentLength, loading) {- return !loading && !isFiltered && contentLength === 0;+ get doesNotHaveNotifications() {+ return (+ !this.model.loading && !this.isFiltered && this.model.content.length === 0+ );}- @discourseComputed("isFiltered", "model.content.length")- nothingFound(isFiltered, contentLength) {- return isFiltered && contentLength === 0;+ get nothingFound() {+ return this.isFiltered && this.model.content.length === 0;}- @discourseComputed()- emptyStateBody() {+ get emptyStateBody() {return htmlSafe(i18n("user.no_notifications_page_body", {preferencesUrl: getURL("/my/preferences/notifications"),@@ -85,12 +78,13 @@ export default class UserNotificationsController extends Controller {async markRead() {await ajax("/notifications/mark-read", { type: "PUT" });- this.model.forEach((notification) => notification.set("read", true));+ this.model.content.forEach((notification) =>+ notification.set("read", true)+ );}@actionupdateFilter(value) {- this.loading = true;this.filter = value;}diff --git a/frontend/discourse/app/templates/user/notifications-index.gjs b/frontend/discourse/app/templates/user/notifications-index.gjsindex ce8d500b4c4..18306410f2b 100644--- a/frontend/discourse/app/templates/user/notifications-index.gjs+++ b/frontend/discourse/app/templates/user/notifications-index.gjs@@ -5,6 +5,7 @@ import MenuItem from "discourse/components/user-menu/menu-item";import lazyHash from "discourse/helpers/lazy-hash";import { i18n } from "discourse-i18n";import NotificationsFilter from "select-kit/components/notifications-filter";+import concatClass from "../../helpers/concat-class";export default <template>{{#if @controller.model.error}}@@ -38,7 +39,12 @@ export default <template>{{#if @controller.nothingFound}}<div class="alert alert-info">{{i18n "notifications.empty"}}</div>{{else}}- <div class={{@controller.listContainerClassNames}}>+ <div+ class={{concatClass+ "user-notifications-list"+ (if @controller.siteSettings.show_user_menu_avatars "show-avatars")+ }}+ >{{#each @controller.items as |item|}}<MenuItem @item={{item}} />{{/each}}diff --git a/frontend/discourse/tests/unit/controllers/user-notifications-test.js b/frontend/discourse/tests/unit/controllers/user-notifications-test.jsindex 29a4e1d31ff..d750b238236 100644--- a/frontend/discourse/tests/unit/controllers/user-notifications-test.js+++ b/frontend/discourse/tests/unit/controllers/user-notifications-test.js@@ -2,37 +2,42 @@ import EmberObject from "@ember/object";import { setupTest } from "ember-qunit";import { module, test } from "qunit";import sinon from "sinon";+import ResultSet from "discourse/models/result-set";import pretender, { response } from "discourse/tests/helpers/create-pretender";module("Unit | Controller | user-notifications", function (hooks) {setupTest(hooks);test("Mark read marks all models read when response is 200", async function (assert) {- const model = [- EmberObject.create({ read: false }),- EmberObject.create({ read: false }),- ];+ const model = ResultSet.create({+ content: [+ EmberObject.create({ read: false }),+ EmberObject.create({ read: false }),+ ],+ });const controller = this.owner.lookup("controller:user-notifications");controller.setProperties({ model });pretender.put("/notifications/mark-read", () => response({}));await controller.markRead();- assert.true(model.every(({ read }) => read === true));+ assert.true(model.content.every(({ read }) => read === true));});test("Mark read does not mark models read when response is not successful", async function (assert) {- const model = [- EmberObject.create({ read: false }),- EmberObject.create({ read: true }),- ];+ const model = ResultSet.create({+ content: [+ EmberObject.create({ read: false }),+ EmberObject.create({ read: true }),+ ],+ });const controller = this.owner.lookup("controller:user-notifications");controller.setProperties({ model });pretender.put("/notifications/mark-read", () => response(500));assert.rejects(controller.markRead());assert.deepEqual(- model.map(({ read }) => read),+ model.content.map(({ read }) => read),[false, true],"models unmodified");@@ -46,7 +51,9 @@ module("Unit | Controller | user-notifications", function (hooks) {});const controller = this.owner.lookup("controller:user-notifications");controller.setProperties({- model: [],+ model: ResultSet.create({+ content: [],+ }),currentUser,});sinon.stub(controller, "markRead").callsFake(() => (markRead = true));diff --git a/spec/system/user_page/user_notifications_spec.rb b/spec/system/user_page/user_notifications_spec.rbindex c90eca6a81d..00c1f6047ce 100644--- a/spec/system/user_page/user_notifications_spec.rb+++ b/spec/system/user_page/user_notifications_spec.rb@@ -52,6 +52,19 @@ describe "User notifications", type: :system doendend+ describe "infinite scrolling" do+ it "loads notifications progressively as user scrolls" do+ Fabricate.times(70, :notification, user: user, read: true)++ user_notifications_page.visit(user)++ expect(user_notifications_page).to have_notification_count_of(60)++ page.execute_script("window.scrollTo(0, document.body.scrollHeight)")+ expect(user_notifications_page).to have_notification_count_of(72)+ end+ end+describe "user group notifications" dofab!(:group) { Fabricate(:group, name: "Awesome_Group") }fab!(:topic) { Fabricate(:topic, title: "Group Mention Notification test") }