summaryrefslogtreecommitdiff
path: root/src/blinkies/add.njk
blob: 999204116f330a15f489f19c60defe23cb84e354 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
---
layout: base.njk
title: Add a Blinkie!
---

<h2>Add your very own blinkie!</h2>
<p>
  Please allow us 1 to 10 business days to process your request. Your engagement
  is very important to our shareholders.
</p>

<form
  id="form"
  method="POST"
  action="https://posthook.liz.coffee/hook/add-a-blinkie"
  enctype="multipart/form-data"
>
  <div class="form-group">
    <label for="name">Name *</label>
    <input
      type="text"
      id="name"
      name="name"
      placeholder="Elizabeth..."
      required
    />
  </div>
  <br />

  <div class="form-group">
    <label for="email">Email</label>
    <p>
      <small>
        In case something goes wrong or you want to update the blinkie in the future and need to email <a href="mailto:me@liz.coffee">me</a>.
      </small>
    </p>
    <input
      type="email"
      id="email"
      name="email"
      placeholder="me@liz.coffee..."
    />
  </div>
  <br />

  <div class="form-group">
    <label for="description">Description *</label>
    <p>
      <small>
         Please explain why you think this blinkie is a good addition to this site.
        "I am cool as fuck yo" is a valid reason.
      </small>
    </p>
    <input
      type="text"
      id="description"
      name="description"
      placeholder="I am cool as fuck yo..."
      required
    />
  </div>
  <br />

  <div class="form-group">
    <label for="gif">Blinkie *</label>
    <p><small>Blinkie gif in 150px:20px or equivalent aspect ratio.</small></p>
    <input
      type="file"
      id="gif"
      name="gif"
      accept="image/gif"
      required
    />
    <label for="gif" class="file-input-button">
      Browse Files
    </label>

  </div>
  <br />

  <div
    class="h-captcha"
    data-sitekey="70fc31c2-32a4-4304-8b5a-b492eba656ec"
  ></div>

  <br />

  <input type="hidden" name="_redirect" value="https://liz.coffee/" />
  <input id="_token" type="hidden" name="_token" />

  <button type="submit">Submit</button>
</form>

<script src="https://hcaptcha.com/1/api.js" async defer></script>

<script>
  const form = document.getElementById("form");
  const tokenInput = document.getElementById("_token");
  const fileInput = document.getElementById("gif");
  const fileName = document.getElementById("file-name");
  const fileGroup = document.getElementById("file-group");

  fileInput.addEventListener("change", () => {
    if (fileInput.files.length > 0) {
      fileName.textContent = fileInput.files[0].name;
      fileGroup.classList.add("has-file");
    } else {
      fileName.textContent = "No file selected";
      fileGroup.classList.remove("has-file");
    }
  });

  form.addEventListener("submit", async (event) => {
    event.preventDefault();
    const hcaptchaResponse = hcaptcha.getResponse();
    if (!hcaptchaResponse) {
      alert("Please complete the hCaptcha before submitting.");
      return;
    }
    try {
      const tokenRes = await fetch(
        "https://posthook.liz.coffee/hook/add-a-blinkie/token",
        { credentials: "omit" }
      );
      const tokenData = await tokenRes.json();
      const csrfToken = tokenData?.ok?.token;
      if (!csrfToken) throw new Error("Missing token");
      tokenInput.value = csrfToken;

      const formData = new FormData(form);
      const submitRes = await fetch(form.action, {
        method: "POST",
        body: formData,
        headers: {
          "H-Captcha-Response": hcaptchaResponse,
          "X-CSRF-Token": csrfToken,
        },
        redirect: "manual",
        credentials: "omit",
      });

      const redirectUrl = formData.get("_redirect");
      if (typeof redirectUrl === "string" && redirectUrl.length > 0) {
        window.location.assign(redirectUrl);
        return;
      }
      const text = await submitRes.text();
      if (!submitRes.ok) throw new Error(text || "Submit failed");
    } catch (err) {
      alert("Something went wrong. Please try again.");
      hcaptcha.reset();
    }
  });
</script>