|
| 1 | +import { useState } from 'react'; |
| 2 | +import { decode } from 'html-entities'; |
| 3 | +import newsletterStyles from "../../styles/Newsletter.module.scss"; |
| 4 | + |
| 5 | +const NewsletterForm = ( { status, message, onValidated }) => { |
| 6 | + |
| 7 | + const [ error, setError ] = useState(null); |
| 8 | + const [ email, setEmail ] = useState(null); |
| 9 | + |
| 10 | + /** |
| 11 | + * Handle form submit. |
| 12 | + * |
| 13 | + * @return {{value}|*|boolean|null} |
| 14 | + */ |
| 15 | + const handleFormSubmit = () => { |
| 16 | + |
| 17 | + setError(null); |
| 18 | + |
| 19 | + if ( ! email ) { |
| 20 | + setError( 'Please enter a valid email address' ); |
| 21 | + return null; |
| 22 | + } |
| 23 | + |
| 24 | + const isFormValidated = onValidated({ EMAIL: email }); |
| 25 | + |
| 26 | + // On success return true |
| 27 | + return email && email.indexOf("@") > -1 && isFormValidated; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Handle Input Key Event. |
| 32 | + * |
| 33 | + * @param event |
| 34 | + */ |
| 35 | + const handleInputKeyEvent = ( event ) => { |
| 36 | + setError(null); |
| 37 | + // Number 13 is the "Enter" key on the keyboard |
| 38 | + if (event.keyCode === 13) { |
| 39 | + // Cancel the default action, if needed |
| 40 | + event.preventDefault(); |
| 41 | + // Trigger the button element with a click |
| 42 | + handleFormSubmit(); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * Extract message from string. |
| 48 | + * |
| 49 | + * @param {String} message |
| 50 | + * @return {null|*} |
| 51 | + */ |
| 52 | + const getMessage = (message) => { |
| 53 | + if ( !message ) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + const result = message?.split('-') ?? null; |
| 57 | + if ( "0" !== result?.[0]?.trim() ) { |
| 58 | + return decode(message); |
| 59 | + } |
| 60 | + const formattedMessage = result?.[1]?.trim() ?? null; |
| 61 | + return formattedMessage ? decode( formattedMessage ) : null; |
| 62 | + } |
| 63 | + |
| 64 | + return ( |
| 65 | + <> |
| 66 | + <div> |
| 67 | + <input |
| 68 | + onChange={(event) => setEmail(event?.target?.value ?? '')} |
| 69 | + type="email" |
| 70 | + placeholder="Your email" |
| 71 | + onKeyUp={(event) => handleInputKeyEvent(event)} |
| 72 | + /> |
| 73 | + <button onClick={handleFormSubmit}> |
| 74 | + Submit |
| 75 | + </button> |
| 76 | + </div> |
| 77 | + <div className={ newsletterStyles.newsletterFormInfo }> |
| 78 | + {status === "sending" && <div className={ newsletterStyles.newsletterFormSending }>Sending...</div>} |
| 79 | + {status === "error" || error ? ( |
| 80 | + <div |
| 81 | + className={ newsletterStyles.newsletterFormError } |
| 82 | + dangerouslySetInnerHTML={{ __html: error || getMessage( message ) }} |
| 83 | + /> |
| 84 | + ) : null } |
| 85 | + {status === "success" && status !== "error" && !error && ( |
| 86 | + <div className={ newsletterStyles.newsletterFormSuccess } dangerouslySetInnerHTML={{ __html: decode(message) }} /> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + </> |
| 90 | + ); |
| 91 | +} |
| 92 | + |
| 93 | +export default NewsletterForm; |
0 commit comments