-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathRpc.ts
More file actions
95 lines (86 loc) · 2.88 KB
/
Rpc.ts
File metadata and controls
95 lines (86 loc) · 2.88 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
84
85
86
87
88
89
90
91
92
93
94
95
/**
* Handles bulk 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,
objectIds: number[],
endpoint: string,
confirmationType: ConfirmationType,
customConfirmationMessage: string = "",
): Promise<void> {
const confirmationResult = await handleConfirmation("", confirmationType, customConfirmationMessage);
if (!confirmationResult.result) {
return;
}
if (confirmationType == ConfirmationType.Delete) {
for (let i = 0; i < objectIds.length; i++) {
const result = await deleteObject(endpoint.replace(/%s/, objectIds[i].toString()));
if (!result.ok) {
return;
}
}
} else {
for (let i = 0; i < objectIds.length; i++) {
const result = await postObject(
endpoint.replace(/%s/, objectIds[i].toString()),
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, () => {
for (let i = 0; i < objectIds.length; i++) {
const element = container.querySelector(`[data-object-id="${objectIds[i]}"]`);
if (!element) {
continue;
}
element.dispatchEvent(
new CustomEvent("interaction:remove", {
bubbles: true,
}),
);
}
});
} else {
for (let i = 0; i < objectIds.length; i++) {
const element = container.querySelector(`[data-object-id="${objectIds[i]}"]`);
if (!element) {
continue;
}
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();
}
container.dispatchEvent(new CustomEvent("interaction:reset-selection"));
}
export function setup(identifier: string, container: HTMLElement): void {
container.addEventListener("bulk-interaction", (event: CustomEvent) => {
if (event.detail.bulkInteraction === identifier) {
void handleRpcInteraction(
container,
JSON.parse(event.detail.objectIds),
event.detail.endpoint,
event.detail.confirmationType,
event.detail.confirmationMessage,
);
}
});
}