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
|
/* eslint-disable multiline-ternary */
import React, { useState } from 'react'
import Webcam from 'react-webcam'
import Header from '../../../components/Header/Header'
import styles from './DocumentScanner.module.css'
import {
Button,
Grid,
Typography,
StepLabel,
Step,
Stepper,
Box
} from '@mui/material'
import SubmitButton from '../../../components/SubmitButton/SubmitButton'
import { useTranslation } from 'react-i18next'
import { userContext } from '../../../context/User'
const DocumentScanner = () => {
const { userData, oriUserData, setUserData } = userContext()
const { t } = useTranslation()
// JSON.stringify(oriUserData?.address) ===
let steps
// use conditional statements to compare userData and oriUserData to determine the steps
// 1st case: if only address is changed, then step=['POA']
// 2nd case: if only either name or gender is changed, then step=['POI']
// 3rd case: if only dob is changed, then step=['DOB']
// 4th case: If only address and name or gender is changed, then step=['POA', 'POI']
// 5th case: If only address and dob is changed, then step=['POA', 'DOB']
// 6th case: If only name or gender and dob is changed, then step=['POI', 'DOB']
// 7th case: If everything is changed, then step=['POA', 'POI', 'DOB']
if (
(userData.address !== oriUserData.address &&
userData.dob !== oriUserData.dob &&
userData.name !== oriUserData.name) ||
userData.gender !== oriUserData.gender
) {
steps = ['POA', 'POI', 'DOB']
} else {
if (userData.address !== oriUserData.address) {
if (
userData.name !== oriUserData.name ||
userData.gender !== oriUserData.gender
) {
steps = ['POA', 'POI']
} else if (userData.dob !== oriUserData.dob) {
steps = ['POA', 'DOB']
} else {
steps = ['POA']
}
} else if (
userData.name !== oriUserData.name ||
userData.gender !== oriUserData.gender
) {
if (userData.dob !== oriUserData.dob) {
steps = ['POI', 'DOB']
} else {
steps = ['POI']
}
} else if (userData.dob !== oriUserData.dob) {
steps = ['DOB']
} else {
steps = []
}
}
const [documents, setDocuments] = useState({ POI: '', POA: '', DOB: '' })
const [activeStep, setActiveStep] = React.useState(0)
const [doccu] = useState({ POI: '', POA: '', DOB: '' })
console.log(documents)
const webcamRef = React.useRef(null)
const capture = React.useCallback((doc) => {
const imageSrc = webcamRef.current.getScreenshot()
doccu[doc] = imageSrc
setDocuments({
...documents,
POI: doccu.POI,
POA: doccu.POA,
DOB: doccu.DOB
})
})
const handleNext = () => {
if (activeStep === steps.length - 1) {
setUserData({ ...userData, documents: documents })
}
setActiveStep((prevActiveStep) => prevActiveStep + 1)
}
const handleBack = () => {
setActiveStep((prevActiveStep) => prevActiveStep - 1)
}
const WebcamComponent = ({ doc }) => {
return (
<>
<div className={styles.card__container}>
{documents[doc] === '' ? (
<Webcam
audio={false}
height={400}
ref={webcamRef}
screenshotFormat="image/jpeg"
width={600}
videoConstraints={{
height: 400,
width: 600,
facingMode: 'user'
}}
/>
) : (
<img src={documents[doc]} />
)}
</div>
<Grid container columnSpacing={10} justifyContent="center">
<Grid item>
<Button
color="primary"
size="large"
type="submit"
variant="contained"
onClick={(e) => {
e.preventDefault()
capture(doc)
}}
>
{t('SCAN')}
</Button>
</Grid>
<Grid item>
<Button
color="primary"
size="large"
type="submit"
variant="contained"
onClick={(e) => {
e.preventDefault()
doccu[doc] = ''
setDocuments({
...documents,
POI: doccu.POI,
POA: doccu.POA,
DOB: doccu.DOB
})
}}
>
{t('RESET')}
</Button>
</Grid>
</Grid>
<br></br>
<div>
<Grid container justifyContent="center">
<Typography align="center">
{t('KINDLY_CLICK_THE_PICTURE_OF_YOUR_DOCUMENTS')}
</Typography>
</Grid>
</div>
</>
)
}
return (
<>
<Header subheading={t('ENROLLMENT')} />
<SubmitButton />
<Box sx={{ width: '100%' }}>
<Stepper activeStep={activeStep}>
{steps.map((label, index) => {
const stepProps = {}
const labelProps = {}
return (
<Step key={label} {...stepProps}>
<StepLabel {...labelProps}>{label}</StepLabel>
</Step>
)
})}
</Stepper>
{activeStep === steps.length ? (
<React.Fragment>
<Typography sx={{ mt: 2, mb: 1 }}>
{t('ALL_STEPS_COMPLETED')}
</Typography>
<Box sx={{ display: 'flex', flexDirection: 'row', pt: 2 }}>
<Box sx={{ flex: '1 1 auto' }} />
</Box>
</React.Fragment>
) : (
<React.Fragment>
{activeStep === 0 ? (
<WebcamComponent doc="POI" />
) : activeStep === 1 ? (
<WebcamComponent doc="POA" />
) : (
activeStep === 2 && <WebcamComponent doc="DOB" />
)}
<Box sx={{ display: 'flex', flexDirection: 'row', pt: 2 }}>
<Button
color="inherit"
disabled={activeStep === 0}
onClick={handleBack}
sx={{ mr: 1 }}
>
{t('BACK')}
</Button>
<Box sx={{ flex: '1 1 auto' }} />
<Button onClick={handleNext}>
{activeStep === steps.length - 1 ? t('FINISH') : t('NEXT')}
</Button>
</Box>
</React.Fragment>
)}
</Box>
</>
)
}
export default DocumentScanner
|