From ae49633abe9b00ae7f9e5bd6791c321f76a61fb1 Mon Sep 17 00:00:00 2001 From: Lenny Dadu Date: Sun, 26 Dec 2021 18:14:55 +0000 Subject: [PATCH] Only show events of current category --- .../initializers/layouts-event-list.js.es6 | 12 +------ .../discourse/widgets/layouts-event-list.js | 33 ++++++++++++++++++- 2 files changed, 33 insertions(+), 12 deletions(-) diff --git a/javascripts/discourse/initializers/layouts-event-list.js.es6 b/javascripts/discourse/initializers/layouts-event-list.js.es6 index 1edd474..3fe9e1f 100644 --- a/javascripts/discourse/initializers/layouts-event-list.js.es6 +++ b/javascripts/discourse/initializers/layouts-event-list.js.es6 @@ -1,4 +1,3 @@ -import { ajax } from "discourse/lib/ajax"; export default { name: "layouts-event-list", @@ -20,15 +19,6 @@ export default { if (layoutsError || !siteSettings.calendar_enabled) return; - const now = new Date(); - ajax( - `/discourse-post-event/events.json?after=${now.toISOString()}` - ).then((eventList) => { - const events = eventList.events; - const props = { - events, - }; - layouts.addSidebarProps(props); - }); + layouts.addSidebarProps({}); }, }; diff --git a/javascripts/discourse/widgets/layouts-event-list.js b/javascripts/discourse/widgets/layouts-event-list.js index 9fa6521..efc8a87 100644 --- a/javascripts/discourse/widgets/layouts-event-list.js +++ b/javascripts/discourse/widgets/layouts-event-list.js @@ -4,6 +4,7 @@ import { createWidget } from "discourse/widgets/widget"; import { h } from "virtual-dom"; const { iconNode } = require("discourse-common/lib/icon-library"); const { iconHTML } = require("discourse-common/lib/icon-library"); +import { ajax } from "discourse/lib/ajax"; let layouts; @@ -32,8 +33,38 @@ function htmlDecode(input) { } export default layouts.createLayoutsWidget("event-list", { + + defaultState(attrs) { + return { + categoryId: false, // Is not set to make a turn by didRenderWidget + events : [], + }; + }, + + didRenderWidget() { + var self = this; + const { attrs } = this; + const { category } = attrs; + const categoryId = category?.id || null; + + if ( + category === false + || categoryId !== this.state.categoryId + ) { + const now = new Date(); + const categoryParam = category?.id ? `&category_id=${category.id}&include_subcategories=true` : ''; + const eventQuery = `/discourse-post-event/events.json?after=${now.toISOString()}${categoryParam}`; + + ajax(eventQuery).then((eventList) => { + self.state.events = eventList.events; + self.state.categoryId = category?.id || null; + self.scheduleRerender(); + }); + } + }, + html(attrs) { - const { events } = attrs; + const { events } = this.state; if (events == null || events == undefined) return;