|
| 1 | +import clsx from 'clsx'; |
| 2 | +import React, { useState } from 'react'; |
| 3 | + |
| 4 | +import CircleAlertIcon from '@site/src/assets/icons/circle-alert.svg'; |
| 5 | +import CircleCheckIcon from '@site/src/assets/icons/circle-check.svg'; |
| 6 | +import { H2, H3 } from '@site/src/components/Typography'; |
| 7 | + |
| 8 | +import styles from './SubscribeSection.module.scss'; |
| 9 | + |
| 10 | +interface SubscribeSectionProps { |
| 11 | + portalId: string; |
| 12 | + formId: string; |
| 13 | +} |
| 14 | + |
| 15 | +export const SubscribeSection: React.FC<SubscribeSectionProps> = ({ portalId, formId }) => { |
| 16 | + const [email, setEmail] = useState(''); |
| 17 | + const [emailContactConsent, setEmailContactConsent] = useState(false); |
| 18 | + const [status, setStatus] = useState<{ type: 'success' | 'error' | null; message: string }>({ |
| 19 | + type: null, |
| 20 | + message: '', |
| 21 | + }); |
| 22 | + const [isSubmitting, setIsSubmitting] = useState(false); |
| 23 | + |
| 24 | + const getPageUri = () => { |
| 25 | + if (typeof window !== 'undefined' && window.location) { |
| 26 | + return window.location.href; |
| 27 | + } |
| 28 | + return ''; |
| 29 | + }; |
| 30 | + |
| 31 | + const handleSubmit = async (e: React.FormEvent) => { |
| 32 | + e.preventDefault(); |
| 33 | + if (!emailContactConsent) { |
| 34 | + setStatus({ |
| 35 | + type: 'error', |
| 36 | + message: 'Please agree to the privacy policy to subscribe.', |
| 37 | + }); |
| 38 | + return; |
| 39 | + } |
| 40 | + setIsSubmitting(true); |
| 41 | + |
| 42 | + const payload = { |
| 43 | + fields: [ |
| 44 | + { |
| 45 | + name: 'email', |
| 46 | + value: email, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: 'email_contact_consent', |
| 50 | + value: emailContactConsent, |
| 51 | + }, |
| 52 | + ], |
| 53 | + context: { |
| 54 | + pageUri: getPageUri(), |
| 55 | + pageName: 'Homepage - Open Self Service', |
| 56 | + }, |
| 57 | + legalConsentOptions: { |
| 58 | + consent: { |
| 59 | + consentToProcess: true, |
| 60 | + text: 'I consent to the processing of my personal data by Hycom SA as described in the openselfservice_EN_Information_obligation.pdf information clause to respond to inquiries and provide information about products and services.', |
| 61 | + communications: [ |
| 62 | + { |
| 63 | + value: true, |
| 64 | + subscriptionTypeId: '296606641', |
| 65 | + text: 'I agree to receive communications that respond to inquiries and provide information about products and services.', |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }, |
| 70 | + }; |
| 71 | + |
| 72 | + const url = new URL(window.location.href); |
| 73 | + |
| 74 | + try { |
| 75 | + const response = await fetch( |
| 76 | + `https://api.hsforms.com/submissions/v3/integration/submit/${portalId}/${formId}`, |
| 77 | + { |
| 78 | + method: 'POST', |
| 79 | + headers: { 'Content-Type': 'application/json' }, |
| 80 | + body: JSON.stringify(payload), |
| 81 | + }, |
| 82 | + ); |
| 83 | + |
| 84 | + if (response.ok) { |
| 85 | + const data = await response.json(); |
| 86 | + setStatus({ type: 'success', message: data.inlineMessage }); |
| 87 | + setEmail(''); |
| 88 | + setEmailContactConsent(false); |
| 89 | + |
| 90 | + url.searchParams.append('success', 'true'); |
| 91 | + window.history.replaceState(null, null, url); |
| 92 | + } else { |
| 93 | + const data = await response.json(); |
| 94 | + setStatus({ type: 'error', message: data.message }); |
| 95 | + setIsSubmitting(false); |
| 96 | + |
| 97 | + url.searchParams.append('success', 'false'); |
| 98 | + window.history.replaceState(null, null, url); |
| 99 | + } |
| 100 | + } catch (error) { |
| 101 | + setStatus({ |
| 102 | + type: 'error', |
| 103 | + message: 'An unexpected error occurred. Please try again later.', |
| 104 | + }); |
| 105 | + setIsSubmitting(false); |
| 106 | + |
| 107 | + url.searchParams.append('success', 'false'); |
| 108 | + window.history.replaceState(null, null, url); |
| 109 | + } |
| 110 | + }; |
| 111 | + |
| 112 | + return ( |
| 113 | + <div |
| 114 | + className={clsx( |
| 115 | + 'flex flex-col md:flex-row gap-12 md:gap-28 items-start justify-start w-full', |
| 116 | + styles.container, |
| 117 | + )} |
| 118 | + > |
| 119 | + {/* Text Content */} |
| 120 | + <div className="flex flex-col gap-10 items-start justify-start flex-1 md:flex-1"> |
| 121 | + <H2 className="mb-0!"> |
| 122 | + <span className="text-highlighted">Be the first</span> to know about the latest updates, features, |
| 123 | + and insights |
| 124 | + </H2> |
| 125 | + </div> |
| 126 | + |
| 127 | + {/* Form */} |
| 128 | + <div className="w-full md:w-auto md:flex-1 md:max-w-none"> |
| 129 | + {status.type !== 'success' && ( |
| 130 | + <form onSubmit={handleSubmit} className="flex flex-col gap-6"> |
| 131 | + <div> |
| 132 | + <H3 className="mb-6!">Subscribe now</H3> |
| 133 | + <div className="flex gap-2"> |
| 134 | + <label htmlFor="subscribe-email" className="sr-only"> |
| 135 | + Your email address |
| 136 | + </label> |
| 137 | + <input |
| 138 | + id="subscribe-email" |
| 139 | + type="email" |
| 140 | + value={email} |
| 141 | + onChange={(e) => setEmail(e.target.value)} |
| 142 | + placeholder="Your email address" |
| 143 | + className={clsx( |
| 144 | + 'flex-1 h-10 px-3 py-2 rounded-md bg-white text-sm text-gray-900', |
| 145 | + styles.input, |
| 146 | + )} |
| 147 | + required |
| 148 | + disabled={isSubmitting} |
| 149 | + /> |
| 150 | + <button type="submit" className="button text-sm!" disabled={isSubmitting}> |
| 151 | + Subscribe |
| 152 | + </button> |
| 153 | + </div> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div className="flex items-start gap-2"> |
| 157 | + <input |
| 158 | + type="checkbox" |
| 159 | + id="subscribe-consent" |
| 160 | + checked={emailContactConsent} |
| 161 | + onChange={(e) => setEmailContactConsent(e.target.checked)} |
| 162 | + className="mt-0.5 accent-violet" |
| 163 | + required |
| 164 | + disabled={isSubmitting} |
| 165 | + /> |
| 166 | + <label htmlFor="subscribe-consent" className="text-sm text-white cursor-pointer leading-5"> |
| 167 | + I consent to the processing of my personal data by Hycom SA as described in the{' '} |
| 168 | + <a |
| 169 | + href="/docs/openselfservice_EN_Information_obligation.pdf" |
| 170 | + target="_blank" |
| 171 | + className="underline" |
| 172 | + > |
| 173 | + information clause |
| 174 | + </a>{' '} |
| 175 | + to respond to inquiries and provide information about products and services. |
| 176 | + </label> |
| 177 | + </div> |
| 178 | + </form> |
| 179 | + )} |
| 180 | + |
| 181 | + {status.type && ( |
| 182 | + <div className="bg-white rounded-xl shadow-md p-8"> |
| 183 | + <div className={clsx('flex items-start gap-2', styles.contactFormText)}> |
| 184 | + {status.type === 'success' ? ( |
| 185 | + <CircleCheckIcon className="h-6 w-6 shrink-0 mt-0.5" /> |
| 186 | + ) : ( |
| 187 | + <CircleAlertIcon className="h-6 w-6 shrink-0 mt-0.5" /> |
| 188 | + )} |
| 189 | + <p |
| 190 | + className={clsx('mb-0!', status.type === 'success' ? styles.success : styles.error)} |
| 191 | + dangerouslySetInnerHTML={{ __html: status.message }} |
| 192 | + /> |
| 193 | + </div> |
| 194 | + </div> |
| 195 | + )} |
| 196 | + </div> |
| 197 | + </div> |
| 198 | + ); |
| 199 | +}; |
0 commit comments