diff --git a/app/assets/javascripts/discourse/app/components/discovery/topics.js b/app/assets/javascripts/discourse/app/components/discovery/topics.js index 01e3a5f3da7..7e4ff6a1164 100644 --- a/app/assets/javascripts/discourse/app/components/discovery/topics.js +++ b/app/assets/javascripts/discourse/app/components/discovery/topics.js @@ -24,7 +24,7 @@ export default class DiscoveryTopics extends Component { } get ascending() { - return this.args.model.get("params.ascending"); + return this.args.model.get("params.ascending") === "true"; } get hasTopics() { diff --git a/app/assets/javascripts/discourse/app/controllers/discovery/list.js b/app/assets/javascripts/discourse/app/controllers/discovery/list.js index 7d80e1b5f63..6d9335b0f4c 100644 --- a/app/assets/javascripts/discourse/app/controllers/discovery/list.js +++ b/app/assets/javascripts/discourse/app/controllers/discovery/list.js @@ -101,6 +101,16 @@ export default class DiscoveryListController extends Controller { return this.model.category && !this.createTopicTargetCategory; } + get resolvedAscending() { + return ( + (this.ascending ?? this.model.list.get("params.ascending")) === "true" + ); + } + + get resolvedOrder() { + return this.order ?? this.model.list.get("params.order") ?? "activity"; + } + @action createTopic() { this.composer.openNewTopic({ @@ -120,14 +130,12 @@ export default class DiscoveryListController extends Controller { @action changeSort(sortBy) { - if (sortBy === this.order) { - this.ascending = !this.ascending; - this.model.list.updateSortParams(sortBy, this.ascending); + if (sortBy === this.resolvedOrder) { + this.ascending = !this.resolvedAscending; } else { - this.order = sortBy; this.ascending = false; - this.model.list.updateSortParams(sortBy, false); } + this.order = sortBy; } @action diff --git a/spec/system/discovery_list_spec.rb b/spec/system/discovery_list_spec.rb new file mode 100644 index 00000000000..1ac2c564016 --- /dev/null +++ b/spec/system/discovery_list_spec.rb @@ -0,0 +1,41 @@ +# frozen_string_literal: true + +describe "Topic list focus", type: :system do + fab!(:topics) { Fabricate.times(10, :post).map(&:topic) } + fab!(:reply) { Fabricate(:post, topic: topics.first) } + + let(:discovery) { PageObjects::Pages::Discovery.new } + + def nth_topic_id(n) + discovery.topic_list.find(".topic-list-item:nth-of-type(#{n})")["data-topic-id"] + end + + it "can sort a topic list by activity" do + visit "/latest" + expect(discovery.topic_list).to have_topics(count: 10) + newest_topic_id = nth_topic_id(1) + + find("th[data-sort-order='activity']").click + + expect(page).to have_css("th[data-sort-order='activity'][aria-sort=ascending]") + expect(nth_topic_id(10)).to eq(newest_topic_id) + + find("th[data-sort-order='activity']").click + expect(page).to have_css("th[data-sort-order='activity'][aria-sort=descending]") + expect(nth_topic_id(1)).to eq(newest_topic_id) + end + + it "can sort a topic list by replies" do + visit "/latest" + expect(discovery.topic_list).to have_topics(count: 10) + + find("th[data-sort-order='posts']").click + + expect(page).to have_css("th[data-sort-order='posts'][aria-sort=descending]") + expect(nth_topic_id(1)).to eq(reply.topic_id.to_s) + + find("th[data-sort-order='posts']").click + expect(page).to have_css("th[data-sort-order='posts'][aria-sort=ascending]") + expect(nth_topic_id(10)).to eq(reply.topic_id.to_s) + end +end