<commit_before>/* @flow */
/** @jsx node */

import { node, dom, type ChildType } from 'jsx-pragmatic/src';
import { FUNDING, CARD, FPTI_KEY } from '@paypal/sdk-constants/src';
import { popup, supportsPopups, writeElementToWindow } from 'belter/src';
import { assertSameDomain, type CrossDomainWindowType } from 'cross-domain-utils/src';
import { SpinnerPage } from '@paypal/common-components/src';
import { getLogger } from '@paypal/sdk-client/src';

import { DEFAULT_POPUP_SIZE } from '../checkout';
import { Buttons } from '../../ui';
import { type ButtonProps } from '../../ui/buttons/props';

type PrerenderedButtonsProps = {|
    nonce : ?string,
    props : ButtonProps,
    onRenderCheckout : ({|
        win? : CrossDomainWindowType,
        fundingSource : $Values<typeof FUNDING>,
        card : ?$Values<typeof CARD>
    |}) => void
|};

export function PrerenderedButtons({ nonce, onRenderCheckout, props } : PrerenderedButtonsProps) : ChildType {

    const handleClick = (event, { fundingSource, card } : {| fundingSource : $Values<typeof FUNDING>, card : ?$Values<typeof CARD> |}) => {
        getLogger().info('button_prerender_click').track({
            [ FPTI_KEY.BUTTON_SESSION_UID ]: props.buttonSessionID,
            [ FPTI_KEY.CONTEXT_TYPE ]:       'button_session_id',
            [ FPTI_KEY.CONTEXT_ID ]:         props.buttonSessionID,
            [ FPTI_KEY.TRANSITION ]:         'process_button_prerender_click'
        }).flush();
        
        if (supportsPopups()) {
            const win = assertSameDomain(popup('', {
                width:  DEFAULT_POPUP_SIZE.WIDTH,
                height: DEFAULT_POPUP_SIZE.HEIGHT
            }));

            const doc = window.document;

            const spinner = (
                <SpinnerPage nonce={ nonce } />
            ).render(dom({ doc }));

            writeElementToWindow(win, spinner);

            onRenderCheckout({ win, fundingSource, card });
<commit_msg>Avoid multiple popups on Prerender state (#1635)<commit_after>/* @flow */
/** @jsx node */

import { node, dom, type ChildType } from 'jsx-pragmatic/src';
import { FUNDING, CARD, FPTI_KEY } from '@paypal/sdk-constants/src';
import { popup, supportsPopups, writeElementToWindow } from 'belter/src';
import { assertSameDomain, type CrossDomainWindowType } from 'cross-domain-utils/src';
import { SpinnerPage } from '@paypal/common-components/src';
import { getLogger } from '@paypal/sdk-client/src';

import { DEFAULT_POPUP_SIZE } from '../checkout';
import { Buttons } from '../../ui';
import { type ButtonProps } from '../../ui/buttons/props';

type PrerenderedButtonsProps = {|
    nonce : ?string,
    props : ButtonProps,
    onRenderCheckout : ({|
        win? : CrossDomainWindowType,
        fundingSource : $Values<typeof FUNDING>,
        card : ?$Values<typeof CARD>
    |}) => void
|};

export function PrerenderedButtons({ nonce, onRenderCheckout, props } : PrerenderedButtonsProps) : ChildType {
    let win;
    const handleClick = (event, { fundingSource, card } : {| fundingSource : $Values<typeof FUNDING>, card : ?$Values<typeof CARD> |}) => {
        getLogger().info('button_prerender_click').track({
            [ FPTI_KEY.BUTTON_SESSION_UID ]: props.buttonSessionID,
            [ FPTI_KEY.CONTEXT_TYPE ]:       'button_session_id',
            [ FPTI_KEY.CONTEXT_ID ]:         props.buttonSessionID,
            [ FPTI_KEY.TRANSITION ]:         'process_button_prerender_click'
        }).flush();
        
        if (supportsPopups()) {
            // remember the popup window to prevent showing a new popup window on every click in the prerender state
            if (!win || win.closed) {
                win = assertSameDomain(popup('', {
                    width:  DEFAULT_POPUP_SIZE.WIDTH,
                    height: DEFAULT_POPUP_SIZE.HEIGHT
                }));
            }

            const doc = window.document;

            const spinner = (
                <SpinnerPage nonce={ nonce } />
            ).render(dom({ doc }));

            writeElementToWindow(win, spinner);

            onRenderCheckout({ win, fundingSource, card });

{'fraction_non_alphanumeric': 0.1298449612403101, 'fraction_numerical': 0.0009689922480620155, 'mean_word_length': 2.618755477651183, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5425498', 'n_tokens_mistral': 1237, 'n_tokens_neox': 1215, 'n_words': 268}