setAttributes( { email: value } ) }
/>
{ submissionError && (
{ sprintf(
/* translators: %s: Email address. */
_x(
'Something went wrong and we couldn’t receive your support request. Please try again later or send us an email to %s.',
'user',
'nelio-ab-testing'
),
'support@neliosoftware.com'
) }
) }
setReason( { value: 'other', details: '' } )
}
>
{ _x( 'Back', 'command', 'nelio-ab-testing' ) }
{ isTicketSubmitting
? _x( 'Submitting…', 'text', 'nelio-ab-testing' )
: _x(
'Submit and Keep Plugin',
'command',
'nelio-ab-testing'
) }
>
);
};
const DefaultDeactivationBody = ( {
isLocked,
isSubscribed,
isStagingSite,
mainActionLabel,
cleanAndDeactivate,
reason,
setReason,
deleteStagingDataOnly,
setDeleteStagingDataOnly,
}: ModalState ): JSX.Element => (
<>
{ _x(
'If you have a moment, please share why you are deactivating Nelio A/B Testing:',
'user',
'nelio-ab-testing'
) }
setReason( { value, details: '' } )
}
extraValue={ reason.details }
onExtraChange={ ( details ) => setReason( { ...reason, details } ) }
disabled={ isLocked }
/>
{ isSubscribed && isStagingSite && (
) }
{ isSubscribed && (
{ _x(
'Deactivating the plugin does not cancel your Nelio A/B Testing subscription. To unsubscribe, visit the plugin’s Account page first.',
'user',
'nelio-ab-testing'
) }
) }
{ isLocked ? (
) : (
cleanAndDeactivate( deleteStagingDataOnly )
}
>
{ _x( 'Just Delete Data', 'command', 'nelio-ab-testing' ) }
) }
cleanAndDeactivate(
deleteStagingDataOnly,
! reason.details
? reason.value
: `${ reason.value }: ${ reason.details }`
)
}
>
{ mainActionLabel }
>
);
// =====
// HOOKS
// =====
type ModalState = ReturnType< typeof useModalState >;
const useModalState = ( {
cleanNonce,
deactivationUrl,
isSubscribed,
isStagingSite,
}: DeactivationActionProps ) => {
const [ state, setState ] = useState( INIT_STATE );
const redirect = () => {
window.location.href = deactivationUrl;
};
const openModal = () =>
setState( {
...INIT_STATE,
isModalOpen: true,
} );
const closeModal = () =>
setState( {
...INIT_STATE,
isModalOpen: false,
} );
const deactivate = () => {
setState( {
...state,
reason: INIT_STATE.reason,
isDeactivating: true,
isLocked: true,
} );
redirect();
};
const lock = ( isLocked = true ) => setState( { ...state, isLocked } );
const cleanAndDeactivate = (
deleteStagingDataOnly: boolean,
reason?: string
): void => {
setState( { ...state, isDeactivating: true } );
void apiFetch( {
path: '/nab/v1/plugin/clean',
method: 'POST',
data: { deleteStagingDataOnly, reason, nabnonce: cleanNonce },
} ).then(
redirect, // on success
closeModal // on error
);
};
const setReason = ( reason: State[ 'reason' ] ) =>
setState( { ...state, reason } );
const setDeleteStagingDataOnly = ( deleteStagingDataOnly: boolean ) =>
setState( { ...state, deleteStagingDataOnly } );
const mainActionLabel =
'temporary-deactivation' === state.reason.value
? getDeactivationLabel( state.isDeactivating )
: getCleanAndDeactivateLabel( state.isDeactivating );
return {
isModalOpen: state.isModalOpen,
openModal,
closeModal,
isDeactivating: state.isDeactivating,
isSubscribed,
isStagingSite,
mainActionLabel,
deactivate,
cleanAndDeactivate,
isLocked: state.isLocked,
lock,
reason: state.reason,
setReason,
deleteStagingDataOnly: state.deleteStagingDataOnly,
setDeleteStagingDataOnly,
};
};
const getDeactivationLabel = ( isDeactivating: boolean ) =>
isDeactivating
? _x( 'Deactivating…', 'text', 'nelio-ab-testing' )
: _x( 'Deactivate', 'command', 'nelio-ab-testing' );
const getCleanAndDeactivateLabel = ( isDeleting: boolean ) =>
isDeleting
? _x( 'Deleting Data…', 'text', 'nelio-ab-testing' )
: _x( 'Submit and Delete Data', 'command', 'nelio-ab-testing' );
// ====
// DATA
// ====
const INIT_STATE: State = {
isModalOpen: false,
isDeactivating: false,
isLocked: false,
reason: {
value: 'temporary-deactivation',
details: '',
},
deleteStagingDataOnly: true,
};
const INIT_CONTACT_FORM_STATE: ContactFormState = {
isTicketSubmitting: false,
email: '',
description: '',
submissionError: false,
};
const DEACTIVATION_MODES: ReadonlyArray< {
readonly value: State[ 'reason' ][ 'value' ];
readonly label: string;
} > = [
{
value: 'temporary-deactivation',
label: _x(
'It’s a temporary deactivation',
'text',
'nelio-ab-testing'
),
},
{
value: 'clean-stuff',
label: _x(
'Delete Nelio A/B Testing’s data and deactivate plugin',
'text',
'nelio-ab-testing'
),
},
];
const DEACTIVATION_REASONS: ReadonlyArray< {
readonly value: State[ 'reason' ][ 'value' ];
readonly label: string;
readonly extra?: string;
} > = [
{
value: 'plugin-no-longer-needed',
label: _x( 'I no longer need the plugin', 'text', 'nelio-ab-testing' ),
},
{
value: 'plugin-doesnt-work',
label: _x(
'I couldn’t get the plugin to work',
'text',
'nelio-ab-testing'
),
},
{
value: 'better-plugin-found',
label: _x( 'I found a better plugin', 'text', 'nelio-ab-testing' ),
extra: _x( 'What’s the plugin’s name?', 'text', 'nelio-ab-testing' ),
},
{
value: 'other',
label: _x( 'Other', 'text', 'nelio-ab-testing' ),
extra: _x( 'Please share the reason…', 'user', 'nelio-ab-testing' ),
},
];
function getDescription(
description: string,
siteId: SiteId,
homeUrl: Url,
isSubscribed: boolean
): string {
const div = document.createElement( 'div' );
const br = () => document.createElement( 'br' );
const extra = document.createElement( 'strong' );
extra.textContent = 'Additional Information';
const ul = document.createElement( 'ul' );
const li1 = document.createElement( 'li' );
li1.textContent = `Site ID: ${ siteId }`;
const li2 = document.createElement( 'li' );
li2.textContent = `Site URL: ${ homeUrl }`;
const li3 = document.createElement( 'li' );
li3.textContent = `Subscribed: ${ isSubscribed ? 'yes' : 'no' }`;
ul.appendChild( li1 );
ul.appendChild( li2 );
ul.appendChild( li3 );
trim( description )
.split( /\n+/ )
.forEach( ( t ) => {
const p = document.createTextNode( t );
div.appendChild( p );
div.appendChild( br() );
div.appendChild( br() );
} );
div.appendChild( extra );
div.appendChild( br() );
div.appendChild( ul );
return div.innerHTML;
}