forked from kuschanton/kyc-embed-react-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKycEmbedComponent.tsx
77 lines (65 loc) · 2.14 KB
/
KycEmbedComponent.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
import React, {useEffect, useState} from 'react'
import './App.css'
import {TwilioComplianceEmbed} from 'twilio-compliance-embed'
import {Spinner} from '@twilio-paste/core/spinner'
import {Box} from '@twilio-paste/core'
type InboundEvent = {
data: {
inquiryId: string,
inquirySessionToken: string
},
origin: string
}
type OutboundEvent = 'cancel' | 'complete' | 'ready' | 'error'
export const KycEmbedComponent = () => {
const [inquiryId, setInquiryId] = useState<string | null>(null)
const [inquirySessionToken, setInquirySessionToken] = useState<string | null>(null)
const isLoading = !inquiryId && !inquirySessionToken
useEffect(() => {
const receiveMessage = (event: InboundEvent) => {
console.log('>>> message received', event)
// IMPORTANT: The verification below should be in place for your production code
// if (event.origin !== 'http://localhost:63342') return
if (!!event.data.inquiryId && !!event.data.inquirySessionToken) {
setInquiryId(event.data.inquiryId)
setInquirySessionToken(event.data.inquirySessionToken)
}
}
window.addEventListener('message', receiveMessage, false)
return () => {
window.removeEventListener('message', receiveMessage)
}
}, [])
const postEvent = (event: OutboundEvent) => {
window.parent.postMessage({
event: event,
}, '*') // TODO: replace this with your exact origin for security
}
return isLoading ? (
<Box top='50%' left='50%' position='fixed'>
<Spinner size='sizeIcon110' decorative={false} title='Loading'/>
</Box>
) :
(<div className='App'>
<TwilioComplianceEmbed
inquiryId={inquiryId!}
inquirySessionToken={inquirySessionToken!}
onCancel={() => {
postEvent('cancel')
console.log('>>> cancel')
}}
onComplete={() => {
console.log('>>> complete')
postEvent('complete')
}}
onReady={() => {
console.log('>>> ready')
postEvent('ready')
}}
onError={() => {
console.log('>>> error')
postEvent('error')
}}
/>
</div>)
}