From b0dd68684160853b0a53a962f32b7c1dfea1639c Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Thu, 15 Feb 2024 16:31:47 +0100 Subject: [PATCH] Migrate `WCF.User.RecentActivityLoader` to typescript --- .../Component/User/RecentActivity/Loader.ts | 95 +++++++++++++++++++ .../Core/Ui/User/Activity/Recent.ts | 1 + wcfsetup/install/files/js/WCF.User.js | 1 + 3 files changed, 97 insertions(+) create mode 100644 ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts diff --git a/ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts b/ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts new file mode 100644 index 0000000000..6296c38248 --- /dev/null +++ b/ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts @@ -0,0 +1,95 @@ +/** + * Handles the list of recent activities. + * + * @author Marcel Werk + * @copyright 2001-2024 WoltLab GmbH + * @license GNU Lesser General Public License + * @since 6.1 + */ + +import { dboAction } from "WoltLabSuite/Core/Ajax"; +import { stringToBool } from "WoltLabSuite/Core/Core"; +import DomUtil from "WoltLabSuite/Core/Dom/Util"; +import { promiseMutex } from "WoltLabSuite/Core/Helper/PromiseMutex"; +import { getPhrase } from "WoltLabSuite/Core/Language"; + +type ResponseLoadMore = { + lastEventID: number; + lastEventTime: number; + template: string; +}; + +async function loadMore(container: HTMLElement): Promise { + const response = (await dboAction("load", "wcf\\data\\user\\activity\\event\\UserActivityEventAction") + .payload({ + lastEventTime: container.dataset.lastEventTime, + lastEventID: container.dataset.lastEventId || 0, + userID: container.dataset.userId || 0, + boxID: container.dataset.boxId || 0, + filteredByFollowedUsers: stringToBool(container.dataset.filteredByFollowedUsers || ""), + }) + .dispatch()) as ResponseLoadMore; + + if (response.template) { + container.dataset.lastEventTime = response.lastEventTime.toString(); + container.dataset.lastEventId = response.lastEventID.toString(); + + const fragment = DomUtil.createFragmentFromHtml(response.template); + container.insertBefore(fragment, container.querySelector(".recentActivityList__showMoreButton")); + } else { + container.querySelector(".recentActivityList__showMoreButton")?.remove(); + showNoMoreEntries(container); + } +} + +function showNoMoreEntries(container: HTMLElement): void { + const div = document.createElement("div"); + div.classList.add("recentActivityList__showMoreButton"); + container.append(div); + + const small = document.createElement("small"); + small.textContent = getPhrase("wcf.user.recentActivity.noMoreEntries"); + div.append(small); +} + +function initShowMoreButton(container: HTMLElement): void { + if (container.querySelector(".recentActivityList__showMoreButton")) { + return; + } + + const div = document.createElement("div"); + div.classList.add("recentActivityList__showMoreButton"); + container.append(div); + + const button = document.createElement("button"); + button.type = "button"; + button.classList.add("button", "small"); + button.textContent = getPhrase("wcf.user.recentActivity.more"); + div.append(button); + + button.addEventListener( + "click", + promiseMutex(() => loadMore(container)), + ); +} + +function initSwitchContextButtons(container: HTMLElement): void { + container.querySelectorAll(".recentActivityList__switchContextButton").forEach((button) => { + button.addEventListener( + "click", + promiseMutex(() => switchContext(container)), + ); + }); +} + +async function switchContext(container: HTMLElement): Promise { + await dboAction("switchContext", "wcf\\data\\user\\activity\\event\\UserActivityEventAction").dispatch(); + + window.location.hash = `#${container.id}`; + window.location.reload(); +} + +export function setup(container: HTMLElement): void { + initShowMoreButton(container); + initSwitchContextButtons(container); +} diff --git a/ts/WoltLabSuite/Core/Ui/User/Activity/Recent.ts b/ts/WoltLabSuite/Core/Ui/User/Activity/Recent.ts index 675296d078..58db2b5106 100644 --- a/ts/WoltLabSuite/Core/Ui/User/Activity/Recent.ts +++ b/ts/WoltLabSuite/Core/Ui/User/Activity/Recent.ts @@ -1,5 +1,6 @@ /** * @woltlabExcludeBundle all + * @deprecated 6.1 use `WoltLabSuite/Core/Components/User/RecentActivity/Loader` instead */ import * as Ajax from "../../../Ajax"; diff --git a/wcfsetup/install/files/js/WCF.User.js b/wcfsetup/install/files/js/WCF.User.js index 0371d82c54..50c25a9f5a 100644 --- a/wcfsetup/install/files/js/WCF.User.js +++ b/wcfsetup/install/files/js/WCF.User.js @@ -1010,6 +1010,7 @@ else { * Loads recent activity events once the user scrolls to the very bottom. * * @param integer userID + * @deprecated 6.1 use `WoltLabSuite/Core/Components/User/RecentActivity/Loader` instead */ WCF.User.RecentActivityLoader = Class.extend({ /** -- 2.20.1