| 1 |
import React from 'react'; |
| 2 |
import UIButton from '../UIComponents/UIButton'; |
| 3 |
import UIContainer from '../UIComponents/UIContainer'; |
| 4 |
import HubspotWrapper from './HubspotWrapper'; |
| 5 |
import { adminUrl, redirectNonce } from '../../constants/leadinConfig'; |
| 6 |
import { pluginPath } from '../../constants/leadinConfig'; |
| 7 |
import { __ } from '@wordpress/i18n'; |
| 8 |
|
| 9 |
interface IErrorHandlerProps { |
| 10 |
status: number; |
| 11 |
resetErrorState?: React.MouseEventHandler<HTMLButtonElement>; |
| 12 |
errorInfo?: { |
| 13 |
header: string; |
| 14 |
message: string; |
| 15 |
action: string; |
| 16 |
}; |
| 17 |
} |
| 18 |
|
| 19 |
function redirectToPlugin() { |
| 20 |
window.location.href = `${adminUrl}admin.php?page=leadin&leadin_expired=${redirectNonce}`; |
| 21 |
} |
| 22 |
|
| 23 |
export default function ErrorHandler({ |
| 24 |
status, |
| 25 |
resetErrorState, |
| 26 |
errorInfo = { header: '', message: '', action: '' }, |
| 27 |
}: IErrorHandlerProps) { |
| 28 |
const isUnauthorized = status === 401 || status === 403; |
| 29 |
const errorHeader = isUnauthorized |
| 30 |
? __("Your plugin isn't authorized", 'leadin') |
| 31 |
: errorInfo.header; |
| 32 |
const errorMessage = isUnauthorized |
| 33 |
? __('Reauthorize your plugin to access your free HubSpot tools', 'leadin') |
| 34 |
: errorInfo.message; |
| 35 |
|
| 36 |
return ( |
| 37 |
<HubspotWrapper pluginPath={pluginPath}> |
| 38 |
<UIContainer textAlign="center"> |
| 39 |
<h4>{errorHeader}</h4> |
| 40 |
<p> |
| 41 |
<b>{errorMessage}</b> |
| 42 |
</p> |
| 43 |
{isUnauthorized ? ( |
| 44 |
<UIButton data-test-id="authorize-button" onClick={redirectToPlugin}> |
| 45 |
{__('Go to plugin', 'leadin')} |
| 46 |
</UIButton> |
| 47 |
) : ( |
| 48 |
<UIButton data-test-id="retry-button" onClick={resetErrorState}> |
| 49 |
{errorInfo.action} |
| 50 |
</UIButton> |
| 51 |
)} |
| 52 |
</UIContainer> |
| 53 |
</HubspotWrapper> |
| 54 |
); |
| 55 |
} |
| 56 |
|