{
runWalkthrough();
onRunWalkthrough();
}
: undefined
}
{ ...rest }
/>
>
);
}
return (
'button' === props.mode ? (
) : (
)
}
renderContent={ ( { onToggle } ) => (
{
runWalkthrough();
onToggle();
}
: undefined
}
{ ...props }
/>
) }
/>
);
};
// ============
// HELPER VIEWS
// ============
const Content = ( {
screen,
questions,
onCloseForm,
runWalkthrough,
}: Omit< ContentProps, 'mode' | 'onRunWalkthrough' > & {
readonly runWalkthrough?: () => void;
} ) => {
const [ _, openChatBot ] = usePageAttribute( 'chatbot/status', false );
const [ state, doSetState ] = useState< State >( {
mode: 'questions',
email: '',
description: '',
isTicketSubmitting: false,
submissionStatus: 'none',
} );
const setState = ( a: Partial< State > ) =>
doSetState( { ...state, ...a } );
const { mode } = state;
const { siteId, homeUrl, subscription, apiUrl } = useSelect(
( select ) => ( {
siteId: select( NAB_DATA ).getPluginSetting( 'siteId' ),
homeUrl: select( NAB_DATA ).getPluginSetting( 'homeUrl' ),
subscription: select( NAB_DATA ).getPluginSetting( 'subscription' ),
apiUrl: select( NAB_DATA ).getPluginSetting( 'apiUrl' ),
} ),
[]
);
const submitTicket: ContactFormProps[ 'onSubmit' ] = ( {
email,
description,
success,
error,
} ) => {
success();
apiFetch( {
url: `${ apiUrl }/ticket`,
credentials: 'omit',
method: 'POST',
mode: 'cors',
data: {
email,
description: getDescription(
description,
siteId,
homeUrl,
subscription
),
subject: `I need help in ${ screen }`,
},
} ).then( success, error );
};
if ( 'contact-form' === mode ) {
return (
);
}
return (
{
onCloseForm();
openChatBot( true );
} }
onContactUs={ () =>
setState( {
mode: 'contact-form',
email: '',
description: '',
} )
}
/>
);
};
// =======
// HELPERS
// =======
function getDescription(
description: string,
siteId: SiteId,
homeUrl: Url,
subscription: SubscriptionPlan | false
): 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: ${
false === subscription ? 'no' : subscription
}`;
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;
}