summaryrefslogtreecommitdiff
path: root/public/js/modules/group-linker.js
blob: ca5e15931f407accb0a8a639b9b4c1c666cb5ec1 (plain)
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
function eventGroupLinker() {
    return {
        data: {
            eventGroupID: "",
            eventGroupEditToken: "",
            groups: [],
        },
        async init() {
            this.$watch("data.eventGroupID", () => {
                this.$dispatch(
                    "event-group-id-changed",
                    this.data.eventGroupID,
                );
            });
            this.$watch("data.eventGroupEditToken", () => {
                this.$dispatch(
                    "event-group-edit-token-changed",
                    this.data.eventGroupEditToken,
                );
            });
            if (window.eventData && window.eventData.eventGroupID !== "") {
                this.data.eventGroupID = window.eventData.eventGroupID;
            }
            if (window.eventData && window.eventGroupEditToken !== "") {
                this.data.eventGroupEditToken =
                    window.eventData.eventGroupEditToken;
            }
            try {
                const editTokens = JSON.parse(
                    localStorage.getItem("editTokens"),
                );
                if (!editTokens) {
                    return;
                }
                const response = await fetch("/known/groups", {
                    method: "POST",
                    body: JSON.stringify(editTokens),
                    headers: {
                        "Content-Type": "application/json",
                    },
                });
                if (!response.ok) {
                    return;
                }
                const json = await (await response).json();
                this.data.groups = json;
            } catch (e) {
                return false;
            }
        },
        selectGroup(e) {
            const group = this.data.groups.find(
                (group) => group.id === e.target.value,
            );
            if (!group) {
                this.data.eventGroupID = "";
                this.data.eventGroupEditToken = "";
                return;
            }
            this.data.eventGroupID = group.id;
            this.data.eventGroupEditToken = group.editToken;
        },
        showGroupPreview() {
            return (
                this.data.eventGroupID !== "" &&
                this.data.groups.some(
                    (group) =>
                        group.id === this.data.eventGroupID &&
                        group.editToken === this.data.eventGroupEditToken,
                )
            );
        },
        groupPreview() {
            if (!this.showGroupPreview()) {
                return {};
            }
            return this.data.groups.find(
                (group) => group.id === this.data.eventGroupID,
            );
        },
        resetGroupSelector() {
            this.$refs.eventGroupSelect.value = "";
        },
    };
}