diff --git a/src/web-blocks/form/form.tsx b/src/web-blocks/form/form.tsx index 6bc6a134..cfa6d2ff 100644 --- a/src/web-blocks/form/form.tsx +++ b/src/web-blocks/form/form.tsx @@ -20,7 +20,24 @@ const FormBlock = ( } const alpineAttrs = { - "x-data": "{}", + "x-data": `{ + formStatus: '', + async post() { + try { + const formData = new FormData($el); + const response = await fetch($el.action, { + method: $el.method, + body: formData + }); + if (!response.ok) throw new Error(); + this.formStatus = 'SUCCESS'; + $el.querySelector('[x-html]').innerHTML = $el.dataset.success; + } catch (error) { + this.formStatus = 'ERROR'; + $el.querySelector('[x-html]').innerHTML = $el.dataset.error; + } + } + }`, "x-on:submit.prevent": "post", }; const formResponseAttr = {