2023-05-13 16:59:09 +00:00
|
|
|
import { createSignal, JSX, Show } from 'solid-js'
|
2023-02-17 09:21:02 +00:00
|
|
|
|
|
|
|
import { useLocalize } from '../../context/localize'
|
2023-05-13 16:59:09 +00:00
|
|
|
import { isValidEmail } from '../../utils/validators'
|
|
|
|
import { Button } from '../_shared/Button'
|
|
|
|
|
|
|
|
import styles from './Subscribe.module.scss'
|
2023-06-18 18:02:11 +00:00
|
|
|
import { useSnackbar } from '../../context/snackbar'
|
2022-09-09 11:53:35 +00:00
|
|
|
|
|
|
|
export default () => {
|
2023-02-17 09:21:02 +00:00
|
|
|
const { t } = useLocalize()
|
2023-05-13 16:59:09 +00:00
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
const [title, setTitle] = createSignal('')
|
2023-05-13 16:59:09 +00:00
|
|
|
const [email, setEmail] = createSignal('')
|
|
|
|
const [emailError, setEmailError] = createSignal<string>(null)
|
2023-06-18 18:02:11 +00:00
|
|
|
const {
|
|
|
|
actions: { showSnackbar }
|
|
|
|
} = useSnackbar()
|
2023-05-13 16:59:09 +00:00
|
|
|
|
|
|
|
const validate = (): boolean => {
|
|
|
|
if (!email()) {
|
|
|
|
setEmailError(t('Please enter email'))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValidEmail(email())) {
|
|
|
|
setEmailError(t('Please check your email address'))
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
setEmailError(null)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleInput: JSX.ChangeEventHandlerUnion<HTMLInputElement, Event> = (event) => {
|
|
|
|
setEmailError(null)
|
|
|
|
setEmail(event.target.value)
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSubmit = async (event: SubmitEvent) => {
|
|
|
|
event.preventDefault()
|
|
|
|
|
|
|
|
if (!validate()) return
|
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
setTitle(t('...subscribing'))
|
2023-05-11 19:54:05 +00:00
|
|
|
|
|
|
|
const requestOptions = {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
2023-05-13 16:59:09 +00:00
|
|
|
body: JSON.stringify({ email: email() })
|
2023-05-11 19:54:05 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 16:59:09 +00:00
|
|
|
const response = await fetch('/api/newsletter', requestOptions)
|
|
|
|
|
|
|
|
if (response.ok) {
|
|
|
|
setTitle(t('You are subscribed'))
|
2023-06-18 18:02:11 +00:00
|
|
|
showSnackbar({ body: t('Thank you for subscribing') })
|
2023-05-13 16:59:09 +00:00
|
|
|
} else {
|
|
|
|
if (response.status === 400) {
|
|
|
|
setEmailError(t('Please check your email address'))
|
|
|
|
} else {
|
|
|
|
setEmailError(t('Something went wrong, please try again'))
|
|
|
|
}
|
|
|
|
|
|
|
|
setTitle('')
|
|
|
|
}
|
2022-09-09 11:53:35 +00:00
|
|
|
}
|
2022-10-19 22:16:17 +00:00
|
|
|
|
2022-09-09 11:53:35 +00:00
|
|
|
return (
|
2023-05-13 16:59:09 +00:00
|
|
|
<form class={styles.form} onSubmit={handleSubmit} novalidate>
|
2023-05-12 14:57:33 +00:00
|
|
|
<Show when={!title()} fallback={title()}>
|
2023-05-13 16:59:09 +00:00
|
|
|
<div class={styles.controls}>
|
|
|
|
<input
|
|
|
|
type="email"
|
|
|
|
name="email"
|
|
|
|
value={email()}
|
|
|
|
onInput={handleInput}
|
|
|
|
class={styles.input}
|
|
|
|
placeholder={t('Fill email')}
|
|
|
|
/>
|
|
|
|
<Button class={styles.button} type="submit" variant="secondary" value={t('Subscribe')} />
|
|
|
|
</div>
|
|
|
|
<Show when={emailError()}>
|
|
|
|
<div class={styles.error}>{emailError()}</div>
|
|
|
|
</Show>
|
2023-05-12 14:57:33 +00:00
|
|
|
</Show>
|
2023-05-13 16:59:09 +00:00
|
|
|
</form>
|
2022-09-09 11:53:35 +00:00
|
|
|
)
|
|
|
|
}
|