about summary refs log tree commit diff
path: root/templates/javascript/src/onboarding.ts
blob: 0b455eb1728716a62aa492e4f48c0ee8b38752d5 (plain) (blame)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const firstOnboardingCard = "intro";

function switchOnboardingCard(card: string) {
  (Array.from(document.querySelectorAll("form.onboarding > fieldset")) as HTMLElement[])
    .map((node: HTMLElement) => {
      if (node.id == card) {
        node.style.display = "block";
      } else {
        node.style.display = "none";
      }
    });

  (Array.from(document.querySelectorAll("form.onboarding > ul#progressbar > li")) as HTMLElement[])
    .map(node => {
      if (node.id == card) {
        node.classList.add("active")
      } else {
        node.classList.remove("active")
      }
    });
};

interface Window {
  kittybox_onboarding: {
    switchOnboardingCard: (card: string) => void
  }
}

window.kittybox_onboarding = {
  switchOnboardingCard
};

(document.querySelector("form.onboarding > ul#progressbar") as HTMLElement).style.display = "";
switchOnboardingCard(firstOnboardingCard);

function switchCardOnClick(event: MouseEvent) {
  if (event.target instanceof HTMLElement) {
    if (event.target.dataset.card !== undefined) {
      switchOnboardingCard(event.target.dataset.card)
    }
  }
}

function multiInputAddMore(event: (MouseEvent | { target: HTMLElement })) {
  if (event.target instanceof HTMLElement) {
    let parent = event.target.parentElement;
    if (parent !== null) {
      let template = (parent.querySelector("template") as HTMLTemplateElement).content.cloneNode(true);
      parent.prepend(template);
    }
  }
}

(Array.from(
  document.querySelectorAll(
    "form.onboarding > fieldset button.switch_card"
  )
) as HTMLButtonElement[])
  .map(button => {
    button.addEventListener("click", switchCardOnClick)
  });

(Array.from(
  document.querySelectorAll(
    "form.onboarding > fieldset div.multi_input > button.add_more"
  )
) as HTMLButtonElement[])
  .map(button => {
    button.addEventListener("click", multiInputAddMore)
    multiInputAddMore({ target: button });
  });

const form = document.querySelector("form.onboarding") as HTMLFormElement;
console.log(form);
form.onsubmit = async (event: SubmitEvent) => {
    console.log(event);
    event.preventDefault();
    const form = event.target as HTMLFormElement;
    const json = {
        user: {
          type: ["h-card"],
          properties: {
            name: [(form.querySelector("#hcard_name") as HTMLInputElement).value],
            pronoun: (Array.from(
              form.querySelectorAll("#hcard_pronouns")
            ) as HTMLInputElement[])
              .map(input => input.value).filter(i => i != ""),
            url: (Array.from(form.querySelectorAll("#hcard_url")) as HTMLInputElement[])
              .map(input => input.value).filter(i => i != ""),
            note: [(form.querySelector("#hcard_note") as HTMLInputElement).value]
          }
        },
        first_post: {
          type: ["h-entry"],
          properties: {
            content: [(form.querySelector("#first_post_content") as HTMLTextAreaElement).value]
          }
        },
      blog_name: (form.querySelector("#blog_name") as HTMLInputElement).value,
      feeds: (Array.from(
        form.querySelectorAll(".multi_input#custom_feeds > fieldset.feed")
      ) as HTMLElement[])
        .map(form => {
          return {
            name: (form.querySelector("#feed_name") as HTMLInputElement).value,
            slug: (form.querySelector("#feed_slug") as HTMLInputElement).value
          }
        }).filter(feed => feed.name == "" || feed.slug == "")
    };

    await fetch("/.kittybox/onboarding", {
        method: "POST",
        body: JSON.stringify(json),
        headers: { "Content-Type": "application/json" }
    }).then(response => {
        if (response.status == 201) {
            window.location.href = window.location.href;
        }
    })
}