Skip to content

Commit

Permalink
Update avatar in ACP live
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyperghost committed Dec 18, 2024
1 parent a395125 commit 72fcd87
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
25 changes: 24 additions & 1 deletion ts/WoltLabSuite/Core/Component/User/Avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { promiseMutex } from "WoltLabSuite/Core/Helper/PromiseMutex";
import { wheneverFirstSeen } from "WoltLabSuite/Core/Helper/Selector";
import { dialogFactory } from "WoltLabSuite/Core/Component/Dialog";
import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification";
import { registerCallback } from "WoltLabSuite/Core/Form/Builder/Field/Controller/FileProcessor";
import WoltlabCoreFile from "WoltLabSuite/Core/Component/File/woltlab-core-file";

interface Result {
avatar: string;
Expand All @@ -23,8 +25,13 @@ async function editAvatar(button: HTMLElement): Promise<void> {
if (ok) {
const avatarForm = document.getElementById("avatarForm");
if (avatarForm) {
const img = avatarForm.querySelector<HTMLImageElement>("img.userAvatarImage")!;
if (img.src === result.avatar) {
return;
}

// In the ACP, the form should not be reloaded after changing the avatar.
avatarForm.querySelector<HTMLImageElement>("img.userAvatarImage")!.src = result.avatar;
img.src = result.avatar;
showNotification();
} else {
window.location.reload();
Expand All @@ -41,6 +48,22 @@ export function setup(): void {
},
);

const avatarForm = document.getElementById("avatarForm");
if (avatarForm) {
registerCallback("wcf\\action\\UserAvatarAction_avatarFileID", (fileId: number | undefined) => {
if (!fileId) {
return;
}

const file = document.querySelector<WoltlabCoreFile>(
`#wcf\\\\action\\\\UserAvatarAction_avatarFileIDContainer woltlab-core-file[file-id="${fileId}"]`,
)!;

avatarForm.querySelector<HTMLImageElement>("img.userAvatarImage")!.src = file.link!;
showNotification();
});
}

wheneverFirstSeen("[data-edit-avatar]", (button) => {
button.addEventListener(
"click",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72fcd87

Please sign in to comment.