Skip to content

Commit

Permalink
Only show events of current category
Browse files Browse the repository at this point in the history
  • Loading branch information
ledadu committed Dec 26, 2021
1 parent 3ea9fb0 commit ae49633
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 12 deletions.
12 changes: 1 addition & 11 deletions javascripts/discourse/initializers/layouts-event-list.js.es6
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ajax } from "discourse/lib/ajax";

export default {
name: "layouts-event-list",
Expand All @@ -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({});
},
};
33 changes: 32 additions & 1 deletion javascripts/discourse/widgets/layouts-event-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit ae49633

Please sign in to comment.