FIX: slow mode dialog doesn't remember Enabled Until value (#13076)

FIX: slow mode dialog doesn’t remember Enabled Until value (#13076)

If reload a page after enabling slow mode and open the slow mode dialog again it would show a slow mode interval but wouldn’t show Enabled Until value. This PR fixes it.

diff --git a/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js b/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js
index 66a0a0a..f06e9ce 100644
--- a/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js
+++ b/app/assets/javascripts/discourse/app/controllers/edit-slow-mode.js
@@ -14,7 +14,6 @@ export default Controller.extend(ModalFunctionality, {
   minutes: null,
   seconds: null,
   saveDisabled: false,
-  enabledUntil: null,
   showCustomSelect: equal("selectedSlowMode", "custom"),
   durationIsSet: or("hours", "minutes", "seconds"),
 
@@ -87,7 +86,11 @@ export default Controller.extend(ModalFunctionality, {
     }
   },
 
-  @discourseComputed("saveDisabled", "durationIsSet", "enabledUntil")
+  @discourseComputed(
+    "saveDisabled",
+    "durationIsSet",
+    "model.slow_mode_enabled_until"
+  )
   submitDisabled(saveDisabled, durationIsSet, enabledUntil) {
     return saveDisabled || !durationIsSet || !enabledUntil;
   },
@@ -121,7 +124,11 @@ export default Controller.extend(ModalFunctionality, {
       this._parseValue(this.seconds)
     );
 
-    Topic.setSlowMode(this.model.id, seconds, this.enabledUntil)
+    Topic.setSlowMode(
+      this.model.id,
+      seconds,
+      this.model.slow_mode_enabled_until
+    )
       .catch(popupAjaxError)
       .then(() => {
         this.set("model.slow_mode_seconds", seconds);
diff --git a/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs b/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs
index 19f54b2..8b6dbae 100644
--- a/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs
+++ b/app/assets/javascripts/discourse/app/templates/modal/edit-slow-mode.hbs
@@ -33,8 +33,8 @@
       labelClasses="slow-mode-label"
       includeFarFuture=false
       clearable=true
-      input=enabledUntil
-      onChangeInput=(action (mut enabledUntil))
+      input=model.slow_mode_enabled_until
+      onChangeInput=(action (mut model.slow_mode_enabled_until))
     }}
   </div>
 {{/d-modal-body}}
diff --git a/app/assets/javascripts/discourse/tests/acceptance/topic-slow-mode-test.js b/app/assets/javascripts/discourse/tests/acceptance/topic-slow-mode-test.js
new file mode 100644
index 0000000..3324d7b
--- /dev/null
+++ b/app/assets/javascripts/discourse/tests/acceptance/topic-slow-mode-test.js
@@ -0,0 +1,58 @@
+import {
+  acceptance,
+  exists,
+  query,
+  updateCurrentUser,
+} from "discourse/tests/helpers/qunit-helpers";
+import { click, visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import I18n from "I18n";
+import { cloneJSON } from "discourse-common/lib/object";
+import topicFixtures from "discourse/tests/fixtures/topic";
+
+acceptance("Topic - Slow Mode - enabled", function (needs) {
+  needs.user();
+  needs.pretender((server, helper) => {
+    server.get("/t/130.json", () => {
+      const json = cloneJSON(topicFixtures["/t/130.json"]);
+      json.slow_mode_seconds = 600;
+      json.slow_mode_enabled_until = "2040-01-01T04:00:00.000Z";
+
+      return helper.response(json);
+    });
+  });
+
+  test("the slow mode dialog loads settings of currently enabled slow mode ", async function (assert) {
+    updateCurrentUser({ moderator: true });
+    await visit("/t/slow-mode-testing/130");
+    await click(".toggle-admin-menu");
+    await click(".topic-admin-slow-mode button");
+
+    await click(".future-date-input-selector-header");
+
+    assert.equal(
+      query(".future-date-input-selector-header").getAttribute("aria-expanded"),
+      "true",
+      "selector is expanded"
+    );
+
+    assert.equal(
+      query("div.slow-mode-type span.name").innerText,
+      I18n.t("topic.slow_mode_update.durations.10_minutes"),
+      "slow mode interval is rendered"
+    );
+
+    // unfortunately we can't check exact date and time
+    // but at least we can make sure that components for choosing date and time are rendered
+    // (in case of inactive slow mode it would be only a combo box with text "Select a timeframe",
+    // and date picker and time picker wouldn't be rendered)
+    assert.equal(
+      query("div.enabled-until span.name").innerText,
+      I18n.t("topic.auto_update_input.pick_date_and_time"),
+      "enabled until combobox is switched to the option Pick Date and Time"
+    );
+
+    assert.ok(exists("input.date-picker"), "date picker is rendered");
+    assert.ok(exists("input.time-input"), "time picker is rendered");
+  });
+});
diff --git a/app/models/topic.rb b/app/models/topic.rb
index 3d82dec..35ca0a1 100644
--- a/app/models/topic.rb
+++ b/app/models/topic.rb
@@ -538,6 +538,7 @@ class Topic < ActiveRecord::Base
   def reload(options = nil)
     @post_numbers = nil
     @public_topic_timer = nil
+    @slow_mode_topic_timer = nil
     @is_category_topic = nil
     super(options)
   end
@@ -1278,6 +1279,10 @@ class Topic < ActiveRecord::Base
     @public_topic_timer ||= topic_timers.find_by(deleted_at: nil, public_type: true)
   end
 
+  def slow_mode_topic_timer
+    @slow_mode_topic_timer ||= topic_timers.find_by(deleted_at: nil, status_type: TopicTimer.types[:clear_slow_mode])
+  end
+
   def delete_topic_timer(status_type, by_user: Discourse.system_user)
     options = { status_type: status_type }
     options.merge!(user: by_user) unless TopicTimer.public_types[status_type]
diff --git a/app/serializers/topic_view_serializer.rb b/app/serializers/topic_view_serializer.rb
index da76ec2..3a5e1d7 100644
--- a/app/serializers/topic_view_serializer.rb
+++ b/app/serializers/topic_view_serializer.rb
@@ -75,7 +75,8 @@ class TopicViewSerializer < ApplicationSerializer
     :requested_group_name,
     :thumbnails,
     :user_last_posted_at,
-    :is_shared_draft
+    :is_shared_draft,
+    :slow_mode_enabled_until
   )
 
   has_one :details, serializer: TopicViewDetailsSerializer, root: false, embed: :objects
@@ -298,4 +299,8 @@ class TopicViewSerializer < ApplicationSerializer
   def include_user_last_posted_at?
     has_topic_user? && object.topic.slow_mode_seconds.to_i > 0
   end
+
+  def slow_mode_enabled_until
+    object.topic.slow_mode_topic_timer&.execute_at
+  end
 end
diff --git a/app/serializers/web_hook_topic_view_serializer.rb b/app/serializers/web_hook_topic_view_serializer.rb
index cadd50e..13159fd 100644
--- a/app/serializers/web_hook_topic_view_serializer.rb
+++ b/app/serializers/web_hook_topic_view_serializer.rb
@@ -21,6 +21,7 @@ class WebHookTopicViewSerializer < TopicViewSerializer
     details
     image_url
     slow_mode_seconds
+    slow_mode_enabled_until
   }.each do |attr|
     define_method("include_#{attr}?") do
       false

GitHub sha: 0df6b0bc

This commit appears in #13076 which was approved by riking. It was merged by AndrewPrigorshnev.