|
| 1 | +/** |
| 2 | + * Handles the reaction list in the user profile. |
| 3 | + * |
| 4 | + * @author Marcel Werk |
| 5 | + * @copyright 2001-2026 WoltLab GmbH |
| 6 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 7 | + * @since 6.3 |
| 8 | + */ |
| 9 | + |
| 10 | +import DomUtil from "WoltLabSuite/Core/Dom/Util"; |
| 11 | +import { promiseMutex } from "WoltLabSuite/Core/Helper/PromiseMutex"; |
| 12 | +import { getPhrase } from "WoltLabSuite/Core/Language"; |
| 13 | +import { renderUserReactions } from "WoltLabSuite/Core/Api/Users/Reactions/RenderUserReactions"; |
| 14 | + |
| 15 | +async function loadMore(container: HTMLElement): Promise<void> { |
| 16 | + const result = await renderUserReactions( |
| 17 | + parseInt(container.dataset.userId || "0"), |
| 18 | + container.dataset.targetType || "received", |
| 19 | + parseInt(container.dataset.lastLikeTime || "0"), |
| 20 | + parseInt(container.dataset.reactionTypeId || "0"), |
| 21 | + ); |
| 22 | + const response = result.unwrap(); |
| 23 | + |
| 24 | + if ("template" in response) { |
| 25 | + container.dataset.lastLikeTime = response.lastLikeTime.toString(); |
| 26 | + |
| 27 | + const showMoreButton = container.querySelector(".recentActivityList__showMoreButton")!; |
| 28 | + const fragment = DomUtil.createFragmentFromHtml(response.template); |
| 29 | + container.insertBefore(fragment, showMoreButton); |
| 30 | + |
| 31 | + showMoreButton.querySelector("button")!.hidden = false; |
| 32 | + showMoreButton.querySelector("small")!.hidden = true; |
| 33 | + } else { |
| 34 | + const showMoreButton = container.querySelector(".recentActivityList__showMoreButton")!; |
| 35 | + showMoreButton.querySelector("button")!.hidden = true; |
| 36 | + showMoreButton.querySelector("small")!.hidden = false; |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +async function reload(container: HTMLElement): Promise<void> { |
| 41 | + container.querySelectorAll(":scope > div:not(:first-child):not(:last-child)").forEach((el) => el.remove()); |
| 42 | + |
| 43 | + container.dataset.lastLikeTime = "0"; |
| 44 | + |
| 45 | + const showMoreButton = container.querySelector(".recentActivityList__showMoreButton"); |
| 46 | + if (showMoreButton !== null) { |
| 47 | + showMoreButton.querySelector("button")!.hidden = false; |
| 48 | + showMoreButton.querySelector("small")!.hidden = true; |
| 49 | + } |
| 50 | + |
| 51 | + await loadMore(container); |
| 52 | +} |
| 53 | + |
| 54 | +function initShowMoreButton(container: HTMLElement): void { |
| 55 | + if (container.querySelector(".recentActivityList__showMoreButton")) { |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + const div = document.createElement("div"); |
| 60 | + div.classList.add("recentActivityList__showMoreButton"); |
| 61 | + container.append(div); |
| 62 | + |
| 63 | + const button = document.createElement("button"); |
| 64 | + button.type = "button"; |
| 65 | + button.classList.add("button", "small"); |
| 66 | + button.textContent = getPhrase("wcf.like.reaction.more"); |
| 67 | + div.append(button); |
| 68 | + |
| 69 | + const small = document.createElement("small"); |
| 70 | + small.textContent = getPhrase("wcf.like.reaction.noMoreEntries"); |
| 71 | + small.hidden = true; |
| 72 | + div.append(small); |
| 73 | + |
| 74 | + const hasItems = container.querySelectorAll(":scope > div").length > 2; |
| 75 | + if (!hasItems) { |
| 76 | + button.hidden = true; |
| 77 | + small.hidden = false; |
| 78 | + } |
| 79 | + |
| 80 | + button.addEventListener( |
| 81 | + "click", |
| 82 | + promiseMutex(() => loadMore(container)), |
| 83 | + ); |
| 84 | +} |
| 85 | + |
| 86 | +function initTargetTypeButtons(container: HTMLElement): void { |
| 87 | + container.querySelectorAll<HTMLElement>("button[data-target-type]").forEach((button) => { |
| 88 | + button.addEventListener( |
| 89 | + "click", |
| 90 | + promiseMutex(() => { |
| 91 | + const targetType = button.dataset.targetType!; |
| 92 | + if (targetType === container.dataset.targetType) { |
| 93 | + return Promise.resolve(); |
| 94 | + } |
| 95 | + |
| 96 | + container.querySelector("button[data-target-type].active")!.classList.remove("active"); |
| 97 | + button.classList.add("active"); |
| 98 | + container.dataset.targetType = targetType; |
| 99 | + |
| 100 | + return reload(container); |
| 101 | + }), |
| 102 | + ); |
| 103 | + }); |
| 104 | +} |
| 105 | + |
| 106 | +function initReactionTypeButtons(container: HTMLElement): void { |
| 107 | + container.querySelectorAll<HTMLElement>("button[data-reaction-type-id]").forEach((button) => { |
| 108 | + button.addEventListener( |
| 109 | + "click", |
| 110 | + promiseMutex(() => { |
| 111 | + const reactionTypeID = button.dataset.reactionTypeId!; |
| 112 | + const activeButton = container.querySelector("button[data-reaction-type-id].active"); |
| 113 | + |
| 114 | + if (activeButton) { |
| 115 | + activeButton.classList.remove("active"); |
| 116 | + } |
| 117 | + |
| 118 | + if (container.dataset.reactionTypeId !== reactionTypeID) { |
| 119 | + button.classList.add("active"); |
| 120 | + container.dataset.reactionTypeId = reactionTypeID; |
| 121 | + } else { |
| 122 | + container.dataset.reactionTypeId = "0"; |
| 123 | + } |
| 124 | + |
| 125 | + return reload(container); |
| 126 | + }), |
| 127 | + ); |
| 128 | + }); |
| 129 | +} |
| 130 | + |
| 131 | +export function setup(container: HTMLElement): void { |
| 132 | + initShowMoreButton(container); |
| 133 | + initTargetTypeButtons(container); |
| 134 | + initReactionTypeButtons(container); |
| 135 | +} |
0 commit comments