Add `ToggleConfirmEmailAction`
authorjoshuaruesweg <ruesweg@woltlab.com>
Wed, 16 Jun 2021 09:26:38 +0000 (11:26 +0200)
committerjoshuaruesweg <ruesweg@woltlab.com>
Mon, 28 Jun 2021 15:28:44 +0000 (17:28 +0200)
ts/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.ts [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.js [new file with mode: 0644]

diff --git a/ts/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.ts b/ts/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.ts
new file mode 100644 (file)
index 0000000..1b7851a
--- /dev/null
@@ -0,0 +1,59 @@
+import AbstractUserAction from "./AbstractUserAction";
+import * as Ajax from "../../../../Ajax";
+import * as Core from "../../../../Core";
+import { AjaxCallbackObject, DatabaseObjectActionResponse } from "../../../../Ajax/Data";
+import * as UiNotification from "../../../../Ui/Notification";
+
+/**
+ * @author  Joshua Ruesweg
+ * @copyright  2001-2021 WoltLab GmbH
+ * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @module  WoltLabSuite/Core/Acp/Ui/User/Action
+ * @since       5.5
+ */
+export class ToggleConfirmEmailAction extends AbstractUserAction {
+  protected init() {
+    this.button.addEventListener("click", (event) => {
+      event.preventDefault();
+
+      Ajax.api(
+        {
+          _ajaxSetup: () => {
+            const isEmailConfirmed = Core.stringToBool(this.userData.dataset.emailConfirmed!);
+
+            return {
+              data: {
+                actionName: (isEmailConfirmed ? "un" : "") + "confirmEmail",
+                className: "wcf\\data\\user\\UserAction",
+                objectIDs: [this.userId],
+              },
+            };
+          },
+
+          _ajaxSuccess: (data: DatabaseObjectActionResponse) => {
+            if (data.objectIDs.includes(this.userId)) {
+              switch (data.actionName) {
+                case "confirmEmail":
+                  this.userData.dataset.emailConfirmed = "true";
+                  this.button.textContent = this.button.dataset.unconfirmEmailMessage!;
+                  break;
+
+                case "unconfirmEmail":
+                  this.userData.dataset.emailConfirmed = "false";
+                  this.button.textContent = this.button.dataset.confirmEmailMessage!;
+                  break;
+
+                default:
+                  throw new Error("Unreachable");
+              }
+            }
+
+            UiNotification.show();
+          },
+        }
+      );
+    });
+  }
+}
+
+export default ToggleConfirmEmailAction;
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Acp/Ui/User/Action/ToggleConfirmEmailAction.js
new file mode 100644 (file)
index 0000000..20bdb65
--- /dev/null
@@ -0,0 +1,53 @@
+define(["require", "exports", "tslib", "./AbstractUserAction", "../../../../Ajax", "../../../../Core", "../../../../Ui/Notification"], function (require, exports, tslib_1, AbstractUserAction_1, Ajax, Core, UiNotification) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.ToggleConfirmEmailAction = void 0;
+    AbstractUserAction_1 = tslib_1.__importDefault(AbstractUserAction_1);
+    Ajax = tslib_1.__importStar(Ajax);
+    Core = tslib_1.__importStar(Core);
+    UiNotification = tslib_1.__importStar(UiNotification);
+    /**
+     * @author  Joshua Ruesweg
+     * @copyright  2001-2021 WoltLab GmbH
+     * @license  GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+     * @module  WoltLabSuite/Core/Acp/Ui/User/Action
+     * @since       5.5
+     */
+    class ToggleConfirmEmailAction extends AbstractUserAction_1.default {
+        init() {
+            this.button.addEventListener("click", (event) => {
+                event.preventDefault();
+                Ajax.api({
+                    _ajaxSetup: () => {
+                        const isEmailConfirmed = Core.stringToBool(this.userData.dataset.emailConfirmed);
+                        return {
+                            data: {
+                                actionName: (isEmailConfirmed ? "un" : "") + "confirmEmail",
+                                className: "wcf\\data\\user\\UserAction",
+                                objectIDs: [this.userId],
+                            },
+                        };
+                    },
+                }, undefined, (data) => {
+                    if (data.objectIDs.includes(this.userId)) {
+                        switch (data.actionName) {
+                            case "confirmEmail":
+                                this.userData.dataset.emailConfirmed = "true";
+                                this.button.textContent = this.button.dataset.unconfirmEmailMessage;
+                                break;
+                            case "unconfirmEmail":
+                                this.userData.dataset.emailConfirmed = "false";
+                                this.button.textContent = this.button.dataset.confirmEmailMessage;
+                                break;
+                            default:
+                                throw new Error("Unreachable");
+                        }
+                    }
+                    UiNotification.show();
+                });
+            });
+        }
+    }
+    exports.ToggleConfirmEmailAction = ToggleConfirmEmailAction;
+    exports.default = ToggleConfirmEmailAction;
+});