forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.native.tsx
106 lines (98 loc) · 4.54 KB
/
index.native.tsx
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React, {useRef, useState} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {WebView} from 'react-native-webview';
import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView';
import Button from '@components/Button';
import ConfirmModal from '@components/ConfirmModal';
import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Modal from '@components/Modal';
import useLocalize from '@hooks/useLocalize';
import {removePolicyConnection} from '@libs/actions/connections';
import {getQuickBooksOnlineSetupLink} from '@libs/actions/connections/QuickBooksOnline';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Session} from '@src/types/onyx';
import type {ConnectToQuickbooksOnlineButtonProps} from './types';
type ConnectToQuickbooksOnlineButtonOnyxProps = {
/** Session info for the currently logged in user. */
session: OnyxEntry<Session>;
};
const renderLoading = () => <FullScreenLoadingIndicator />;
function ConnectToQuickbooksOnlineButton({
policyID,
session,
shouldDisconnectIntegrationBeforeConnecting,
integrationToDisconnect,
}: ConnectToQuickbooksOnlineButtonProps & ConnectToQuickbooksOnlineButtonOnyxProps) {
const {translate} = useLocalize();
const webViewRef = useRef<WebView>(null);
const [isWebViewOpen, setWebViewOpen] = useState(false);
const authToken = session?.authToken ?? null;
const [isDisconnectModalOpen, setIsDisconnectModalOpen] = useState(false);
return (
<>
<Button
onPress={() => {
if (shouldDisconnectIntegrationBeforeConnecting && integrationToDisconnect) {
setIsDisconnectModalOpen(true);
return;
}
setWebViewOpen(true);
}}
text={translate('workspace.accounting.setup')}
small
/>
{shouldDisconnectIntegrationBeforeConnecting && integrationToDisconnect && isDisconnectModalOpen && (
<ConfirmModal
title={translate('workspace.accounting.disconnectTitle', CONST.POLICY.CONNECTIONS.NAME.XERO)}
onConfirm={() => {
removePolicyConnection(policyID, integrationToDisconnect);
setIsDisconnectModalOpen(false);
setWebViewOpen(true);
}}
isVisible
onCancel={() => setIsDisconnectModalOpen(false)}
prompt={translate('workspace.accounting.disconnectPrompt', CONST.POLICY.CONNECTIONS.NAME.QBO)}
confirmText={translate('workspace.accounting.disconnect')}
cancelText={translate('common.cancel')}
danger
/>
)}
{isWebViewOpen && (
<Modal
onClose={() => setWebViewOpen(false)}
fullscreen
isVisible
type={CONST.MODAL.MODAL_TYPE.CENTERED}
>
<HeaderWithBackButton
title={translate('workspace.accounting.title')}
onBackButtonPress={() => setWebViewOpen(false)}
/>
<FullPageOfflineBlockingView>
<WebView
ref={webViewRef}
source={{
uri: getQuickBooksOnlineSetupLink(policyID),
headers: {
Cookie: `authToken=${authToken}`,
},
}}
incognito // 'incognito' prop required for Android, issue here https://github.com/react-native-webview/react-native-webview/issues/1352
startInLoadingState
renderLoading={renderLoading}
/>
</FullPageOfflineBlockingView>
</Modal>
)}
</>
);
}
ConnectToQuickbooksOnlineButton.displayName = 'ConnectToQuickbooksOnlineButton';
export default withOnyx<ConnectToQuickbooksOnlineButtonProps & ConnectToQuickbooksOnlineButtonOnyxProps, ConnectToQuickbooksOnlineButtonOnyxProps>({
session: {
key: ONYXKEYS.SESSION,
},
})(ConnectToQuickbooksOnlineButton);