FIX: save bookmark reminder on tap unless custom (#9611)

FIX: save bookmark reminder on tap unless custom (#9611)

diff --git a/app/assets/javascripts/discourse/app/controllers/bookmark.js b/app/assets/javascripts/discourse/app/controllers/bookmark.js
index 30dc412..cd071c6 100644
--- a/app/assets/javascripts/discourse/app/controllers/bookmark.js
+++ b/app/assets/javascripts/discourse/app/controllers/bookmark.js
@@ -1,4 +1,5 @@
 import { and } from "@ember/object/computed";
+import { action } from "@ember/object";
 import { isPresent } from "@ember/utils";
 import Controller from "@ember/controller";
 import { Promise } from "rsvp";
@@ -424,53 +425,60 @@ export default Controller.extend(ModalFunctionality, {
     }
   },
 
-  actions: {
-    saveAndClose() {
-      if (this._saving || this._deleting) {
-        return;
-      }
+  @action
+  saveAndClose() {
+    if (this._saving || this._deleting) {
+      return;
+    }
 
-      this._saving = true;
-      this._savingBookmarkManually = true;
-      this._saveBookmark()
-        .then(() => this.send("closeModal"))
-        .catch(e => this._handleSaveError(e))
-        .finally(() => (this._saving = false));
-    },
-
-    delete() {
-      this._deleting = true;
-      let deleteAction = () => {
-        this._closeWithoutSaving = true;
-        this._deleteBookmark()
-          .then(() => {
-            this._deleting = false;
-            this.send("closeModal");
-          })
-          .catch(e => this._handleSaveError(e));
-      };
-
-      if (this._existingBookmarkHasReminder()) {
-        bootbox.confirm(I18n.t("bookmarks.confirm_delete"), result => {
-          if (result) {
-            deleteAction();
-          }
-        });
-      } else {
-        deleteAction();
-      }
-    },
+    this._saving = true;
+    this._savingBookmarkManually = true;
+    return this._saveBookmark()
+      .then(() => this.send("closeModal"))
+      .catch(e => this._handleSaveError(e))
+      .finally(() => (this._saving = false));
+  },
 
-    closeWithoutSavingBookmark() {
+  @action
+  delete() {
+    this._deleting = true;
+    let deleteAction = () => {
       this._closeWithoutSaving = true;
-      this.send("closeModal");
-    },
+      this._deleteBookmark()
+        .then(() => {
+          this._deleting = false;
+          this.send("closeModal");
+        })
+        .catch(e => this._handleSaveError(e));
+    };
 
-    selectReminderType(type) {
-      if (type === REMINDER_TYPES.LATER_TODAY && !this.showLaterToday) {
-        return;
-      }
-      this.set("selectedReminderType", type);
+    if (this._existingBookmarkHasReminder()) {
+      bootbox.confirm(I18n.t("bookmarks.confirm_delete"), result => {
+        if (result) {
+          deleteAction();
+        }
+      });
+    } else {
+      deleteAction();
+    }
+  },
+
+  @action
+  closeWithoutSavingBookmark() {
+    this._closeWithoutSaving = true;
+    this.send("closeModal");
+  },
+
+  @action
+  selectReminderType(type) {
+    if (type === REMINDER_TYPES.LATER_TODAY && !this.showLaterToday) {
+      return;
+    }
+
+    this.set("selectedReminderType", type);
+
+    if (type !== REMINDER_TYPES.CUSTOM) {
+      return this.saveAndClose();
     }
   }
 });
diff --git a/test/javascripts/acceptance/bookmarks-test.js b/test/javascripts/acceptance/bookmarks-test.js
index 2c2008f..6361d43 100644
--- a/test/javascripts/acceptance/bookmarks-test.js
+++ b/test/javascripts/acceptance/bookmarks-test.js
@@ -1,14 +1,17 @@
 import { acceptance, loggedInUser } from "helpers/qunit-helpers";
 import pretender from "helpers/create-pretender";
 
-acceptance("Bookmarking", {
-  loggedIn: true,
+acceptance("Bookmarking", { loggedIn: true });
 
-  beforeEach() {}
-});
+function handleRequest(assert, request) {
+  const body = request.requestBody;
+  const reminderType = body
+    .substr(0, body.indexOf("&"))
+    .replace("reminder_type=", "");
+
+  assert.step(reminderType || "none");
 
-function mockSuccessfulBookmarkPost() {
-  pretender.post("/bookmarks", () => [
+  return [
     200,
     {
       "Content-Type": "application/json"
@@ -17,15 +20,24 @@ function mockSuccessfulBookmarkPost() {
       id: 999,
       success: "OK"
     }
-  ]);
+  ];
+}
+
+function mockSuccessfulBookmarkPost(assert) {
+  pretender.post("/bookmarks", request => handleRequest(assert, request));
+  pretender.put("/bookmarks/999", request => handleRequest(assert, request));
 }
 
 async function openBookmarkModal() {
-  await click(".topic-post:first-child button.show-more-actions");
-  return await click(".topic-post:first-child button.bookmark");
+  if (exists(".topic-post:first-child button.show-more-actions")) {
+    await click(".topic-post:first-child button.show-more-actions");
+  }
+
+  await click(".topic-post:first-child button.bookmark");
 }
+
 async function openEditBookmarkModal() {
-  return await click(".topic-post:first-child button.bookmarked");
+  await click(".topic-post:first-child button.bookmarked");
 }
 
 test("Bookmarks modal opening", async assert => {
@@ -35,32 +47,45 @@ test("Bookmarks modal opening", async assert => {
 });
 
 test("Bookmarks modal selecting reminder type", async assert => {
+  mockSuccessfulBookmarkPost(assert);
+
   await visit("/t/internationalization-localization/280");
+
   await openBookmarkModal();
   await click("#tap_tile_tomorrow");
-  assert.ok(exists("#tap_tile_tomorrow.active"), "it selects tomorrow");
+
+  await openBookmarkModal();
   await click("#tap_tile_start_of_next_business_week");
-  assert.ok(
-    exists("#tap_tile_start_of_next_business_week.active"),
-    "it selects next monday"
-  );
+
+  await openBookmarkModal();
   await click("#tap_tile_next_week");
-  assert.ok(exists("#tap_tile_next_week.active"), "it selects next week");
+
+  await openBookmarkModal();
   await click("#tap_tile_next_month");
-  assert.ok(exists("#tap_tile_next_month.active"), "it selects next month");
+
+  await openBookmarkModal();
   await click("#tap_tile_custom");
   assert.ok(exists("#tap_tile_custom.active"), "it selects custom");
   assert.ok(exists(".tap-tile-date-input"), "it shows the custom date input");
   assert.ok(exists(".tap-tile-time-input"), "it shows the custom time input");
+  await click("#save-bookmark");
+
+  assert.verifySteps([
+    "tomorrow",
+    "start_of_next_business_week",
+    "next_week",
+    "next_month",
+    "custom"
+  ]);
 });
 
 test("Saving a bookmark with a reminder", async assert => {
-  mockSuccessfulBookmarkPost();
+  mockSuccessfulBookmarkPost(assert);
   await visit("/t/internationalization-localization/280");
   await openBookmarkModal();
   await fillIn("input#bookmark-name", "Check this out later");
   await click("#tap_tile_tomorrow");
-  await click("#save-bookmark");
+
   assert.ok(
     exists(".topic-post:first-child button.bookmark.bookmarked"),
     "it shows the bookmarked icon on the post"
@@ -71,13 +96,15 @@ test("Saving a bookmark with a reminder", async assert => {
     ),
     "it shows the bookmark clock icon because of the reminder"
   );
+  assert.verifySteps(["tomorrow"]);
 });
 
 test("Saving a bookmark with no reminder or name", async assert => {
-  mockSuccessfulBookmarkPost();
+  mockSuccessfulBookmarkPost(assert);
   await visit("/t/internationalization-localization/280");
   await openBookmarkModal();
   await click("#save-bookmark");
+
   assert.ok(
     exists(".topic-post:first-child button.bookmark.bookmarked"),
     "it shows the bookmarked icon on the post"
@@ -88,6 +115,7 @@ test("Saving a bookmark with no reminder or name", async assert => {
     ),
     "it shows the regular bookmark active icon"
   );
+  assert.verifySteps(["none"]);
 });
 
 test("Deleting a bookmark with a reminder", async assert => {
@@ -101,14 +129,21 @@ test("Deleting a bookmark with a reminder", async assert => {
       topic_bookmarked: false
     }
   ]);
-  mockSuccessfulBookmarkPost();
+
+  mockSuccessfulBookmarkPost(assert);
+
   await visit("/t/internationalization-localization/280");
   await openBookmarkModal();
   await click("#tap_tile_tomorrow");

[... diff too long, it was truncated ...]

GitHub sha: 0e4db918

This commit appears in #9611 which was merged by jjaffeux.

This seems a bit hacky to me.

Could you do parsePostData(request.requestBody); instead?

1 Like

Oh didnt jnow we had this, cool :ok_hand:

2 Likes