Skip to content

Commit

Permalink
DEV: Use topic-list-header-sortable-column transformer (#651)
Browse files Browse the repository at this point in the history
  • Loading branch information
CvX authored Dec 2, 2024
1 parent b8d50e9 commit 026d755
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 18 deletions.
52 changes: 34 additions & 18 deletions assets/javascripts/discourse/initializers/disable-sort.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,46 @@
import { withPluginApi } from "discourse/lib/plugin-api";
import { withSilencedDeprecations } from "discourse-common/lib/deprecated";
import discourseComputed from "discourse-common/utils/decorators";

export default {
name: "disable-sort",
initialize() {

initialize(container) {
withPluginApi("0.8", (api) => {
api.modifyClass(
"component:topic-list",
(Superclass) =>
class extends Superclass {
@discourseComputed(
"category",
"siteSettings.disable_resorting_on_categories_enabled"
)
sortable(category, disable_resorting_on_categories_enabled) {
let disableSort = false;
if (
disable_resorting_on_categories_enabled &&
category?.custom_fields
) {
disableSort =
!!category.custom_fields["disable_topic_resorting"];
// TODO: cvx - remove after the glimmer topic list transition
withSilencedDeprecations("discourse.hbr-topic-list-overrides", () => {
api.modifyClass(
"component:topic-list",
(Superclass) =>
class extends Superclass {
@discourseComputed(
"category",
"siteSettings.disable_resorting_on_categories_enabled"
)
sortable(category, disable_resorting_on_categories_enabled) {
const disableSort =
disable_resorting_on_categories_enabled &&
!!category?.custom_fields?.disable_topic_resorting;

return super.sortable && !disableSort;
}
return !!this.changeSort && !disableSort;
}
);
});

api.registerValueTransformer(
"topic-list-header-sortable-column",
({ value, context }) => {
if (!value) {
return value;
}

const siteSettings = container.lookup("service:site-settings");
return !(
siteSettings.disable_resorting_on_categories_enabled &&
context.category?.custom_fields?.disable_topic_resorting
);
}
);
});
},
Expand Down
49 changes: 49 additions & 0 deletions spec/system/disable_sort_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# frozen_string_literal: true

describe "Disabling topic list sorting", type: :system do
fab!(:category)
let(:category_page) { PageObjects::Pages::Category.new }

before do
SiteSetting.calendar_enabled = true
Fabricate.times(2, :topic, category:)
end

it "disables the ability to sort topic list columns" do
category_page.visit(category)
expect(find("th.activity")).to match_selector(".sortable")

category.custom_fields["disable_topic_resorting"] = true
category.save!
page.refresh
expect(find("th.activity")).to match_selector(".sortable")

SiteSetting.disable_resorting_on_categories_enabled = true
page.refresh
expect(find("th.activity")).to_not match_selector(".sortable")
end

# TODO: cvx - remove after the glimmer topic list transition
context "when glimmer topic list is enabled" do
fab!(:user)

before do
SiteSetting.experimental_glimmer_topic_list_groups = Group::AUTO_GROUPS[:everyone]
sign_in(user)
end

it "disables the ability to sort topic list columns" do
category_page.visit(category)
expect(find("th.activity")).to match_selector(".sortable")

category.custom_fields["disable_topic_resorting"] = true
category.save!
page.refresh
expect(find("th.activity")).to match_selector(".sortable")

SiteSetting.disable_resorting_on_categories_enabled = true
page.refresh
expect(find("th.activity")).to_not match_selector(".sortable")
end
end
end

0 comments on commit 026d755

Please sign in to comment.