import React from 'react' import MailchimpSubscribe from 'react-mailchimp-subscribe' import { validate } from 'email-validator' import { useKeyboardListener } from 'actionsack' import Input from './Input' import Button from './Button' import { COLORS } from '../lib/constants' const Error = ({ message = 'something went wrong' }) => { if ( message.indexOf && message.indexOf('The domain portion of the email address is invalid') > -1 ) { message = `Oh no, that's not a valid email!` } if (message.indexOf && message.indexOf('is already subscribed')) { message = message.split(' {message} ) } const Form = ({ message, subscribe, error }) => { const [email, setEmail] = React.useState('') const [showError, setShowError] = React.useState(true) const disabled = !validate(email) return (
{ e.preventDefault() subscribe({ EMAIL: email }) }} > { setEmail(e.target.value) setShowError(false) }} /> {error && showError && } ) } const EmailSubscribe = () => { const [open, setOpen] = React.useState(false) useKeyboardListener('Escape', () => setOpen(false)) if (!process.env.NEXT_PUBLIC_MAILCHIMP_URL) { return null } return (
{open && ( { const success = status === 'success' const loading = status === 'sending' const error = status === 'error' return (
{loading || success ? ( {loading ? 'loading...' : 'subscribed!'} ) : (
)}
) }} /> )}
) } export default EmailSubscribe