From: Marcel Werk Date: Tue, 9 Jan 2024 11:37:59 +0000 (+0100) Subject: Apply suggestions from code review X-Git-Tag: 6.1.0_Alpha_1~216^2 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=e7aab5ea7373cff054f4ea06d7d08b8a12ccc03e;p=GitHub%2FWoltLab%2FWCF.git Apply suggestions from code review --- diff --git a/ts/WoltLabSuite/Core/BootstrapFrontend.ts b/ts/WoltLabSuite/Core/BootstrapFrontend.ts index 2ed6d376df..8b8b5fac8c 100644 --- a/ts/WoltLabSuite/Core/BootstrapFrontend.ts +++ b/ts/WoltLabSuite/Core/BootstrapFrontend.ts @@ -117,4 +117,7 @@ export function setup(options: BootstrapOptions): void { whenFirstSeen("woltlab-core-comment-response", () => { void import("./Component/Comment/Response/woltlab-core-comment-response"); }); + whenFirstSeen("[data-follow-user]", () => { + void import("./Component/User/Follow").then(({ setup }) => setup()); + }); } diff --git a/ts/WoltLabSuite/Core/Component/User/Follow.ts b/ts/WoltLabSuite/Core/Component/User/Follow.ts index 0bb7d09e82..4d4d35a06a 100644 --- a/ts/WoltLabSuite/Core/Component/User/Follow.ts +++ b/ts/WoltLabSuite/Core/Component/User/Follow.ts @@ -8,34 +8,42 @@ */ import { prepareRequest } from "WoltLabSuite/Core/Ajax/Backend"; +import { promiseMutex } from "WoltLabSuite/Core/Helper/PromiseMutex"; +import { wheneverFirstSeen } from "WoltLabSuite/Core/Helper/Selector"; import { getPhrase } from "WoltLabSuite/Core/Language"; +import * as UiNotification from "WoltLabSuite/Core/Ui/Notification"; -function toggleFollow(button: HTMLButtonElement): void { - if (button.dataset.following != "1") { - button.dataset.following = "1"; - button.dataset.tooltip = getPhrase("wcf.user.button.unfollow"); - button.querySelector("fa-icon")?.setIcon("circle-minus"); - void prepareRequest(button.dataset.endpoint!) +async function toggleFollow(button: HTMLElement): Promise { + if (button.dataset.following !== "1") { + await prepareRequest(button.dataset.followUser!) .post({ action: "follow", }) .fetchAsResponse(); + + button.dataset.following = "1"; + button.dataset.tooltip = getPhrase("wcf.user.button.unfollow"); + button.querySelector("fa-icon")?.setIcon("circle-minus"); } else { - button.dataset.following = "0"; - button.dataset.tooltip = getPhrase("wcf.user.button.follow"); - button.querySelector("fa-icon")?.setIcon("circle-plus"); - void prepareRequest(button.dataset.endpoint!) + await prepareRequest(button.dataset.followUser!) .post({ action: "unfollow", }) .fetchAsResponse(); + + button.dataset.following = "0"; + button.dataset.tooltip = getPhrase("wcf.user.button.follow"); + button.querySelector("fa-icon")?.setIcon("circle-plus"); } + + UiNotification.show(); } export function setup(): void { - document.querySelectorAll(".jsFollowButton").forEach((button) => { - button.addEventListener("click", () => { - toggleFollow(button); - }); + wheneverFirstSeen("[data-follow-user]", (button) => { + button.addEventListener( + "click", + promiseMutex(() => toggleFollow(button)), + ); }); } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js b/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js index 8ae7bbcc04..a24fdbdd4e 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/BootstrapFrontend.js @@ -90,6 +90,9 @@ define(["require", "exports", "tslib", "./BackgroundQueue", "./Bootstrap", "./Co (0, LazyLoader_1.whenFirstSeen)("woltlab-core-comment-response", () => { void new Promise((resolve_4, reject_4) => { require(["./Component/Comment/Response/woltlab-core-comment-response"], resolve_4, reject_4); }).then(tslib_1.__importStar); }); + (0, LazyLoader_1.whenFirstSeen)("[data-follow-user]", () => { + void new Promise((resolve_5, reject_5) => { require(["./Component/User/Follow"], resolve_5, reject_5); }).then(tslib_1.__importStar).then(({ setup }) => setup()); + }); } exports.setup = setup; }); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/User/Follow.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/User/Follow.js index 7f354bbe77..2a7f7659f5 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/User/Follow.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/User/Follow.js @@ -6,37 +6,37 @@ * @license GNU Lesser General Public License * @since 6.1 */ -define(["require", "exports", "WoltLabSuite/Core/Ajax/Backend", "WoltLabSuite/Core/Language"], function (require, exports, Backend_1, Language_1) { +define(["require", "exports", "tslib", "WoltLabSuite/Core/Ajax/Backend", "WoltLabSuite/Core/Helper/PromiseMutex", "WoltLabSuite/Core/Helper/Selector", "WoltLabSuite/Core/Language", "WoltLabSuite/Core/Ui/Notification"], function (require, exports, tslib_1, Backend_1, PromiseMutex_1, Selector_1, Language_1, UiNotification) { "use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setup = void 0; - function toggleFollow(button) { - if (button.dataset.following != "1") { - button.dataset.following = "1"; - button.dataset.tooltip = (0, Language_1.getPhrase)("wcf.user.button.unfollow"); - button.querySelector("fa-icon")?.setIcon("circle-minus"); - void (0, Backend_1.prepareRequest)(button.dataset.endpoint) + UiNotification = tslib_1.__importStar(UiNotification); + async function toggleFollow(button) { + if (button.dataset.following !== "1") { + await (0, Backend_1.prepareRequest)(button.dataset.followUser) .post({ action: "follow", }) .fetchAsResponse(); + button.dataset.following = "1"; + button.dataset.tooltip = (0, Language_1.getPhrase)("wcf.user.button.unfollow"); + button.querySelector("fa-icon")?.setIcon("circle-minus"); } else { - button.dataset.following = "0"; - button.dataset.tooltip = (0, Language_1.getPhrase)("wcf.user.button.follow"); - button.querySelector("fa-icon")?.setIcon("circle-plus"); - void (0, Backend_1.prepareRequest)(button.dataset.endpoint) + await (0, Backend_1.prepareRequest)(button.dataset.followUser) .post({ action: "unfollow", }) .fetchAsResponse(); + button.dataset.following = "0"; + button.dataset.tooltip = (0, Language_1.getPhrase)("wcf.user.button.follow"); + button.querySelector("fa-icon")?.setIcon("circle-plus"); } + UiNotification.show(); } function setup() { - document.querySelectorAll(".jsFollowButton").forEach((button) => { - button.addEventListener("click", () => { - toggleFollow(button); - }); + (0, Selector_1.wheneverFirstSeen)("[data-follow-user]", (button) => { + button.addEventListener("click", (0, PromiseMutex_1.promiseMutex)(() => toggleFollow(button))); }); } exports.setup = setup; diff --git a/wcfsetup/install/files/lib/action/UserFollowAction.class.php b/wcfsetup/install/files/lib/action/UserFollowAction.class.php index 959fa93716..7cf6fafa96 100644 --- a/wcfsetup/install/files/lib/action/UserFollowAction.class.php +++ b/wcfsetup/install/files/lib/action/UserFollowAction.class.php @@ -56,7 +56,7 @@ final class UserFollowAction implements RequestHandlerInterface EOT ); - if ($bodyParameters['action'] == 'follow') { + if ($bodyParameters['action'] === 'follow') { $this->assertUserIsNotIgnored($user); $command = new Follow(WCF::getUser(), $user); @@ -82,10 +82,10 @@ final class UserFollowAction implements RequestHandlerInterface private function assertUserIsNotIgnored(User $target): void { $sql = "SELECT ignoreID - FROM wcf" . WCF_N . "_user_ignore + FROM wcf1_user_ignore WHERE userID = ? AND ignoreUserID = ?"; - $statement = WCF::getDB()->prepareStatement($sql); + $statement = WCF::getDB()->prepare($sql); $statement->execute([ $target->userID, WCF::getUser()->userID, diff --git a/wcfsetup/install/files/lib/system/event/listener/PreloadPhrasesCollectingListener.class.php b/wcfsetup/install/files/lib/system/event/listener/PreloadPhrasesCollectingListener.class.php index d32a33fd4a..9691c1fc48 100644 --- a/wcfsetup/install/files/lib/system/event/listener/PreloadPhrasesCollectingListener.class.php +++ b/wcfsetup/install/files/lib/system/event/listener/PreloadPhrasesCollectingListener.class.php @@ -151,5 +151,7 @@ final class PreloadPhrasesCollectingListener $event->preload('wcf.user.language'); $event->preload('wcf.user.panel.settings'); $event->preload('wcf.user.panel.showAll'); + $event->preload('wcf.user.button.follow'); + $event->preload('wcf.user.button.unfollow'); } }