Skip to content

Commit

Permalink
FIX: Order items properly in user menu
Browse files Browse the repository at this point in the history
The recent changes to the user menu changed how we were ordering the
items in it. A small bug was noticed though, as we’re not displaying the
unread assignments at the very top.

This patch fixes the ordering like this:

- Unread individual assignment notifications
- Unread group assignment notifications
- Read individual assignments
- Read group assignments

In each group of items, they are sorted by date, the most recent ones
being at the top.
  • Loading branch information
Flink committed Nov 9, 2023
1 parent 41f08fd commit 5055bcb
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
22 changes: 17 additions & 5 deletions assets/javascripts/discourse/components/user-menu/assigns-list.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { set } from "@ember/object";
import { sort } from "@ember/object/computed";
import UserMenuNotificationsList from "discourse/components/user-menu/notifications-list";
import I18n from "I18n";
import UserMenuAssignsListEmptyState from "./assigns-list-empty-state";
Expand Down Expand Up @@ -51,10 +53,20 @@ export default class UserMenuAssignNotificationsList extends UserMenuNotificatio
}

async fetchItems() {
// sorting by `data.message` length to group single user assignments and
// group assignments, then by `created_at` to keep chronological order.
return (await super.fetchItems())
.sortBy("notification.data.message", "notification.created_at")
.reverse();
return new SortedItems(await super.fetchItems()).sortedItems;
}
}

class SortedItems {
itemsSorting = [
"notification.read",
"notification.data.message:desc",
"notification.created_at:desc",
];

@sort("items", "itemsSorting") sortedItems;

constructor(items) {
set(this, "items", items);
}
}
21 changes: 21 additions & 0 deletions spec/fabricators/notification_fabricator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# frozen_string_literal: true

Fabricator(:assignment_notification, from: :notification) do
transient :group
notification_type Notification.types[:assigned]
post_number 1
data do |attrs|
{
message:
(
if attrs[:group]
"discourse_assign.assign_group_notification"
else
"discourse_assign.assign_notification"
end
),
display_username: attrs[:group] ? "group" : "user",
assignment_id: rand(1..100),
}.to_json
end
end
17 changes: 17 additions & 0 deletions spec/system/page_objects/pages/user_menu.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

module PageObjects
module Components
class UserMenu < PageObjects::Components::Base
def click_assignments_tab
click_link("user-menu-button-assign-list")
has_css?("#quick-access-assign-list")
self
end

def has_assignments_in_order?(assignments)
expect(all(".notification.assigned .item-description").map(&:text)).to eq(assignments)
end
end
end
end
40 changes: 40 additions & 0 deletions spec/system/user_menu_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

RSpec.describe "Assign | User Menu", type: :system, js: true do
fab!(:admin) { Fabricate(:admin) }

let(:user_menu) { PageObjects::Components::UserMenu.new }

before do
SiteSetting.assign_enabled = true
sign_in(admin)
end

describe "Assign tab ordering" do
let!(:unread_user_assign) { Fabricate(:assignment_notification, user: admin) }
let!(:unread_user_assign_2) { Fabricate(:assignment_notification, user: admin) }
let!(:read_user_assign) { Fabricate(:assignment_notification, user: admin, read: true) }
let!(:read_user_assign_2) { Fabricate(:assignment_notification, user: admin, read: true) }
let!(:unread_group_assign) { Fabricate(:assignment_notification, user: admin, group: true) }
let!(:read_group_assign) do
Fabricate(:assignment_notification, user: admin, read: true, group: true)
end
let(:expected_order) do
[
unread_user_assign_2,
unread_user_assign,
unread_group_assign,
read_user_assign_2,
read_user_assign,
read_group_assign,
].map { _1.topic.fancy_title }
end

it "orders the items properly" do
visit "/"
user_menu.open
user_menu.click_assignments_tab
expect(user_menu).to have_assignments_in_order(expected_order)
end
end
end

0 comments on commit 5055bcb

Please sign in to comment.