// Dependencies import { linkClasses, validateEmail, getSearchParam } from '@freesewing/utils' // Context import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus' // Hooks import React, { useState, useContext } from 'react' import { useBackend } from '@freesewing/react/hooks/useBackend' // Components import { Link as WebLink } from '@freesewing/react/components/Link' import { OkIcon, WarningIcon } from '@freesewing/react/components/Icon' import { EmailInput } from '@freesewing/react/components/Input' import { MiniTip } from '@freesewing/react/components/Mini' /** * Component for newsletter signup by visitors (not logged-in users) * * @component * @param {object} props - All component props * @param {React.FC} [props.Link = false] - An optional framework-specific Link component * @param {boolean} [props.noP = false] - Set this to true to not display the signup message paragraph * @param {boolean} [props.noTitle = false] - Set this to true to not display the signup title * @param {boolean} [props.noBox = false] - Set this to true to not apply the box style * @returns {JSX.Element} */ export const NewsletterSignup = ({ Link = false, noP = false, noTitle = false, noBox = false }) => { if (!Link) Link = WebLink // Hooks const backend = useBackend() const { setLoadingStatus } = useContext(LoadingStatusContext) // State const [email, setEmail] = useState('') const [unsubscribe, setUnsubscribe] = useState(false) const [subscribed, setSubscribed] = useState(false) // Helper method to handle subscription const subscribe = async () => { setLoadingStatus([true, 'Contacting backend']) const [status] = unsubscribe ? await backend.newsletterStartUnsubscribe(email) : await backend.newsletterSubscribe(email) if (status === 200) { setLoadingStatus([true, 'Request Initiated', true, true]) setSubscribed(true) } else setLoadingStatus([true, 'An error occured. Please report this.', true, true]) } return (
{subscribed ? ( <>

Now check your inbox for a confirmation E-mail

We have sent and email to {email} with a link to confirm this action.

Why do I need to confirm this?
Without this confirmation step, anyone could attempt to {unsubscribe ? 'un' : ''} subcribe this E-mail address {unsubscribe ? 'from' : 'to'} our newsletter.
) : ( <> {noTitle ? null :

Subcribe to the FreeSewing newsletter

} {noP ? null : (

Subscribe to our newsletter and once every 3 months you will receive an email from us with honest wholesome content. No tracking, no ads, no nonsense.

)} You can{' '} {' '} at any time } labelBL={ {validateEmail(email) ? ( <> Looks great, click below to {unsubscribe ? 'un' : ''}subscribe this address ) : ( <> Please enter a valid E-mail address )} } update={setEmail} current={email} valid={validateEmail} /> )}
) } /** * Component for handling newsletter unsubscribe links * * @component * @param {object} props - All component props * @param {React.FC} [props.Link = false] - An optional framework-specific Link component * @returns {JSX.Element} */ export const NewsletterUnsubscribe = ({ Link = false }) => { if (!Link) Link = WebLink const ehash = getSearchParam('x') // Hooks const backend = useBackend() // State const [gone, setGone] = useState(false) const [error, setError] = useState(false) // Helper method to handle subscription const unsubscribe = async () => { const [status] = await backend.newsletterUnsubscribe(ehash) if (status === 204 || status === 404) setGone(true) else setError(true) } if (!ehash) return

This does not seem to be a valid unsubscribe link.

if (gone) return ( <>

Done

This email address is no longer subscribed to the FreeSewing newsletter

) if (error) return ( <>

Oops

Something went wrong. This is unexpected. Please{' '} Contact support

) return ( <>

To confirm you want to unsubscribe from the FreeSewing newsletter, click the button below:

Sorry, but one-click unsubscribe violates internet standards ( learn more ). ) }