minor refactorings

minor refactorings

diff --git a/assets/javascripts/discourse/components/events-calendar.js.es6 b/assets/javascripts/discourse/components/events-calendar.js.es6
index 43dab75..abd053f 100644
--- a/assets/javascripts/discourse/components/events-calendar.js.es6
+++ b/assets/javascripts/discourse/components/events-calendar.js.es6
@@ -38,17 +38,13 @@ export default Component.extend({
     this._super(...arguments);
 
     (this.events || []).forEach(event => {
+      const { starts_at, ends_at, post } = event;
       this._calendar.addEvent({
         title: formatEventName(event),
-        start: event.starts_at,
-        end: event.ends_at || event.starts_at,
-        allDay: !isNotFullDayEvent(
-          moment(event.starts_at),
-          moment(event.ends_at)
-        ),
-        url: Discourse.getURL(
-          `/t/-/${event.post.topic.id}/${event.post.post_number}`
-        )
+        start: starts_at,
+        end: ends_at || starts_at,
+        allDay: !isNotFullDayEvent(moment(starts_at), moment(ends_at)),
+        url: Discourse.getURL(`/t/-/${post.topic.id}/${post.post_number}`)
       });
     });
     this._calendar.render();
diff --git a/assets/javascripts/initializers/discourse-calendar.js.es6 b/assets/javascripts/initializers/discourse-calendar.js.es6
index 782ede8..d000957 100644
--- a/assets/javascripts/initializers/discourse-calendar.js.es6
+++ b/assets/javascripts/initializers/discourse-calendar.js.es6
@@ -1,6 +1,6 @@
+import { Promise } from "rsvp";
 import { isPresent } from "@ember/utils";
 import DiscourseURL from "discourse/lib/url";
-import { Promise } from "rsvp";
 import { cookAsync } from "discourse/lib/text";
 import { escapeExpression } from "discourse/lib/utilities";
 import loadScript from "discourse/lib/load-script";
@@ -25,6 +25,14 @@ function stringToHexColor(str) {
   return hex;
 }
 
+function loadFullCalendar() {
+  return new Promise(resolve =>
+    loadScript(
+      "/plugins/discourse-calendar/javascripts/fullcalendar-with-moment-timezone.min.js"
+    ).then(resolve)
+  );
+}
+
 function initializeDiscourseCalendar(api) {
   let _topicController;
   const outletName = Discourse.SiteSettings.calendar_categories_outlet;
@@ -71,9 +79,7 @@ function initializeDiscourseCalendar(api) {
           '<div class="calendar"><div class="spinner medium"></div></div>'
         );
         $calendarContainer.html($spinner);
-        loadScript(
-          "/plugins/discourse-calendar/javascripts/fullcalendar-with-moment-timezone.min.js"
-        ).then(() => {
+        loadFullCalendar().then(() => {
           const options = [`postId=${postId}`];
 
           const optionals = ["weekends", "tzPicker", "defaultView"];
@@ -114,11 +120,9 @@ function initializeDiscourseCalendar(api) {
     const $calendar = $op.find(".calendar").first();
 
     if (post && $calendar.length > 0) {
-      ajax(`/posts/${post.id}.json`).then(post => {
-        loadScript(
-          "/plugins/discourse-calendar/javascripts/fullcalendar-with-moment-timezone.min.js"
-        ).then(() => render($calendar, post));
-      });
+      ajax(`/posts/${post.id}.json`).then(post =>
+        loadFullCalendar().then(() => render($calendar, post))
+      );
     }
   });
 
@@ -154,9 +158,7 @@ function initializeDiscourseCalendar(api) {
       return;
     }
 
-    loadScript(
-      "/plugins/discourse-calendar/javascripts/fullcalendar-with-moment-timezone.min.js"
-    ).then(() => render($calendar, helper.getModel()));
+    loadFullCalendar().then(() => render($calendar, helper.getModel()));
   }
 
   function _buildCalendar($calendar, timeZone) {

GitHub sha: d38d3478

if loadScript returns a promise it shouldn’t be necessary to wrap this in a new promise right?

:man_facepalming:

I had a different design idea at the beginning, the one I applied to the next commit, and it seemed un-necessary in the end for this case, but I kept the promise. yes it’s totally not needed here.

1 Like