summaryrefslogtreecommitdiff
path: root/public/js/modules/group-linker.js
diff options
context:
space:
mode:
authorRaphael Kabo <raphaelkabo@hey.com>2023-10-09 19:22:39 +0100
committerRaphael Kabo <raphaelkabo@hey.com>2023-10-09 19:22:39 +0100
commit7fe1f42d56edb98875399b1da5e9b7e972209a0d (patch)
tree6f96696b583e4fab1a2d1f54a2f99bcee3267c04 /public/js/modules/group-linker.js
parentdbbb94117c2d6266cfc45a091b4b87012024f788 (diff)
Add known group linker to event form
Diffstat (limited to 'public/js/modules/group-linker.js')
-rw-r--r--public/js/modules/group-linker.js85
1 files changed, 85 insertions, 0 deletions
diff --git a/public/js/modules/group-linker.js b/public/js/modules/group-linker.js
new file mode 100644
index 0000000..ca5e159
--- /dev/null
+++ b/public/js/modules/group-linker.js
@@ -0,0 +1,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 = "";
+ },
+ };
+}