aboutsummaryrefslogtreecommitdiff
path: root/src/components/Form.astro
blob: 540a4d4580cba34aa1c2f99d9cc0f130b4c39913 (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
---
import { sha256 } from 'js-sha256';

export interface Props {
  board: string;
  tid?: string
}

const { board, tid } = Astro.props;
const iphash = sha256(Astro.clientAddress);
---

<div class="blackbox">
  <slot />
</div>

<script define:vars={{ board, iphash, tid }}>
  document.forms['form'].addEventListener('submit', async (event) => {
    event.preventDefault();

    let form = new FormData(event.target);
    let reader = new FileReader();

      reader.onload = (e) => {
      form.append('image',
                  (e != null) ? e.target.result.toString() : ''
                 );
    }

    reader.onloadend = async () => {
      await fetch(event.target.action, {
        method: event.target.method,
        body: new URLSearchParams(form)
      }).then(async (r) => {
        if(r.status == 200) {
          alert('Thread Successfuly Posted');
          let id = await r.text();
          if(id == 'close') window.top.close();
          window.location.assign(`/board/${board}/${id}`);
        }
        else alert('An Error has Accured');
      })
    }

    form.append('board', board);
    form.append('iphash', iphash);
    if(tid) form.append('tid', tid);

    let image = document.getElementById("image").files[0];
    if(image) {
      reader.readAsDataURL(image);
    } else {
      reader.onload(null);
      await reader.onloadend();
    }

  });
</script>