FIX: Hide form after password reset (#14526)

FIX: Hide form after password reset (#14526)

When hide_email_address_taken was disabled, the forgot password modal showed a flash message and continued to display the form causing confusion. This change shows the flash message only when an error occurs and in all other cases it shows a success message and hides the form.

diff --git a/app/assets/javascripts/discourse/app/controllers/forgot-password.js b/app/assets/javascripts/discourse/app/controllers/forgot-password.js
index 8a7d814..d2887d4 100644
--- a/app/assets/javascripts/discourse/app/controllers/forgot-password.js
+++ b/app/assets/javascripts/discourse/app/controllers/forgot-password.js
@@ -54,35 +54,32 @@ export default Controller.extend(ModalFunctionality, {
           const accountEmailOrUsername = escapeExpression(
             this.accountEmailOrUsername
           );
-          const isEmail = accountEmailOrUsername.match(/@/);
-          let key = `forgot_password.complete_${
-            isEmail ? "email" : "username"
-          }`;
-          let extraClass;
 
-          if (data.user_found === true) {
-            key += "_found";
-            this.set("accountEmailOrUsername", "");
-            this.set(
-              "offerHelp",
+          let key = "forgot_password.complete";
+          key += accountEmailOrUsername.match(/@/) ? "_email" : "_username";
+
+          if (data.user_found === false) {
+            key += "_not_found";
+
+            this.flash(
               I18n.t(key, {
                 email: accountEmailOrUsername,
                 username: accountEmailOrUsername,
-              })
+              }),
+              "error"
             );
           } else {
-            if (data.user_found === false) {
-              key += "_not_found";
-              extraClass = "error";
-            }
+            key += data.user_found ? "_found" : "";
 
-            this.flash(
+            this.set("accountEmailOrUsername", "");
+            this.set(
+              "offerHelp",
               I18n.t(key, {
                 email: accountEmailOrUsername,
                 username: accountEmailOrUsername,
-              }),
-              extraClass
+              })
             );
+            this.set("helpSeen", !data.user_found);
           }
         })
         .catch((e) => {
diff --git a/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js b/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js
index 0905fd3..278f57b 100644
--- a/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js
+++ b/app/assets/javascripts/discourse/tests/acceptance/forgot-password-test.js
@@ -85,3 +85,37 @@ acceptance("Forgot password", function (needs) {
     );
   });
 });
+
+acceptance(
+  "Forgot password - hide_email_address_taken enabled",
+  function (needs) {
+    needs.pretender((server, helper) => {
+      server.post("/session/forgot_password", () => {
+        return helper.response({});
+      });
+    });
+
+    test("requesting password reset", async function (assert) {
+      await visit("/");
+      await click("header .login-button");
+      await click("#forgot-password-link");
+
+      assert.equal(
+        queryAll(".forgot-password-reset").attr("disabled"),
+        "disabled",
+        "it should disable the button until the field is filled"
+      );
+
+      await fillIn("#username-or-email", "someuser");
+      await click(".forgot-password-reset");
+
+      assert.equal(
+        queryAll(".modal-body").html().trim(),
+        I18n.t("forgot_password.complete_username", {
+          username: "someuser",
+        }),
+        "it should display a success message"
+      );
+    });
+  }
+);

GitHub sha: 14efd17b7be628ccbde9b18633b5628d987b0dd4

This commit appears in #14526 which was approved by ZogStriP. It was merged by nbianca.