FIX: prevents timezone to show duplicated zones in preview

FIX: prevents timezone to show duplicated zones in preview

It could happen if you have Etc/UTC in default settings, and timezone=“UTC” on date, the equality check is now more strict.

diff --git a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js.no-module.es6 b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js.no-module.es6
index 3be2593ce2..d9c9f138e0 100644
--- a/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js.no-module.es6
+++ b/plugins/discourse-local-dates/assets/javascripts/discourse-local-dates.js.no-module.es6
@@ -125,6 +125,10 @@
   }
 
   function _isEqualZones(timezoneA, timezoneB) {
+    if (timezoneA.includes(timezoneB) || timezoneB.includes(timezoneA)) {
+      return true;
+    }
+
     return (
       moment.tz(timezoneA).utcOffset() === moment.tz(timezoneB).utcOffset()
     );
@@ -222,7 +226,9 @@
     const previewedTimezones = [];
     const watchingUserTimezone = moment.tz.guess();
     const timezones = options.timezones.filter(
-      timezone => timezone !== watchingUserTimezone
+      timezone =>
+        !_isEqualZones(timezone, watchingUserTimezone) &&
+        !_isEqualZones(timezone, options.timezone)
     );
 
     previewedTimezones.push({
@@ -244,26 +250,24 @@
       timezones.unshift(options.timezone);
     }
 
-    timezones
-      .filter(z => z)
-      .forEach(timezone => {
-        if (_isEqualZones(timezone, displayedTimezone)) {
-          return;
-        }
+    timezones.filter(Boolean).forEach(timezone => {
+      if (_isEqualZones(timezone, displayedTimezone)) {
+        return;
+      }
 
-        if (_isEqualZones(timezone, watchingUserTimezone)) {
-          timezone = watchingUserTimezone;
-        }
+      if (_isEqualZones(timezone, watchingUserTimezone)) {
+        timezone = watchingUserTimezone;
+      }
 
-        previewedTimezones.push({
-          timezone,
-          dateTime: options.time
-            ? moment(dateTime)
-                .tz(timezone)
-                .format("LLL")
-            : _createDateTimeRange(dateTime, timezone)
-        });
+      previewedTimezones.push({
+        timezone,
+        dateTime: options.time
+          ? moment(dateTime)
+              .tz(timezone)
+              .format("LLL")
+          : _createDateTimeRange(dateTime, timezone)
       });
+    });
 
     if (!previewedTimezones.length) {
       previewedTimezones.push({

GitHub sha: a40bc1e9

1 Like

@jjaffeux I got a Uncaught TypeError: Cannot read property 'includes' of undefined there.

at b (discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:109)
    at discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:32
    at Array.filter (<anonymous>)
    at discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:31
    at v (discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:52)
    at HTMLSpanElement.<anonymous> (discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:128)
    at Function.each (ember_jquery-c2cca4a19fd4c70eabc95b010a6d7643955ec2a2251aeb1d70f625773eebea21.br.js:1)
    at C.fn.init.each (ember_jquery-c2cca4a19fd4c70eabc95b010a6d7643955ec2a2251aeb1d70f625773eebea21.br.js:1)
    at C.fn.init.u.fn.applyLocalDates (discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:116)
    at e.decorateCooked.id (discourse-local-dates-37e5f3609a569713e4eb86a5aa84b9c092f0caad56d8e9f38fafff1fdd0c43ee.br.js:formatted:218)
2 Likes

oh didnt think it could be undefined thx will fix!

2 Likes

That should fix it:

@ZogStriP I will probably refactor more this code next week, I think it has grown to complicated over time.

2 Likes

Yup, that did the trick! :slightly_smiling_face: Thanks!

1 Like