-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
51 lines (45 loc) · 1.42 KB
/
main.js
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
// Forms
const initializeForm = (props) => {
let dialog = document.getElementById(props.dialogId)
let form = document.getElementById(props.formId)
let openDialogButtons = document.querySelectorAll(`button[data-action="${props.openDialogActionName}"]`)
let closeDialogButton = document.querySelector(`button[data-action="${props.closeDialogActionName}"]`)
const onFormSubmit = (event) => {
event.preventDefault()
const myForm = event.target
const formData = new FormData(myForm)
const body = new URLSearchParams(formData).toString()
fetch("/", {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body
})
.then(() => dialog.close())
.catch((error) => alert(error))
}
form.addEventListener("submit", onFormSubmit)
openDialogButtons.forEach(button => {
button.addEventListener('click', () => {
dialog.showModal()
})
})
closeDialogButton.addEventListener('click', () => {
dialog.close()
})
}
window.onload = () => {
// Waitlist form
initializeForm({
dialogId: 'waitlistDialog',
formId: 'waitlistForm',
openDialogActionName: 'openWaitlistDialog',
closeDialogActionName: 'closeWaitlistDialog'
})
// Contact us form
initializeForm({
dialogId: 'contactUsDialog',
formId: 'contactUsForm',
openDialogActionName: 'openContactUsDialog',
closeDialogActionName: 'closeContactUsDialog'
})
}