A platform for community discussion. Free, open, simple.
UX: Show event dates on topics in the categories page lists (#43657)
Krisa95804046acd
diff --git a/frontend/discourse/app/components/topic-list/featured-topic.gjs b/frontend/discourse/app/components/topic-list/featured-topic.gjsindex 349c3b24c9d..926d4221cc2 100644--- a/frontend/discourse/app/components/topic-list/featured-topic.gjs+++ b/frontend/discourse/app/components/topic-list/featured-topic.gjs@@ -1,6 +1,8 @@import { trustHTML } from "@ember/template";+import PluginOutlet from "discourse/components/plugin-outlet";import TopicPostBadges from "discourse/components/topic-post-badges";import TopicStatus from "discourse/components/topic-status";+import lazyHash from "discourse/helpers/lazy-hash";import dAgeWithTooltip from "discourse/ui-kit/helpers/d-age-with-tooltip";const FeaturedTopic = <template>@@ -11,6 +13,11 @@ const FeaturedTopic = <template>@topic.fancyTitle}}</a>+ <PluginOutlet+ @name="topic-list-after-title"+ @outletArgs={{lazyHash topic=@topic}}+ />+<TopicPostBadges@unreadPosts={{@topic.unread_posts}}@unseen={{@topic.unseen}}diff --git a/frontend/discourse/app/components/topic-list/latest-topic-list-item.gjs b/frontend/discourse/app/components/topic-list/latest-topic-list-item.gjsindex 4ae614f98ff..43c8fa6c853 100644--- a/frontend/discourse/app/components/topic-list/latest-topic-list-item.gjs+++ b/frontend/discourse/app/components/topic-list/latest-topic-list-item.gjs@@ -76,6 +76,11 @@ export default class LatestTopicListItem extends Component {{{~#if @topic.featured_link}} {{topicFeaturedLink @topic}}{{/if~}}+ <PluginOutlet+ @name="topic-list-after-title"+ @outletArgs={{lazyHash topic=@topic}}+ />+ {{~! no whitespace ~}}<TopicPostBadges@unreadPosts={{@topic.unread_posts}}@unseen={{@topic.unseen}}diff --git a/frontend/discourse/tests/integration/components/latest-topic-list-item-test.gjs b/frontend/discourse/tests/integration/components/latest-topic-list-item-test.gjsindex 719718e79d1..e41c96e5d9e 100644--- a/frontend/discourse/tests/integration/components/latest-topic-list-item-test.gjs+++ b/frontend/discourse/tests/integration/components/latest-topic-list-item-test.gjs@@ -7,6 +7,19 @@ import { setupRenderingTest } from "discourse/tests/helpers/component-test";module("Integration | Component | LatestTopicListItem", function (hooks) {setupRenderingTest(hooks);+ hooks.beforeEach(function () {+ withPluginApi((api) => {+ api.renderInOutlet(+ "topic-list-after-title",+ <template>+ {{#if @outletArgs.topic.event_starts_at}}+ <span class="event-date test-badge">in 14 hours</span>+ {{/if}}+ </template>+ );+ });+ });+test("latest-topic-list-item-class value transformer", async function (assert) {withPluginApi((api) => {api.registerValueTransformer(@@ -35,4 +48,19 @@ module("Integration | Component | LatestTopicListItem", function (hooks) {.dom(".latest-topic-list-item[data-topic-id='1235']").doesNotHaveClass("bar");});++ test("renders the topic-list-after-title connector (event badge) for event topics", async function (assert) {+ const store = this.owner.lookup("service:store");+ const topic = store.createRecord("topic", {+ id: 1234,+ title: "My event topic",+ event_starts_at: "2026-06-01T10:00:00.000Z",+ });++ await render(<template><LatestTopicListItem @topic={{topic}} /></template>);++ assert+ .dom(".latest-topic-list-item .event-date.test-badge")+ .exists("renders the event badge after the topic title");+ });});diff --git a/frontend/discourse/tests/integration/components/topic-list/featured-topic-test.gjs b/frontend/discourse/tests/integration/components/topic-list/featured-topic-test.gjsnew file mode 100644index 00000000000..608d7a5b781--- /dev/null+++ b/frontend/discourse/tests/integration/components/topic-list/featured-topic-test.gjs@@ -0,0 +1,36 @@+import { render } from "@ember/test-helpers";+import { module, test } from "qunit";+import FeaturedTopic from "discourse/components/topic-list/featured-topic";+import { withPluginApi } from "discourse/lib/plugin-api";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";++const EventBadgeStub = <template>+ {{#if @outletArgs.topic.event_starts_at}}+ <span class="event-date test-badge">in 14 hours</span>+ {{/if}}+</template>;++module("Integration | Component | FeaturedTopic", function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ withPluginApi((api) => {+ api.renderInOutlet("topic-list-after-title", EventBadgeStub);+ });+ });++ test("renders the topic-list-after-title connector (event badge) for event topics", async function (assert) {+ const store = this.owner.lookup("service:store");+ const topic = store.createRecord("topic", {+ id: 1234,+ title: "My event topic",+ event_starts_at: "2026-06-01T10:00:00.000Z",+ });++ await render(<template><FeaturedTopic @topic={{topic}} /></template>);++ assert+ .dom(".featured-topic .event-date.test-badge")+ .exists("renders the event badge after the topic title");+ });+});diff --git a/plugins/discourse-events/plugin.rb b/plugins/discourse-events/plugin.rbindex 3ba561ea16d..7febc3f9e84 100644--- a/plugins/discourse-events/plugin.rb+++ b/plugins/discourse-events/plugin.rb@@ -479,6 +479,7 @@ after_initialize doendadd_preloaded_topic_list_custom_field DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT+ CategoryList.preloaded_topic_custom_fields << DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_ATadd_to_serializer(:topic_view,@@ -501,15 +502,18 @@ after_initialize doendadd_to_serializer(- :topic_list_item,+ :listable_topic,:event_starts_at,include_condition: -> doSiteSetting.discourse_post_event_enabled &&- SiteSetting.display_post_event_date_on_topic_title && object.event_starts_at+ SiteSetting.display_post_event_date_on_topic_title &&+ object.custom_field_preloaded?(DiscourseEvents::Events::TOPIC_POST_EVENT_STARTS_AT) &&+ object.event_starts_atend,) { object.event_starts_at }add_preloaded_topic_list_custom_field DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT+ CategoryList.preloaded_topic_custom_fields << DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_ATadd_to_serializer(:topic_view,@@ -530,15 +534,18 @@ after_initialize doendadd_to_serializer(- :topic_list_item,+ :listable_topic,:event_ends_at,include_condition: -> doSiteSetting.discourse_post_event_enabled &&- SiteSetting.display_post_event_date_on_topic_title && object.event_ends_at+ SiteSetting.display_post_event_date_on_topic_title &&+ object.custom_field_preloaded?(DiscourseEvents::Events::TOPIC_POST_EVENT_ENDS_AT) &&+ object.event_ends_atend,) { object.event_ends_at }add_preloaded_topic_list_custom_field DiscourseEvents::Events::TOPIC_POST_EVENT_ALL_DAY+ CategoryList.preloaded_topic_custom_fields << DiscourseEvents::Events::TOPIC_POST_EVENT_ALL_DAYadd_to_serializer(:topic_view,@@ -559,11 +566,13 @@ after_initialize doendadd_to_serializer(- :topic_list_item,+ :listable_topic,:event_all_day,include_condition: -> doSiteSetting.discourse_post_event_enabled &&- SiteSetting.display_post_event_date_on_topic_title && object.event_all_day+ SiteSetting.display_post_event_date_on_topic_title &&+ object.custom_field_preloaded?(DiscourseEvents::Events::TOPIC_POST_EVENT_ALL_DAY) &&+ object.event_all_dayend,) { object.event_all_day }diff --git a/plugins/discourse-events/spec/requests/categories_controller_spec.rb b/plugins/discourse-events/spec/requests/categories_controller_spec.rbindex 7d6a974ab70..9e90932b3d4 100644--- a/plugins/discourse-events/spec/requests/categories_controller_spec.rb+++ b/plugins/discourse-events/spec/requests/categories_controller_spec.rb@@ -14,6 +14,34 @@ describe CategoriesController dosign_in(admin)end+ describe "#index" do+ it "includes event dates on featured topics" do+ freeze_time(Time.utc(2020, 4, 24, 14, 10))+ Jobs.run_immediately!+ topic = Fabricate(:topic, category:)+ post = Fabricate(:post, topic:)+ DiscourseEvents::Events::Event.create!(+ id: post.id,+ original_starts_at: 1.hour.from_now,+ original_ends_at: 2.hours.from_now,+ )+ CategoryFeaturedTopic.feature_topics++ get "/categories.json?include_topics=true"++ expect(response.status).to eq(200)+ categories = response.parsed_body["category_list"]["categories"]+ serialized_category = categories.find { |serialized| serialized["id"] == category.id }+ expect(serialized_category["topics"]).to include(+ a_hash_including(+ "id" => topic.id,+ "event_starts_at" => "2020-04-24T15:10:00.000Z",+ "event_ends_at" => "2020-04-24T16:10:00.000Z",+ ),+ )+ end+ end+describe "#update" doit "persists category_type_settings into calendar_categories" doput "/categories/#{category.id}.json",diff --git a/plugins/discourse-events/spec/serializers/topic_list_item_serializer_spec.rb b/plugins/discourse-events/spec/serializers/topic_list_item_serializer_spec.rbindex 3ec6ee3edae..857a708cdb5 100644--- a/plugins/discourse-events/spec/serializers/topic_list_item_serializer_spec.rb+++ b/plugins/discourse-events/spec/serializers/topic_list_item_serializer_spec.rb@@ -1,7 +1,10 @@# frozen_string_literal: trueRSpec.describe TopicListItemSerializer do- subject(:serializer) { described_class.new(topic, scope: Guardian.new, root: false) }+ subject(:serializer) do+ Topic.preload_custom_fields([topic], TopicList.preloaded_custom_fields)+ described_class.new(topic, scope: Guardian.new, root: false)+ endlet(:topic) { Fabricate(:topic) }let(:first_post) { Fabricate(:post, topic:) }@@ -19,6 +22,12 @@ RSpec.describe TopicListItemSerializer do)end+ it "omits event fields when the topic's custom fields were not preloaded" do+ json = JSON.parse(described_class.new(topic, scope: Guardian.new, root: false).to_json)++ expect(json.keys).not_to include("event_starts_at", "event_ends_at")+ end+describe "#event_starts_at" doit "returns the start time of the event in proper format" doexpect(parsed_json["event_starts_at"]).to eq("2020-04-24T15:10:00.000Z")@@ -60,6 +69,7 @@ RSpec.describe TopicListItemSerializer doall_day: true,)+ Topic.preload_custom_fields([all_day_topic], TopicList.preloaded_custom_fields)all_day_serializer = described_class.new(all_day_topic, scope: Guardian.new, root: false)all_day_json = JSON.parse(all_day_serializer.to_json)