import React, { useState } from 'react'; import { Box, Text, Input, Button, ArrowForwardIcon, Link, } from '../../primitives'; import { Done } from './images'; import Image from 'next/image'; import axios from 'axios'; import { Avatar } from '../../composites'; export type NewsletterAvatarItem = { id: 'string'; userName: 'string'; avatarURl: 'string'; profileUrl: 'string'; }; const emailValidator = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; export const Newsletter = ({ newsletterAvatarData, }: { newsletterAvatarData: NewsletterAvatarItem[]; }) => { const [loading, setLoading] = useState(false); const [, setError] = useState(false); const [success, setSuccess] = useState(false); const [email, setEmail] = useState(''); const [errorMessage, setErrorMessage] = useState(''); const makeRequestToServer = async () => { setLoading(true); const res = await axios.post('/api/newsletter-subscribe', { email: email }); if (res.status === 200) { setSuccess(true); setError(false); setLoading(false); setErrorMessage(''); setEmail(''); } else { setError(true); setSuccess(false); setLoading(false); setErrorMessage('Error in subscribing!'); setEmail(''); } }; const subscribeToNewsLetter = (e: any) => { e.preventDefault(); if (email === '') { setErrorMessage('Email address is required!'); setError(true); setError(true); } else if (!emailValidator.test(email)) { setErrorMessage('Enter a valid email address!'); setError(true); } else { makeRequestToServer(); } }; return ( Get exclusive updates! We can't do this alone, we would love feedback and the fastest way for us to reach out to you is via emails. We won't spam, I promise! mail icon { if (e.key === 'Enter') { subscribeToNewsLetter(e); } }} onChangeText={(value: string) => { setEmail(value); }} fontFamily="$body" placeholder="Enter your email" color="$trueGray400" fontWeight="$normal" py="$0" px="$4" ml="$0" sx={{ '@base': { fontSize: '$xl', lineHeight: '$xl', }, // "@md": { // fontSize: "$2xl", // lineHeight: "$2xl", // }, }} /> {success && ( Thank you for subscribing to our newsletter! )} {errorMessage} {newsletterAvatarData.map((item) => ( {item.userName} ))} ); };