Skip to content

Commit

Permalink
Update package.json and Form.astro
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcgilibert committed Mar 14, 2024
1 parent ba516f3 commit 9f48a46
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"type": "module",
"version": "0.0.1",
"scripts": {
"dev": "astro dev --remote",
"dev": "astro dev",
"start": "astro dev",
"build": "astro build --remote",
"preview": "astro preview",
Expand Down
45 changes: 39 additions & 6 deletions src/components/Form.astro
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<form class='w-full'>
<div class='mb-5'>
<label for='email' class='block mb-2 text-sm font-medium text-gray-900 dark:text-white'
>Your Email</label
>Tu email</label
>
<input
type='email'
Expand All @@ -19,7 +19,7 @@
<div class='mb-5'></div>

<label for='message' class='block mb-2 text-sm font-medium text-gray-900 dark:text-white'
>Your message</label
>Tu mensaje</label
>
<textarea
id='message'
Expand All @@ -30,16 +30,22 @@
class='w-full p-4 text-lg bg-black/30 rounded-lg'></textarea>
<button
type='submit'
class='w-full p-4 text-lg bg-black/40 hover:border-2 border-white/20 rounded-lg'
'>Send</button
class='w-full p-4 text-lg bg-black/40 hover:scale-105 border-white/20 rounded-lg transition-all'
'>Enviar</button
>

<div
class='flex alert items-center p-4 mb-4 text-sm rounded-lg bg-black/40 text-green-400 mt-6 opacity-0 transition-all duration-300 ease-in-out'
role='alert'
>
<span class='sr-only'>Info</span>
<div class='font-medium'></div>
</div>
</form>

<script>
const form = document.querySelector('form')
form!.addEventListener('submit', async (e) => {
console.log('hola')

e.preventDefault()
const email = form!.email.value
const message = form!.message.value
Expand All @@ -50,5 +56,32 @@
},
body: JSON.stringify({ email, message })
})
.then((res) => {
if (res.ok) {
form!.querySelector('.alert')!.classList.remove('opacity-0')
const alertElement = form!.querySelector('.alert') as HTMLElement
alertElement.innerText = 'Mensaje enviado correctamente'

setInterval(() => {
alertElement.classList.add('opacity-0')
alertElement.innerText = ''
}, 5000)
form!.reset()
} else {
const alertElement = form!.querySelector('.alert') as HTMLElement

alertElement.classList.remove('opacity-0')
alertElement.classList.add('text-red-400')
alertElement.innerText = 'Mensaje no enviado, intente de nuevo.'

setInterval(() => {
alertElement.classList.add('opacity-0')
alertElement.innerText = ''
}, 5000)
}
})
.finally(() => {
form!.reset()
})
})
</script>

0 comments on commit 9f48a46

Please sign in to comment.