-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAbortionButton.js
66 lines (54 loc) · 1.99 KB
/
AbortionButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import PropTypes from 'prop-types';
import React, {useContext} from 'react';
import {FormattedMessage, useIntl} from 'react-intl';
import {AnalyticsToolsConfigContext} from 'Context';
import {OFButton} from 'components/Button';
import {buildGovMetricUrl} from 'components/analytics/utils';
import useFormContext from 'hooks/useFormContext';
const AbortionButton = ({isAuthenticated, onDestroySession}) => {
const intl = useIntl();
const analyticsToolsConfig = useContext(AnalyticsToolsConfigContext);
const form = useFormContext();
const confirmationMessage = isAuthenticated
? intl.formatMessage({
description: 'log out confirmation prompt',
defaultMessage: 'Are you sure that you want to logout?',
})
: intl.formatMessage({
description: 'Abort confirmation prompt',
defaultMessage:
'Are you sure that you want to abort this submission? You will lose your progress if you continue.',
});
const callback = async event => {
event.preventDefault();
if (!window.confirm(confirmationMessage)) return;
await onDestroySession();
if (analyticsToolsConfig.enableGovmetricAnalytics) {
const govmetricUrl = buildGovMetricUrl(
analyticsToolsConfig.govmetricSourceId,
form.slug,
analyticsToolsConfig.govmetricSecureGuid
);
window.open(govmetricUrl);
}
};
const message = isAuthenticated ? (
<FormattedMessage description="Log out button text" defaultMessage="Log out" />
) : (
<FormattedMessage
description="Button label to abort submission"
defaultMessage="Abort submission"
/>
);
if (!isAuthenticated && !analyticsToolsConfig.enableGovmetricAnalytics) return null;
return (
<OFButton appearance="primary-action-button" hint="danger" name="abort" onClick={callback}>
{message}
</OFButton>
);
};
AbortionButton.propTypes = {
isAuthenticated: PropTypes.bool.isRequired,
onDestroySession: PropTypes.func.isRequired,
};
export default AbortionButton;