/
Persona.tsx
114 lines (103 loc) · 3.7 KB
/
Persona.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
107
108
109
110
111
112
113
114
import * as React from 'react'
import { useCallback, useState } from 'react'
import { useAsync } from 'react-async-hook'
import { useTranslation } from 'react-i18next'
import { Fields, Inquiry } from 'react-native-persona'
import { KycStatus } from 'src/account/reducer'
import { showError } from 'src/alert/actions'
import { CICOEvents } from 'src/analytics/Events'
import ValoraAnalytics from 'src/analytics/ValoraAnalytics'
import { ErrorMessages } from 'src/app/ErrorMessages'
import Button, { BtnSizes, BtnTypes } from 'src/components/Button'
import { getPersonaTemplateId } from 'src/firebase/firebase'
import { createPersonaAccount, verifyWalletAddress } from 'src/in-house-liquidity'
import { useDispatch, useSelector } from 'src/redux/hooks'
import Logger from 'src/utils/Logger'
import networkConfig from 'src/web3/networkConfig'
import { walletAddressSelector } from 'src/web3/selectors'
// eslint-disable-next-line import/no-relative-packages
import pjson from '../../package.json'
const TAG = 'PERSONA'
export interface Props {
kycStatus: KycStatus | undefined
disabled: boolean
text?: string | undefined
onPress?: () => any
onCanceled?: () => any
onError?: () => any
onSuccess?: () => any
}
enum Status {
completed = 'completed',
failed = 'failed',
}
const Persona = ({ kycStatus, text, onCanceled, onError, onPress, onSuccess, disabled }: Props) => {
const { t } = useTranslation()
const [personaAccountCreated, setPersonaAccountCreated] = useState(
kycStatus !== undefined && kycStatus !== KycStatus.NotCreated
)
const walletAddress = useSelector(walletAddressSelector)
const dispatch = useDispatch()
const templateIdResponse = useAsync(async () => getPersonaTemplateId(), [])
const templateId = templateIdResponse.result
const launchPersonaInquiry = useCallback(() => {
if (typeof templateId !== 'string') {
Logger.error(TAG, `Attempted to initiate Persona with invalid templateId: ${templateId}`)
return
}
if (!walletAddress) {
Logger.warn(TAG, "Can't render Persona because walletAddress is null")
return
}
onPress?.()
Inquiry.fromTemplate(templateId)
.referenceId(walletAddress)
.environment(networkConfig.personaEnvironment)
.iosTheme(pjson.persona.iosTheme)
.onComplete((inquiryId: string, status: string, _fields: Fields) => {
if (status === Status.failed) {
onError?.()
ValoraAnalytics.track(CICOEvents.persona_kyc_failed)
Logger.error(TAG, `Inquiry failed for ${inquiryId}`)
} else {
onSuccess?.()
ValoraAnalytics.track(CICOEvents.persona_kyc_success)
Logger.info(TAG, `Inquiry completed for ${inquiryId}`)
}
})
.onCanceled(() => {
onCanceled?.()
ValoraAnalytics.track(CICOEvents.persona_kyc_cancel)
Logger.info(TAG, 'Inquiry is canceled by the user.')
})
.onError((error: Error) => {
onError?.()
ValoraAnalytics.track(CICOEvents.persona_kyc_error)
Logger.error(TAG, `Error: ${error.message}`)
})
.build()
.start()
}, [templateId])
useAsync(async () => {
if (!personaAccountCreated) {
try {
await createPersonaAccount(verifyWalletAddress({ walletAddress }))
setPersonaAccountCreated(true)
} catch (error) {
Logger.warn(TAG, error)
dispatch(showError(ErrorMessages.PERSONA_ACCOUNT_ENDPOINT_FAIL))
}
}
}, [])
return (
<Button
onPress={launchPersonaInquiry}
text={text || t('raiseLimitBegin')}
type={BtnTypes.PRIMARY}
size={BtnSizes.MEDIUM}
testID="PersonaButton"
disabled={!personaAccountCreated || !templateId || disabled}
/>
)
}
export default Persona