A platform for community discussion. Free, open, simple.
FIX: Keep event calendars visible when filtering topics
Jake Goldsborough547e6fb1c92f
diff --git a/plugins/discourse-events/app/controllers/discourse_events/events_controller.rb b/plugins/discourse-events/app/controllers/discourse_events/events_controller.rbindex 95177b78f14..03ca2279902 100644--- a/plugins/discourse-events/app/controllers/discourse_events/events_controller.rb+++ b/plugins/discourse-events/app/controllers/discourse_events/events_controller.rb@@ -198,6 +198,7 @@ module DiscourseEvents:after,:order,:tags,+ :no_tags,:search,:status,tags: [],diff --git a/plugins/discourse-events/assets/javascripts/discourse/components/category-calendar.gjs b/plugins/discourse-events/assets/javascripts/discourse/components/category-calendar.gjsindex 417964e86d6..866db58bc4f 100644--- a/plugins/discourse-events/assets/javascripts/discourse/components/category-calendar.gjs+++ b/plugins/discourse-events/assets/javascripts/discourse/components/category-calendar.gjs@@ -3,7 +3,6 @@ import { action } from "@ember/object";import { service } from "@ember/service";import { popupAjaxError } from "discourse/lib/ajax-error";import { bind } from "discourse/lib/decorators";-import Category from "discourse/models/category";import formatEventForCalendar from "../lib/format-event-for-calendar";import openEventComposer from "../lib/open-event-composer";import FullCalendar from "./full-calendar";@@ -32,7 +31,12 @@ export default class CategoryCalendar extends Component {}get refreshKey() {- return `${this.category.id}-${this.includeSubcategories}`;+ return JSON.stringify([+ this.category.id,+ this.includeSubcategories,+ this.tags,+ this.noTags,+ ]);}get shouldRender() {@@ -44,10 +48,6 @@ export default class CategoryCalendar extends Component {return false;}- if (!this.router.currentRoute?.params?.category_slug_path_with_id) {- return false;- }-if (!this.category) {return false;}@@ -60,34 +60,44 @@ export default class CategoryCalendar extends Component {}get validCategory() {- if (- !this.categorySetting &&- !this.siteSettings.events_calendar_categories- ) {- return false;- }-- return (- this.categorySetting?.categoryId === this.category.id.toString() ||- this.siteSettings.events_calendar_categories- .split("|")- .filter(Boolean)- .includes(this.category.id.toString())- );+ return !!this.calendarCategory;}get category() {- return Category.findBySlugPathWithID(- this.router.currentRoute.params.category_slug_path_with_id- );+ return this.router.currentRoute?.attributes?.category;+ }++ get tags() {+ const { tag, additionalTags = [] } =+ this.router.currentRoute?.attributes || {};+ return tag && !this.noTags ? [tag.name, ...additionalTags] : [];+ }++ get noTags() {+ const tag = this.router.currentRoute?.attributes?.tag;+ return tag?.slug === "none" && !tag.id;+ }++ get calendarCategory() {+ const categoryIds = [+ ...this.siteSettings.events_calendar_categories.split("|"),+ ...this.categorySettings.map((item) => item.categoryId),+ ];+ let category = this.category;+ while (category) {+ if (categoryIds.includes(category.id.toString())) {+ return category;+ }+ category = category.parentCategory;+ }}get renderWeekends() {return this.categorySetting?.weekends !== "false";}- get categorySetting() {- const settings = this.siteSettings.calendar_categories+ get categorySettings() {+ return this.siteSettings.calendar_categories.split("|").filter(Boolean).map((stringSetting) => {@@ -101,9 +111,11 @@ export default class CategoryCalendar extends Component {});return data;});+ }- return settings.find(- (item) => item.categoryId === this.category.id.toString()+ get categorySetting() {+ return this.categorySettings.find(+ (item) => item.categoryId === this.calendarCategory?.id.toString());}@@ -132,6 +144,12 @@ export default class CategoryCalendar extends Component {params.include_subcategories = true;}+ if (this.noTags) {+ params.no_tags = true;+ } else if (this.tags.length) {+ params.tags = this.tags;+ }+const events = await this.discoursePostEventService.fetchEvents(params);return this.formattedEvents(events);} catch (error) {diff --git a/plugins/discourse-events/assets/javascripts/discourse/connectors/discovery-list-container-top/category-events-calendar.gjs b/plugins/discourse-events/assets/javascripts/discourse/connectors/discovery-list-container-top/category-events-calendar.gjsindex f66e7fc6bc0..45fd0a513be 100644--- a/plugins/discourse-events/assets/javascripts/discourse/connectors/discovery-list-container-top/category-events-calendar.gjs+++ b/plugins/discourse-events/assets/javascripts/discourse/connectors/discovery-list-container-top/category-events-calendar.gjs@@ -1,18 +1,32 @@import Component from "@glimmer/component";+import { service } from "@ember/service";import CategoryCalendar from "../../components/category-calendar";export default class CategoryEventsCalendar extends Component {static shouldRender(_, ctx) {+ return ["discovery-list-container-top", "before-topic-list-body"].includes(+ ctx.siteSettings.calendar_categories_outlet+ );+ }++ @service router;++ @service siteSettings;++ get showCalendar() {return (- ctx.siteSettings.calendar_categories_outlet ===- "discovery-list-container-top"+ this.siteSettings.calendar_categories_outlet ===+ "discovery-list-container-top" ||+ !this.router.currentRoute?.attributes?.list?.topics?.length);}<template>- <div- class="--discovery-list-container-top"- id="category-events-calendar"- ><CategoryCalendar /></div>+ {{#if this.showCalendar}}+ <div+ class="--discovery-list-container-top"+ id="category-events-calendar"+ ><CategoryCalendar /></div>+ {{/if}}</template>}diff --git a/plugins/discourse-events/lib/discourse_events/events/finder.rb b/plugins/discourse-events/lib/discourse_events/events/finder.rbindex 7b527b2adb7..bb965663361 100644--- a/plugins/discourse-events/lib/discourse_events/events/finder.rb+++ b/plugins/discourse-events/lib/discourse_events/events/finder.rb@@ -208,6 +208,10 @@ module DiscourseEventsenddef self.filter_by_tags(events, params, guardian)+ if ActiveModel::Type::Boolean.new.cast(params[:no_tags])+ return events.where.not(topics: { id: TopicTag.select(:topic_id) })+ end+tag_names =Array(params[:tags]).flat_map { |tag| tag.to_s.split(",") }diff --git a/plugins/discourse-events/spec/requests/events_controller_spec.rb b/plugins/discourse-events/spec/requests/events_controller_spec.rbindex 0e6ef97da3e..5fb11a013ce 100644--- a/plugins/discourse-events/spec/requests/events_controller_spec.rb+++ b/plugins/discourse-events/spec/requests/events_controller_spec.rb@@ -160,6 +160,36 @@ module DiscourseEvents::Eventsexpect(response.parsed_body["events"].pluck("id")).to contain_exactly(private_event.id)end+ it "filters untagged events within the selected category" do+ category = Fabricate(:category)+ untagged =+ Fabricate(:event, post: Fabricate(:post, topic: Fabricate(:topic, category: category)))+ tagged =+ Fabricate(+ :event,+ post:+ Fabricate(+ :post,+ topic: Fabricate(:topic, category: category, tags: [Fabricate(:tag)]),+ ),+ )+ Fabricate(:event)++ get "/discourse-post-event/events.json", params: { category_id: category.id, no_tags: true }+ expect(response.status).to eq(200)+ expect(response.parsed_body["events"].pluck("id")).to contain_exactly(untagged.id)++ get "/discourse-post-event/events.json",+ params: {+ category_id: category.id,+ no_tags: false,+ }+ expect(response.parsed_body["events"].pluck("id")).to contain_exactly(+ untagged.id,+ tagged.id,+ )+ end+it "returns events in ICS format" doevent1 = Fabricate(:event, original_starts_at: 1.day.from_now, name: "Test Event 1")event2 = Fabricate(:event, original_starts_at: 2.days.from_now, name: "Test Event 2")diff --git a/plugins/discourse-events/test/javascripts/acceptance/category-calendar-subcategories-test.js b/plugins/discourse-events/test/javascripts/acceptance/category-calendar-subcategories-test.jsindex bef4ba6c516..a1da5e2bea5 100644--- a/plugins/discourse-events/test/javascripts/acceptance/category-calendar-subcategories-test.js+++ b/plugins/discourse-events/test/javascripts/acceptance/category-calendar-subcategories-test.js@@ -4,55 +4,135 @@ import { cloneJSON } from "discourse/lib/object";import { fixturesByUrl } from "discourse/tests/helpers/create-pretender";import { acceptance } from "discourse/tests/helpers/qunit-helpers";-acceptance("Category Events Calendar - subcategory filter", function (needs) {- needs.user();- needs.settings({- discourse_events_enabled: true,- discourse_post_event_enabled: true,- events_calendar_categories: "1",- calendar_categories: "",- });-- let lastEventsParams;-- needs.hooks.beforeEach(function () {- lastEventsParams = undefined;- });-- needs.pretender((server, helper) => {- server.get("/discourse-post-event/events", (request) => {- lastEventsParams = request.queryParams;- return helper.response({ events: [] });- });-- server.get("/c/bug/1/none/l/latest.json", () => {- return helper.response(- cloneJSON(fixturesByUrl["/c/bug/1/l/latest.json"])- );- });- });-- test("follows the category list's subcategory filter", async function (assert) {- await visit("/c/bug/1");-- assert.strictEqual(- lastEventsParams.include_subcategories,- "true",- "requests subcategory events when the list shows subcategory topics"- );-- lastEventsParams = undefined;- await visit("/c/bug/1/none");-- assert.strictEqual(- lastEventsParams?.category_id,- "1",- "refetches events when the subcategory filter changes"- );- assert.strictEqual(- lastEventsParams?.include_subcategories,- undefined,- "omits subcategory events when the list hides subcategory topics"- );- });-});+for (const outlet of [+ "discovery-list-container-top",+ "before-topic-list-body",+]) {+ acceptance(+ `Category Events Calendar - filters (${outlet})`,+ function (needs) {+ needs.user();+ needs.settings({+ discourse_events_enabled: true,+ discourse_post_event_enabled: true,+ events_calendar_categories: "1|2",+ tagging_enabled: true,+ calendar_categories_outlet: outlet,+ calendar_categories: "",+ });++ let lastEventsParams;++ needs.hooks.beforeEach(function () {+ lastEventsParams = undefined;+ });++ needs.pretender((server, helper) => {+ server.get("/tag/12/notifications.json", () =>+ helper.response({+ tag_notification: { id: 12, notification_level: 1 },+ })+ );++ server.get("/discourse-post-event/events", (request) => {+ lastEventsParams = request.queryParams;+ return helper.response({ events: [] });+ });++ [+ "/c/feature/2/l/latest.json",+ "/c/feature/spec/26/l/latest.json",+ "/tags/c/bug/1/planters/12/l/latest.json",+ "/tags/c/bug/1/none/planters/12/l/latest.json",+ "/tags/c/bug/1/none/l/latest.json",+ ].forEach((url) => {+ server.get(url, () =>+ helper.response({+ users: [],+ topic_list: {+ topics: [],+ tags: url.includes("/planters/")+ ? [{ id: 12, name: "planters", slug: "planters" }]+ : [],+ },+ })+ );+ });++ server.get("/c/bug/1/none/l/latest.json", () => {+ return helper.response(+ cloneJSON(fixturesByUrl["/c/bug/1/l/latest.json"])+ );+ });+ });++ test("inherits calendar settings when opening a subcategory directly", async function (assert) {+ this.siteSettings.events_calendar_categories = "";+ this.siteSettings.calendar_categories =+ "categoryId=2;defaultView=timeGridWeek;weekends=false";+ await visit("/c/feature/spec/26");+ assert.dom(".fc-timeGridWeek-view").exists();+ assert.dom(".fc-day-sat").doesNotExist();+ assert.strictEqual(lastEventsParams.category_id, "26");+ await visit("/c/bug/1");+ assert.dom(".fc").doesNotExist();+ });++ test("keeps the calendar when selecting and clearing a subcategory", async function (assert) {+ await visit("/c/feature/2");+ assert.dom(".fc").exists();+ await visit("/c/feature/spec/26");+ assert.dom(".fc").exists();+ assert.strictEqual(lastEventsParams.category_id, "26");+ await visit("/c/feature/2");+ assert.dom(".fc").exists();+ assert.strictEqual(lastEventsParams.category_id, "2");+ });++ test("keeps the calendar when selecting and clearing tags", async function (assert) {+ await visit("/c/bug/1");+ await visit("/tags/c/bug/1/planters/12");+ assert.dom(".fc").exists();+ assert.deepEqual(lastEventsParams.tags, ["planters"]);+ assert.strictEqual(lastEventsParams.category_id, "1");+ await visit("/tags/c/bug/1/none/planters/12");+ assert.dom(".fc").exists();+ assert.deepEqual(lastEventsParams.tags, ["planters"]);+ assert.strictEqual(lastEventsParams.include_subcategories, undefined);+ await visit("/tags/c/bug/1/none");+ assert.dom(".fc").exists();+ assert.strictEqual(lastEventsParams.no_tags, "true");+ assert.strictEqual(lastEventsParams.tags, undefined);+ await visit("/c/bug/1");+ assert.dom(".fc").exists();+ assert.strictEqual(lastEventsParams.tags, undefined);+ assert.strictEqual(lastEventsParams.no_tags, undefined);+ assert.strictEqual(lastEventsParams.include_subcategories, "true");+ });++ test("follows the category list's subcategory filter", async function (assert) {+ await visit("/c/bug/1");++ assert.strictEqual(+ lastEventsParams.include_subcategories,+ "true",+ "requests subcategory events when the list shows subcategory topics"+ );++ lastEventsParams = undefined;+ await visit("/c/bug/1/none");++ assert.strictEqual(+ lastEventsParams?.category_id,+ "1",+ "refetches events when the subcategory filter changes"+ );+ assert.strictEqual(+ lastEventsParams?.include_subcategories,+ undefined,+ "omits subcategory events when the list hides subcategory topics"+ );+ });+ }+ );+}