import { v4 as uuidv4 } from "uuid"; import defineComponentProperties from "./defineComponentProperties"; import kebabToCamelCase from "./kebabToCamelCase"; /* A dialog that registers new payment methods on a Moov account */ class PaymentMethods extends HTMLElement { static observedAttributes = ['facilitator-account-id', 'token', 'account-id']; static componentProps = ['accountData', 'facilitatorAccountID', 'onCancel', 'onError', 'onResourceCreated', 'onPlaidRedirect', 'open', 'token', 'plaid', 'accountID', 'paymentMethodTypes', 'microDeposits', 'allowedCardBrands']; private _drop: any; [key: string]: any; constructor() { super(); } handleMoovDropsInitialized = () => { if (!this.id) this.id = `moov-payment-methods-${uuidv4()}`; const initialPropValues = {} as Record; PaymentMethods.componentProps.forEach((propName) => { if (propName in this) initialPropValues[propName] = this[propName]; }); const dropObject = window.drops.paymentMethods(initialPropValues, this.id); this._drop = dropObject; } handleMoovJsLoaded = () => { document.removeEventListener("moov.moovJSLoaded", this.handleMoovJSLoaded); if (window.Moov && !window.drops) window.Moov("fake-token").drops().then(this.handleMoovDropsInitialized); } connectedCallback() { if (window.drops) this.handleMoovDropsInitialized(); if (window.Moov && !window.drops) this.handleMoovJsLoaded(); if (!window.Moov && !window.drops) document.addEventListener("moov.moovJSLoaded", this.handleMoovJsLoaded); } attributeToProperty(attribute: string) { if (attribute === "account-id") return "accountID"; if (attribute === "facilitator-account-id") return "facilitatorAccountID"; return kebabToCamelCase(attribute); } /** Callback fired when an observed attribute changes value */ attributeChangedCallback(name: string, oldValue: string, newValue: string) { if (oldValue === newValue) return; const propertyName = this.attributeToProperty(name); if (propertyName in this) this[propertyName] = newValue; } } defineComponentProperties(PaymentMethods, PaymentMethods.componentProps); export default PaymentMethods;