2025-04-01 16:15:20 +02:00
|
|
|
// Dependencies
|
2025-04-02 17:05:54 +02:00
|
|
|
import { linkClasses, validateEmail, getSearchParam } from '@freesewing/utils'
|
2025-04-01 16:15:20 +02:00
|
|
|
|
|
|
|
// Context
|
|
|
|
import { LoadingStatusContext } from '@freesewing/react/context/LoadingStatus'
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
import React, { useState, useContext } from 'react'
|
|
|
|
import { useAccount } from '@freesewing/react/hooks/useAccount'
|
|
|
|
import { useBackend } from '@freesewing/react/hooks/useBackend'
|
|
|
|
|
|
|
|
// Components
|
|
|
|
import { Link as WebLink } from '@freesewing/react/components/Link'
|
|
|
|
import { NoIcon, OkIcon, SaveIcon, RightIcon, WarningIcon } from '@freesewing/react/components/Icon'
|
|
|
|
import { EmailInput } from '@freesewing/react/components/Input'
|
|
|
|
import { Popout } from '@freesewing/react/components/Popout'
|
|
|
|
import { IconButton } from '@freesewing/react/components/Button'
|
2025-04-02 17:05:54 +02:00
|
|
|
import { MiniTip } from '@freesewing/react/components/Mini'
|
2025-04-01 16:15:20 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Component for newsletter signup (by visitors)
|
|
|
|
*
|
|
|
|
* @params {object} props - All React props
|
|
|
|
* @param {function} props.Link - An optional framework-specific Link component
|
|
|
|
*/
|
|
|
|
export const NewsletterSignup = ({ Link = false, noP = false, noTitle = false, noBox = false }) => {
|
|
|
|
if (!Link) Link = WebLink
|
|
|
|
|
|
|
|
// Hooks
|
|
|
|
const { account, setAccount } = useAccount()
|
|
|
|
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, body] = 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 (
|
|
|
|
<div
|
|
|
|
className={
|
2025-04-18 08:07:13 +00:00
|
|
|
noBox ? '' : 'tw:w-full tw:shadow tw:border tw:rounded-lg tw:p-4 tw:bg-secondary/5'
|
2025-04-01 16:15:20 +02:00
|
|
|
}
|
|
|
|
>
|
|
|
|
{subscribed ? (
|
|
|
|
<>
|
|
|
|
<h4>Now check your inbox for a confirmation E-mail</h4>
|
|
|
|
<p>
|
|
|
|
We have sent and email to <b>{email}</b> with a link to confirm this action.
|
|
|
|
</p>
|
2025-04-18 08:07:13 +00:00
|
|
|
<div className="tw:ml-2 tw:pl-2 tw:border-l-4 tw:border-secondary/20">
|
|
|
|
<h6 className="tw:mt-2 tw:pb-0">Why do I need to confirm this?</h6>
|
2025-04-01 16:15:20 +02:00
|
|
|
<small>
|
|
|
|
Without this confirmation step, anyone could attempt to {unsubscribe ? 'un' : ''}
|
|
|
|
subcribe this E-mail address {unsubscribe ? 'from' : 'to'} our newsletter.
|
|
|
|
</small>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
|
|
|
{noTitle ? null : <h4>Subcribe to the FreeSewing newsletter</h4>}
|
|
|
|
{noP ? null : (
|
|
|
|
<p>
|
|
|
|
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.
|
|
|
|
</p>
|
|
|
|
)}
|
|
|
|
<EmailInput
|
|
|
|
label="E-mail Address"
|
|
|
|
labelTR={
|
|
|
|
<>
|
|
|
|
You can{' '}
|
|
|
|
<button className={linkClasses} onClick={() => setUnsubscribe(!unsubscribe)}>
|
|
|
|
{unsubscribe ? 're-' : 'un'}subscribe
|
|
|
|
</button>{' '}
|
|
|
|
at any time
|
|
|
|
</>
|
|
|
|
}
|
|
|
|
labelBL={
|
2025-04-18 08:07:13 +00:00
|
|
|
<span className="tw:flex tw:flex-row tw:items-center tw:flex-wrap tw:gap-1">
|
2025-04-01 16:15:20 +02:00
|
|
|
{validateEmail(email) ? (
|
|
|
|
<>
|
2025-04-18 08:07:13 +00:00
|
|
|
<OkIcon className="tw:text-success tw:w-5 tw:h-5" stroke={3} />
|
2025-04-01 16:15:20 +02:00
|
|
|
Looks great, click below to {unsubscribe ? 'un' : ''}subscribe this address
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<>
|
2025-04-18 08:07:13 +00:00
|
|
|
<WarningIcon className="tw:text-error tw:w-5 tw:h-5" /> Please enter a valid
|
2025-04-01 16:15:20 +02:00
|
|
|
E-mail address
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</span>
|
|
|
|
}
|
|
|
|
update={setEmail}
|
|
|
|
current={email}
|
|
|
|
valid={validateEmail}
|
|
|
|
/>
|
|
|
|
<button
|
2025-04-18 08:07:13 +00:00
|
|
|
className="tw:daisy-btn tw:daisy-btn-primary tw:w-full"
|
2025-04-01 16:15:20 +02:00
|
|
|
disabled={!validateEmail(email)}
|
|
|
|
onClick={subscribe}
|
|
|
|
>
|
|
|
|
{unsubscribe ? `Unsubscribe` : 'Subscribe'}
|
|
|
|
</button>
|
|
|
|
</>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2025-04-02 17:05:54 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Component to handle newsletter unsubscribe links
|
|
|
|
*
|
|
|
|
* @params {object} props - All React props
|
|
|
|
* @param {function} props.Link - An optional framework-specific Link component
|
|
|
|
*/
|
|
|
|
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, body] = await backend.newsletterUnsubscribe(ehash)
|
|
|
|
if (status === 204 || status === 404) setGone(true)
|
|
|
|
else setError(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ehash) return <p>This does not seem to be a valid unsubscribe link.</p>
|
|
|
|
|
|
|
|
if (gone)
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h2>Done</h2>
|
|
|
|
<p>This email address is no longer subscribed to the FreeSewing newsletter</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<h2>Oops</h2>
|
|
|
|
<p>
|
|
|
|
Something went wrong. This is unexpected. Please{' '}
|
|
|
|
<Link className={linkClasses} href="/support">
|
|
|
|
Contact support
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<p>
|
|
|
|
To confirm you want to unsubscribe from the FreeSewing newsletter, click the button below:
|
|
|
|
</p>
|
|
|
|
<button
|
|
|
|
onClick={unsubscribe}
|
2025-04-18 08:07:13 +00:00
|
|
|
className="tw:daisy-btn tw:daisy-btn-primary tw:daisy-btn-large tw:w-full tw:my-4"
|
2025-04-02 17:05:54 +02:00
|
|
|
>
|
|
|
|
Unsubscribe
|
|
|
|
</button>
|
|
|
|
<MiniTip>
|
|
|
|
Sorry, but one-click unsubscribe violates internet standards (
|
|
|
|
<Link
|
|
|
|
href="/docs/about/faq/newsletter/why-unsubscribe-multiple-clicks/"
|
|
|
|
className={linkClasses}
|
|
|
|
>
|
|
|
|
learn more
|
|
|
|
</Link>
|
|
|
|
).
|
|
|
|
</MiniTip>
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
|
|
|
|
return <p>Unsubscribe here {ehash}</p>
|
|
|
|
}
|