/** * WordPress dependencies */ import { useDispatch, useSelect } from '@wordpress/data'; import { useEffect, useState } from '@wordpress/element'; /** * Internal dependencies */ import { store as NAB_CHECKER } from '../store'; import { removeCookie, setCookie } from '../../public/utils/cookies'; export const Status = (): JSX.Element => (
); // ===== // VIEWS // ===== function CloudStatus() { const cloudStatus = useSelect( ( select ) => select( NAB_CHECKER ).getCloudStatus(), [] ); return (
Cloud
{ 'unreachable' === cloudStatus && (
Cloud is not reachable and, therefore, events can’t be properly tracked.
) }
); } function PageStatus() { return (
Page Status
); } function PageStatusContent(): JSX.Element | string { const { isAwaitingGdpr, status } = useSelect( ( select ) => ( { isAwaitingGdpr: select( NAB_CHECKER ).isAwaitingGdpr(), status: select( NAB_CHECKER ).getStatus(), } ), [] ); switch ( status.type ) { case 'missing-settings': return ( <> Global object nabSettings is missing. ); case 'bootstrap': switch ( status.detail ) { case 'missing-session': return 'Something went wrong. Visitor can’t be tested.'; case 'awaiting-session': return 'Awaiting session…'; case 'awaiting-variant': return 'Loading variant…'; } // eslint-disable-next-line no-fallthrough case 'content-ready': if ( isAwaitingGdpr ) { return 'Awaiting GDPR consent…'; } return status.isTestVariant ? 'Variant content ready' : 'Untested content ready'; } } function AlternativeSelector() { const { actualAlternative, cookieTesting, maxCombinations, newAlternative, } = useSelect( ( select ) => ( { actualAlternative: select( NAB_CHECKER ).getAlternative(), cookieTesting: select( NAB_CHECKER ).getSettings().cookieTesting, maxCombinations: select( NAB_CHECKER ).getSettings().maxCombinations, newAlternative: select( NAB_CHECKER ).getNewAlternative(), } ), [] ); const { setNewAlternative } = useDispatch( NAB_CHECKER ); const [ locked, lock ] = useState( false ); const isDirty = newAlternative !== actualAlternative; const onClick = () => { lock( true ); setCookie( 'nabAlternative', newAlternative, { expires: 120 } ); removeCookie( 'nabExperimentsWithPageViews' ); removeCookie( 'nabUniqueViews' ); if ( false === cookieTesting ) { const url = new URL( window.location.href ); url.searchParams.set( 'nab', `${ newAlternative }` ); window.location.href = url.toString(); } else { window.location.reload(); } }; return (
); } function NumberControl( { id, value: inputValue, min, max, disabled, onChange, }: { readonly id?: string; readonly value: number; readonly min: number; readonly max: number; readonly disabled?: boolean; readonly onChange: ( n: number ) => void; } ): JSX.Element { const [ value, setValue ] = useState( `${ inputValue }` ); useEffect( () => setValue( `${ inputValue }` ), [ inputValue ] ); return ( { const newValue = ev.target.value; setValue( newValue ); const newNumericValue = Number.parseInt( newValue ); if ( min <= newNumericValue && newNumericValue <= max ) { onChange( newNumericValue ); } } } /> ); }