Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | 4x 4x 4x 4x 4x | import { DOCUMENT } from '@angular/common';
import { DestroyRef, Inject, Injectable, inject } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { PaypalComponentsConfig } from 'ish-core/utils/paypal/adapters/paypal-adapters.builder';
import { PAYPAL_MESSAGE_STYLING } from 'ish-core/utils/paypal/adapters/paypal-adapters.styling';
import { PaypalComponent } from 'ish-core/utils/paypal/paypal-model/paypal.model';
/**
* Representation of the PayPal SDK Messages object, responsible for rendering PayPal messages.
* Life cycle of this component ends with destroying of parent component PaymentPaypalComponent.
**/
@Injectable()
export class PaypalMessagesAdapter {
constructor(@Inject(DOCUMENT) private document: Document) {}
private destroyRef = inject(DestroyRef);
/**
* Renders PayPal Messages in the specified container.
* @param config
* @returns
*/
renderMessages(config: PaypalComponentsConfig): Promise<void> {
const paypalObject = (window as unknown as Record<string, PaypalComponent>)[config.scriptNamespace];
const containerId = config.containerId;
// Verify element exists at initialization
Iif (!document.getElementById(containerId)) {
throw new Error(`Container element '${containerId}' not found in DOM at initialization`);
}
Iif (!paypalObject?.Messages) {
throw new Error(
`PayPal Messages not available in loaded paypal sdk script with namespace '${config.scriptNamespace}'`
);
}
config.amount$.pipe(takeUntilDestroyed(this.destroyRef)).subscribe(amount => {
// Check if element still exists before each render attempt
Iif (!this.document.getElementById(containerId)) {
// Element has been removed from DOM, skip rendering silently
return;
}
Iif (amount) {
return paypalObject.Messages(this.getMessagesConfig(config, amount)).render(`#${containerId}`);
}
return paypalObject.Messages(this.getMessagesConfig(config)).render(`#${containerId}`);
});
return Promise.resolve();
}
private getMessagesConfig(config: PaypalComponentsConfig, amount?: number) {
let messageConfig;
switch (config.pageType) {
case 'home':
messageConfig = { style: PAYPAL_MESSAGE_STYLING.home };
break;
case 'product-listing':
messageConfig = { style: PAYPAL_MESSAGE_STYLING.category };
break;
case 'product-details':
messageConfig = { amount, style: PAYPAL_MESSAGE_STYLING.product };
break;
case 'checkout':
messageConfig = { amount, style: PAYPAL_MESSAGE_STYLING.checkout };
break;
default:
messageConfig = { amount, style: PAYPAL_MESSAGE_STYLING.cart };
break;
}
return messageConfig;
}
}
|