/
index.jsx
242 lines (223 loc) · 7.83 KB
/
index.jsx
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import React, { useContext, useState } from 'react'
import PropTypes from 'prop-types'
import cx from 'classnames'
import { useClient } from 'cozy-client'
import { Account } from 'cozy-doctypes'
import { useVaultClient, CozyUtils } from 'cozy-keys-lib'
import Button from 'cozy-ui/transpiled/react/Button'
import Spinner from 'cozy-ui/transpiled/react/Spinner'
import palette from 'cozy-ui/transpiled/react/palette'
import Icon from 'cozy-ui/transpiled/react/Icon'
import useBreakpoints from 'cozy-ui/transpiled/react/hooks/useBreakpoints'
import ListItem from 'cozy-ui/transpiled/react/MuiCozyTheme/ListItem'
import ListItemIcon from 'cozy-ui/transpiled/react/MuiCozyTheme/ListItemIcon'
import NavigationList, {
NavigationListSection,
NavigationListHeader
} from 'cozy-ui/transpiled/react/NavigationList'
import { ConfirmDialog } from 'cozy-ui/transpiled/react/CozyDialogs'
import { useI18n } from 'cozy-ui/transpiled/react/I18n'
import KeyIcon from 'cozy-ui/transpiled/react/Icons/Key'
import RightIcon from 'cozy-ui/transpiled/react/Icons/Right'
import UnlinkIcon from 'cozy-ui/transpiled/react/Icons/Unlink'
import ListItemText from 'cozy-ui/transpiled/react/ListItemText'
import ListItemSecondaryAction from 'cozy-ui/transpiled/react/MuiCozyTheme/ListItemSecondaryAction'
import Alerter from 'cozy-ui/transpiled/react/Alerter'
import { useVaultUnlockContext } from 'cozy-keys-lib'
import { deleteAccount } from '../../../connections/accounts'
import { unshareCipher } from '../../../models/cipherUtils'
import { findKonnectorPolicy } from '../../../konnector-policies'
import useSafeState from '../../useSafeState'
import { MountPointContext } from '../../MountPointContext'
import { useTrackPage, useTracker } from '../../hoc/tracking'
import { ContractsForAccount } from './Contracts'
import {
intentsApiProptype,
innerAccountModalOverridesProptype
} from '../../../helpers/proptypes'
const tabMobileNavListStyle = { borderTop: 'none' }
const ConfirmationDialog = ({ onConfirm, onCancel }) => {
const { t } = useI18n()
return (
<ConfirmDialog
open
title={t('modal.deleteAccount.title')}
content={t('modal.deleteAccount.description')}
onClose={onCancel}
actions={
<>
<Button
theme="secondary"
label={t('modal.deleteAccount.cancel')}
onClick={onCancel}
/>
<Button
theme="danger"
label={t('modal.deleteAccount.confirm')}
onClick={onConfirm}
/>
</>
}
/>
)
}
const ConfigurationTab = ({
konnector,
account,
addAccount,
onAccountDeleted,
showNewAccountButton,
flow,
intentsApi,
innerAccountModalOverrides
}) => {
const { t } = useI18n()
const { isMobile } = useBreakpoints()
const { replaceHistory } = useContext(MountPointContext)
const client = useClient()
const vaultClient = useVaultClient()
const [deleting, setDeleting] = useSafeState(false)
const [requestingDeletion, setRequestDeletion] = useState(false)
const tracker = useTracker()
const { running } = flow.getState()
const { showUnlockForm } = useVaultUnlockContext()
useTrackPage('configuration')
const handleDeleteConfirm = async () => {
setRequestDeletion(false)
const konnectorPolicy = findKonnectorPolicy(konnector)
if (konnectorPolicy.saveInVault) {
showUnlockForm({
closable: true,
onUnlock: handleUnlockForDeletion,
addCheckShouldUnlock: () => CozyUtils.checkHasInstalledExtension(client)
})
} else {
await handleDeleteAccount()
}
}
const handleDeleteAccount = async () => {
setDeleting(true)
try {
onAccountDeleted(account)
await deleteAccount(client, account)
Alerter.success(t('modal.updateAccount.delete-account-success'))
tracker.trackEvent({
name: 'compte_bancaire_supprime',
connectorSlug: account.account_type
})
} catch (error) {
// eslint-disable-next-line no-console
console.warn('Error while deleting account', error)
Alerter.error(t('modal.updateAccount.delete-account-error'))
} finally {
setDeleting(false)
}
}
const handleCancelDeleteRequest = () => {
setRequestDeletion(false)
}
const handleDeleteRequest = () => {
setRequestDeletion(true)
}
const handleUnlockForDeletion = async () => {
await handleDeleteAccount()
await unshareCipher(vaultClient, account)
}
const konnectorPolicy = findKonnectorPolicy(konnector)
return (
<div className={isMobile ? '' : 'u-pt-1 u-pb-1-half'}>
{/**
* Use an extra div with padding instead of setting margins on TriggerErrorInfo since
* the offsetHeight of the parent does not take into account margins; slide content was
* cropped since SwipeableViews uses the offsetHeight of the first slide children when
* computing the height of the slide wrapper.
*/}
<NavigationList style={isMobile ? tabMobileNavListStyle : null}>
<NavigationListHeader>
{t('modal.updateAccount.general-subheader')}
</NavigationListHeader>
<NavigationListSection>
{konnector.oauth || konnectorPolicy.isBIWebView ? null : (
<ListItem
button
divider
onClick={() => replaceHistory(`/accounts/${account._id}/edit`)}
>
<ListItemIcon>
<Icon icon={KeyIcon} color={palette['slateGrey']} />
</ListItemIcon>
<ListItemText
primary={t('modal.updateAccount.identifiers')}
secondary={Account.getAccountName(account)}
/>
<ListItemSecondaryAction>
<div>
{running && <Spinner />}
<Icon
className="u-mr-1"
icon={RightIcon}
color={palette['coolGrey']}
/>
</div>
</ListItemSecondaryAction>
</ListItem>
)}
<ListItem button onClick={handleDeleteRequest}>
<ListItemIcon>
<Icon icon={UnlinkIcon} className="u-error" />
</ListItemIcon>
<ListItemText
primary={
<span className="u-error">
{innerAccountModalOverrides?.deleteAccountLabel
? innerAccountModalOverrides.deleteAccountLabel
: t('accountForm.disconnect.button')}
</span>
}
/>
<ListItemSecondaryAction>
{deleting && <Spinner />}
</ListItemSecondaryAction>
</ListItem>
{requestingDeletion ? (
<ConfirmationDialog
onCancel={handleCancelDeleteRequest}
onConfirm={handleDeleteConfirm}
/>
) : null}
</NavigationListSection>
<ContractsForAccount
konnector={konnector}
account={account}
intentsApi={intentsApi}
innerAccountModalOverrides={innerAccountModalOverrides}
onAccountDeleted={onAccountDeleted}
/>
</NavigationList>
{showNewAccountButton ? (
<div
className={cx('u-ta-right u-mt-1', isMobile ? 'u-ph-1 u-pb-1' : null)}
>
<Button
extension={isMobile ? 'full' : null}
onClick={addAccount}
className="u-ml-0"
label={t('modal.addAccount.button')}
theme="ghost"
/>
</div>
) : null}
</div>
)
}
ConfigurationTab.propTypes = {
konnector: PropTypes.object.isRequired,
account: PropTypes.object.isRequired,
error: PropTypes.object,
flow: PropTypes.object,
addAccount: PropTypes.func.isRequired,
onAccountDeleted: PropTypes.func.isRequired,
intentsApi: intentsApiProptype,
innerAccountModalOverrides: innerAccountModalOverridesProptype
}
export default ConfigurationTab