/** * WordPress dependencies */ import { CheckboxControl, ExternalLink } from '@safe-wordpress/components'; import { createInterpolateElement, useEffect } from '@safe-wordpress/element'; import { _x, sprintf } from '@safe-wordpress/i18n'; /** * Internal dependencies */ import './style.scss'; import { useAttributes } from '../hooks'; import { DEFAULT_ATTRS } from './config'; import type { FieldSettingProps } from '../types'; const INCOMPATIBLE_SETTINGS = [ 'match_all_segments', 'preload_query_args' ]; const PARTICIPATION_SETTINGS = [ 'exclude_bots', 'percentage_of_tested_visitors', ]; export const AlternativeLoadingSetting = ( { name, disabled, }: FieldSettingProps ): JSX.Element => { const [ attributes, setAttributes ] = useAttributes( name, DEFAULT_ATTRS ); useEffect( () => { INCOMPATIBLE_SETTINGS.forEach( attributes.mode === 'cookie' ? lock : unlock ); PARTICIPATION_SETTINGS.forEach( attributes.mode === 'cookie' && attributes.lockParticipationSettings ? lock : unlock ); }, [ attributes ] ); return (
%2$s', sprintf( /* translators: %s: Cookie name. */ _x( 'Load variants using %s cookie', 'command', 'nelio-ab-testing' ), 'nabAlternative' ), _x( 'Read more…', 'user', 'nelio-ab-testing' ) ), { code: , link: ( // @ts-expect-error Link text will be added later. ), } ) } checked={ 'cookie' === attributes.mode } onChange={ ( checked ) => setAttributes( { mode: checked ? 'cookie' : 'redirection', } ) } /> { 'cookie' === attributes.mode && ( setAttributes( { redirectIfCookieIsMissing: checked || undefined, } ) } /> ) } { 'cookie' === attributes.mode && ( setAttributes( { lockParticipationSettings: checked || undefined, } ) } /> ) }
); }; // ======= // HELPERS // ======= function lock( id: string ): void { id = id.replaceAll( '_', '-' ); const el = document.getElementById( id ); if ( el && 'disabled' in el ) { el.disabled = true; } const parent = el?.parentElement; if ( parent ) { parent.style.opacity = '0.8'; } } function unlock( id: string ): void { id = id.replaceAll( '_', '-' ); const el = document.getElementById( id ); if ( el?.dataset.disabled ) { lock( id ); return; } if ( el && 'disabled' in el ) { el.disabled = false; } const parent = el?.parentElement; if ( parent ) { parent.style.opacity = ''; } }