-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathRpc.ts
More file actions
83 lines (77 loc) · 2.47 KB
/
Rpc.ts
File metadata and controls
83 lines (77 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Handles interactions that call a RPC endpoint.
*
* @author Marcel Werk
* @copyright 2001-2025 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @since 6.2
*/
import { deleteObject } from "WoltLabSuite/Core/Api/DeleteObject";
import { postObject } from "WoltLabSuite/Core/Api/PostObject";
import { show as showNotification } from "WoltLabSuite/Core/Ui/Notification";
import { ConfirmationType, handleConfirmation } from "./Confirmation";
async function handleRpcInteraction(
container: HTMLElement,
element: HTMLElement,
objectName: string,
endpoint: string,
confirmationType: ConfirmationType,
customConfirmationMessage: string = "",
invalidatesAllItems = false,
): Promise<void> {
const confirmationResult = await handleConfirmation(objectName, confirmationType, customConfirmationMessage);
if (!confirmationResult.result) {
return;
}
if (confirmationType == ConfirmationType.Delete) {
const result = await deleteObject(endpoint);
if (!result.ok) {
return;
}
} else {
const result = await postObject(
endpoint,
confirmationResult.reason ? { reason: confirmationResult.reason } : undefined,
);
if (!result.ok) {
return;
}
}
if (confirmationType === ConfirmationType.Delete) {
// TODO: This shows a generic success message and should be replaced with a more specific message.
showNotification(undefined, () => {
element.dispatchEvent(
new CustomEvent("interaction:remove", {
bubbles: true,
}),
);
});
} else {
if (invalidatesAllItems) {
container.dispatchEvent(new CustomEvent("interaction:invalidate-all"));
} else {
element.dispatchEvent(
new CustomEvent("interaction:invalidate", {
bubbles: true,
}),
);
}
// TODO: This shows a generic success message and should be replaced with a more specific message.
showNotification();
}
}
export function setup(identifier: string, container: HTMLElement): void {
container.addEventListener("interaction:execute", (event: CustomEvent) => {
if (event.detail.interaction === identifier) {
void handleRpcInteraction(
container,
event.target as HTMLElement,
event.detail.objectName,
event.detail.endpoint,
event.detail.confirmationType,
event.detail.confirmationMessage,
event.detail.invalidatesAllItems === "true",
);
}
});
}