|
| 1 | +'use client'; |
| 2 | + |
| 3 | +import { useRef, useState } from 'react'; |
| 4 | +import { CallToAction, cn, Heading, Input } from '@theguild/components'; |
| 5 | + |
| 6 | +export function NewsletterFormCard(props: React.HTMLAttributes<HTMLElement>) { |
| 7 | + type Idle = undefined; |
| 8 | + type Pending = { status: 'pending'; message?: never }; |
| 9 | + type Success = { status: 'success'; message: string }; |
| 10 | + type Error = { status: 'error'; message: string }; |
| 11 | + type State = Idle | Pending | Success | Error; |
| 12 | + const [state, setState] = useState<State>(); |
| 13 | + |
| 14 | + // we don't want to blink a message on retries when request is pending |
| 15 | + const lastErrorMessage = useRef<string>(); |
| 16 | + lastErrorMessage.current = state?.message || lastErrorMessage.current; |
| 17 | + |
| 18 | + return ( |
| 19 | + <article |
| 20 | + {...props} |
| 21 | + className={cn( |
| 22 | + props.className, |
| 23 | + 'bg-primary dark:bg-primary/95 light @container/card text-green-1000 relative rounded-2xl', |
| 24 | + )} |
| 25 | + > |
| 26 | + <div className="p-6 pb-0"> |
| 27 | + <Heading |
| 28 | + as="h3" |
| 29 | + size="xs" |
| 30 | + className="@[354px]/card:text-5xl/[56px] @[354px]/card:tracking-[-0.48px]" |
| 31 | + > |
| 32 | + Stay in the loop |
| 33 | + </Heading> |
| 34 | + <p className="relative mt-4"> |
| 35 | + Get the latest insights and best practices on GraphQL API management delivered straight to |
| 36 | + your inbox. |
| 37 | + </p> |
| 38 | + </div> |
| 39 | + <form |
| 40 | + className="relative z-10 p-6" |
| 41 | + onSubmit={async event => { |
| 42 | + event.preventDefault(); |
| 43 | + const email = event.currentTarget.email.value; |
| 44 | + |
| 45 | + if (!email?.includes('@')) { |
| 46 | + setState({ status: 'error', message: 'Please enter a valid email address.' }); |
| 47 | + return; |
| 48 | + } |
| 49 | + |
| 50 | + setState({ status: 'pending' }); |
| 51 | + |
| 52 | + try { |
| 53 | + const response = await fetch('https://utils.the-guild.dev/api/newsletter-subscribe', { |
| 54 | + body: JSON.stringify({ email }), |
| 55 | + method: 'POST', |
| 56 | + }); |
| 57 | + |
| 58 | + const json = await response.json(); |
| 59 | + if (json.status === 'success') { |
| 60 | + lastErrorMessage.current = undefined; |
| 61 | + setState({ status: 'success', message: json.message }); |
| 62 | + } else { |
| 63 | + setState({ status: 'error', message: json.message }); |
| 64 | + } |
| 65 | + } catch (e: unknown) { |
| 66 | + if (!navigator.onLine) { |
| 67 | + setState({ |
| 68 | + status: 'error', |
| 69 | + message: 'Please check your internet connection and try again.', |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + if (e instanceof Error && e.message !== 'Failed to fetch') { |
| 74 | + setState({ status: 'error', message: e.message }); |
| 75 | + return; |
| 76 | + } |
| 77 | + |
| 78 | + setState({ status: 'error', message: 'Something went wrong. Please let us know.' }); |
| 79 | + } |
| 80 | + }} |
| 81 | + > |
| 82 | + <Input |
| 83 | + name="email" |
| 84 | + placeholder="E-mail" |
| 85 | + severity={lastErrorMessage.current ? 'critical' : undefined} |
| 86 | + message={lastErrorMessage.current} |
| 87 | + /> |
| 88 | + {!state || state.status === 'error' ? ( |
| 89 | + <CallToAction type="submit" variant="secondary-inverted" className="mt-2 !w-full"> |
| 90 | + Subscribe |
| 91 | + </CallToAction> |
| 92 | + ) : state.status === 'pending' ? ( |
| 93 | + <CallToAction |
| 94 | + type="submit" |
| 95 | + variant="secondary-inverted" |
| 96 | + className="mt-2 !w-full" |
| 97 | + disabled |
| 98 | + > |
| 99 | + Subscribing... |
| 100 | + </CallToAction> |
| 101 | + ) : state.status === 'success' ? ( |
| 102 | + <CallToAction |
| 103 | + type="reset" |
| 104 | + variant="secondary-inverted" |
| 105 | + className="group/button mt-2 !w-full before:absolute" |
| 106 | + onClick={() => { |
| 107 | + // the default behavior of <button type="reset"> doesn't work here |
| 108 | + // because it gets unmounted too fast |
| 109 | + setTimeout(() => { |
| 110 | + setState(undefined); |
| 111 | + }, 0); |
| 112 | + }} |
| 113 | + > |
| 114 | + <span className="group-hover/button:hidden group-focus/button:hidden">Subscribed</span> |
| 115 | + <span aria-hidden className="hidden group-hover/button:block group-focus/button:block"> |
| 116 | + Another email? |
| 117 | + </span> |
| 118 | + </CallToAction> |
| 119 | + ) : null} |
| 120 | + </form> |
| 121 | + <DecorationArch color="#A2C1C4" className="absolute bottom-0 right-0" /> |
| 122 | + </article> |
| 123 | + ); |
| 124 | +} |
| 125 | + |
| 126 | +function DecorationArch({ className, color }: { className?: string; color: string }) { |
| 127 | + return ( |
| 128 | + <svg width="200" height="200" viewBox="0 0 200 200" fill="none" className={className}> |
| 129 | + <path |
| 130 | + d="M6.72485 73.754C2.74132 77.7375 0.499999 83.1445 0.499999 88.7742L0.499998 199.5L41.2396 199.5L41.2396 74.3572C41.2396 56.0653 56.0652 41.2396 74.3571 41.2396L199.5 41.2396L199.5 0.500033L88.7741 0.500032C83.1444 0.500032 77.7374 2.74135 73.7539 6.72488L42.0931 38.3857L38.3856 42.0932L6.72485 73.754Z" |
| 131 | + stroke="url(#paint0_linear_2735_2359)" |
| 132 | + /> |
| 133 | + <defs> |
| 134 | + <linearGradient |
| 135 | + id="paint0_linear_2735_2359" |
| 136 | + x1="100" |
| 137 | + y1="104.605" |
| 138 | + x2="6.24999" |
| 139 | + y2="3.28952" |
| 140 | + gradientUnits="userSpaceOnUse" |
| 141 | + > |
| 142 | + <stop stopColor={color} stopOpacity="0" /> |
| 143 | + <stop offset="1" stopColor={color} stopOpacity="0.8" /> |
| 144 | + </linearGradient> |
| 145 | + </defs> |
| 146 | + </svg> |
| 147 | + ); |
| 148 | +} |
0 commit comments