import { __, sprintf } from '@wordpress/i18n';
import { SelectControl, RadioControl } from '@wordpress/components';
import { PayPalCheckboxGroup } from '../../ReusableComponents/Fields';
import { useState, useMemo, useEffect } from '@wordpress/element';
import { PayPalScriptProvider, PayPalButtons } from '@paypal/react-paypal-js';
import {
defaultLocationSettings,
paymentMethodOptions,
colorOptions,
shapeOptions,
buttonLayoutOptions,
buttonLabelOptions,
} from '../../../data/settings/tab-styling-data';
const TabStyling = () => {
const [ location, setLocation ] = useState( 'cart' );
const [ canRender, setCanRender ] = useState( false );
const [ locationSettings, setLocationSettings ] = useState( {
...defaultLocationSettings,
} );
// Sometimes buttons won't render. This fixes the timing problem.
useEffect( () => {
const handleDOMContentLoaded = () => setCanRender( true );
if (
document.readyState === 'interactive' ||
document.readyState === 'complete'
) {
handleDOMContentLoaded();
} else {
document.addEventListener(
'DOMContentLoaded',
handleDOMContentLoaded
);
}
}, [] );
const currentLocationSettings = useMemo( () => {
return locationSettings[ location ];
}, [ location, locationSettings ] );
const locationOptions = useMemo( () => {
return Object.keys( locationSettings ).reduce(
( locationOptionsData, key ) => {
locationOptionsData.push( {
value: locationSettings[ key ].value,
label: locationSettings[ key ].label,
} );
return locationOptionsData;
},
[]
);
}, [] );
const updateButtonSettings = ( key, value ) => {
setLocationSettings( {
...locationSettings,
[ location ]: {
...currentLocationSettings,
settings: {
...currentLocationSettings.settings,
[ key ]: value,
},
},
} );
};
const updateButtonStyle = ( key, value ) => {
setLocationSettings( {
...locationSettings,
[ location ]: {
...currentLocationSettings,
settings: {
...currentLocationSettings.settings,
style: {
...currentLocationSettings.settings.style,
[ key ]: value,
},
},
},
} );
};
if ( ! canRender ) {
return <>>;
}
return (
);
};
const TabStylingSection = ( props ) => {
let sectionTitleClassName = 'ppcp-r-styling__section';
if ( props?.className ) {
sectionTitleClassName += ` ${ props.className }`;
}
return (
{ props.title }
{ props?.description && (
) }
{ props.children }
);
};
const SectionIntro = ( { location } ) => {
const { description, descriptionLink } =
defaultLocationSettings[ location ];
const buttonStyleDescription = sprintf( description, descriptionLink );
return (
);
};
const SectionLocations = ( { locationOptions, location, setLocation } ) => {
return (
setLocation( newLocation ) }
label={ __( 'Locations', 'woocommerce-paypal-payments' ) }
options={ locationOptions }
/>
);
};
const SectionPaymentMethods = ( {
locationSettings,
updateButtonSettings,
} ) => {
return (
updateButtonSettings( 'paymentMethods', newValue )
}
currentValue={ locationSettings.settings.paymentMethods }
/>
);
};
const SectionButtonLayout = ( { locationSettings, updateButtonStyle } ) => {
const buttonLayoutIsAllowed =
locationSettings.settings.style?.layout &&
locationSettings.settings.style?.tagline === false;
return (
buttonLayoutIsAllowed && (
updateButtonStyle( 'layout', newValue )
}
selected={ locationSettings.settings.style.layout }
options={ buttonLayoutOptions }
/>
)
);
};
const SectionButtonShape = ( { locationSettings, updateButtonStyle } ) => {
return (
updateButtonStyle( 'shape', newValue )
}
selected={ locationSettings.settings.style.shape }
options={ shapeOptions }
/>
);
};
const SectionButtonLabel = ( { locationSettings, updateButtonStyle } ) => {
return (
updateButtonStyle( 'label', newValue )
}
value={ locationSettings.settings.style.label }
label={ __( 'Button Label', 'woocommerce-paypal-payments' ) }
options={ buttonLabelOptions }
/>
);
};
const SectionButtonColor = ( { locationSettings, updateButtonStyle } ) => {
return (
updateButtonStyle( 'color', newValue )
}
value={ locationSettings.settings.style.color }
options={ colorOptions }
/>
);
};
const SectionButtonTagline = ( { locationSettings, updateButtonStyle } ) => {
const taglineIsAllowed =
locationSettings.settings.style.hasOwnProperty( 'tagline' ) &&
locationSettings.settings.style?.layout === 'horizontal';
return (
taglineIsAllowed && (
{
updateButtonStyle( 'tagline', newValue );
} }
currentValue={ locationSettings.settings.style.tagline }
/>
)
);
};
const SectionButtonPreview = ( { locationSettings } ) => {
return (
Error
);
};
export default TabStyling;