aboutsummaryrefslogtreecommitdiff
path: root/src/components/Form.astro
diff options
context:
space:
mode:
Diffstat (limited to 'src/components/Form.astro')
-rw-r--r--src/components/Form.astro53
1 files changed, 53 insertions, 0 deletions
diff --git a/src/components/Form.astro b/src/components/Form.astro
new file mode 100644
index 0000000..417b569
--- /dev/null
+++ b/src/components/Form.astro
@@ -0,0 +1,53 @@
+---
+import '../styles/blackbox.css';
+import { sha256 } from 'js-sha256';
+
+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();
+ 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>