Migrate `WCF.User.RecentActivityLoader` to typescript
authorMarcel Werk <burntime@woltlab.com>
Thu, 15 Feb 2024 15:31:47 +0000 (16:31 +0100)
committerCyperghost <olaf_schmitz_1@t-online.de>
Fri, 23 Feb 2024 13:43:02 +0000 (14:43 +0100)
ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts [new file with mode: 0644]
ts/WoltLabSuite/Core/Ui/User/Activity/Recent.ts
wcfsetup/install/files/js/WCF.User.js

diff --git a/ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts b/ts/WoltLabSuite/Core/Component/User/RecentActivity/Loader.ts
new file mode 100644 (file)
index 0000000..6296c38
--- /dev/null
@@ -0,0 +1,95 @@
+/**
+ * Handles the list of recent activities.
+ *
+ * @author Marcel Werk
+ * @copyright 2001-2024 WoltLab GmbH
+ * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
+ * @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<void> {
+  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<void> {
+  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);
+}
index 675296d078cbcc7d7ba2c97c2602c69826638560..58db2b51068b0d90e0ea69d0695822869d8d083a 100644 (file)
@@ -1,5 +1,6 @@
 /**
  * @woltlabExcludeBundle all
+ * @deprecated 6.1 use `WoltLabSuite/Core/Components/User/RecentActivity/Loader` instead
  */
 
 import * as Ajax from "../../../Ajax";
index 0371d82c54a5f3c0dc46896adfcfd52ac93aab1e..50c25a9f5a916cd4a25409228c87295ab4e14d33 100644 (file)
@@ -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({
        /**