|
| 1 | +import { LitElement } from 'lit' |
| 2 | +import { |
| 3 | + getEmailCollectionSnapshot, |
| 4 | + subscribeToEmailCollection, |
| 5 | + type EmailCollectionState, |
| 6 | +} from '@components/scripts/store' |
| 7 | +import { addScriptBreadcrumb } from '@components/scripts/errors' |
| 8 | +import { handleScriptError } from '@components/scripts/errors/handler' |
| 9 | +import { defineCustomElement } from '@components/scripts/utils' |
| 10 | +import type { WebComponentModule } from '@components/scripts/@types/webComponentModule' |
| 11 | +import { getDownloadCtaPrimaryLink, getDownloadCtaUrls } from './selectors' |
| 12 | + |
| 13 | +const scriptName = 'DownloadCtaElement' |
| 14 | + |
| 15 | +export class DownloadCtaElement extends LitElement { |
| 16 | + static registeredName = 'download-cta' |
| 17 | + |
| 18 | + private primaryLink: HTMLAnchorElement | null = null |
| 19 | + private unsubscribeFromEmailCollection: (() => void) | null = null |
| 20 | + private isInitialized = false |
| 21 | + |
| 22 | + protected override createRenderRoot(): HTMLElement { |
| 23 | + return this |
| 24 | + } |
| 25 | + |
| 26 | + override connectedCallback(): void { |
| 27 | + super.connectedCallback() |
| 28 | + |
| 29 | + queueMicrotask(() => { |
| 30 | + this.initialize() |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + override disconnectedCallback(): void { |
| 35 | + this.unsubscribeFromEmailCollection?.() |
| 36 | + this.unsubscribeFromEmailCollection = null |
| 37 | + this.isInitialized = false |
| 38 | + super.disconnectedCallback() |
| 39 | + } |
| 40 | + |
| 41 | + public initialize(): void { |
| 42 | + const context = { scriptName, operation: 'initialize' } |
| 43 | + addScriptBreadcrumb(context) |
| 44 | + |
| 45 | + try { |
| 46 | + if (this.isInitialized) { |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + this.primaryLink = getDownloadCtaPrimaryLink(this) |
| 51 | + const { landingUrl, directDownloadUrl } = getDownloadCtaUrls(this) |
| 52 | + |
| 53 | + this.syncPrimaryLink(getEmailCollectionSnapshot(), { |
| 54 | + landingUrl, |
| 55 | + directDownloadUrl, |
| 56 | + }) |
| 57 | + |
| 58 | + this.unsubscribeFromEmailCollection = subscribeToEmailCollection(state => { |
| 59 | + this.syncPrimaryLink(state, { |
| 60 | + landingUrl, |
| 61 | + directDownloadUrl, |
| 62 | + }) |
| 63 | + }) |
| 64 | + |
| 65 | + this.isInitialized = true |
| 66 | + } catch (error) { |
| 67 | + handleScriptError(error, context) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private syncPrimaryLink( |
| 72 | + state: EmailCollectionState, |
| 73 | + urls: { landingUrl: string; directDownloadUrl: string } |
| 74 | + ): void { |
| 75 | + if (!this.primaryLink) { |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + const hasProvidedEmail = state.hasProvidedEmail === true |
| 80 | + const nextHref = hasProvidedEmail ? urls.directDownloadUrl : urls.landingUrl |
| 81 | + const nextState = hasProvidedEmail ? 'ready' : 'gated' |
| 82 | + |
| 83 | + this.primaryLink.href = nextHref |
| 84 | + this.primaryLink.dataset['emailState'] = nextState |
| 85 | + this.dataset['emailState'] = nextState |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +export const registerDownloadCtaWebComponent = (tagName = DownloadCtaElement.registeredName) => { |
| 90 | + if (typeof window === 'undefined') { |
| 91 | + return |
| 92 | + } |
| 93 | + |
| 94 | + defineCustomElement(tagName, DownloadCtaElement) |
| 95 | +} |
| 96 | + |
| 97 | +export const registerWebComponent = registerDownloadCtaWebComponent |
| 98 | + |
| 99 | +export const webComponentModule: WebComponentModule<DownloadCtaElement> = { |
| 100 | + registeredName: DownloadCtaElement.registeredName, |
| 101 | + componentCtor: DownloadCtaElement, |
| 102 | + registerWebComponent: registerDownloadCtaWebComponent, |
| 103 | +} |
0 commit comments