import React, {useEffect, useState} from "react"; import Button from "aws-northstar/components/Button"; import {Alert, Container} from "aws-northstar"; import Webcam from "react-webcam"; import Stack from "aws-northstar/layouts/Stack"; import {API} from "aws-amplify"; import Select from "aws-northstar/components/Select"; import Utils from "../../Utils"; import axios from "axios"; import LoadingIndicator from "aws-northstar/components/LoadingIndicator"; const apiName = "ekycApi"; export interface DocumentTakerProps { sessionId?: string; onDocumentImageTaken: (documentType: string) => void; } interface DocumentType { Code: string; Name: string; } const videoConstraints = { width: 1280, height: 720, facingMode: "user", }; const DocumentTaker = (props: DocumentTakerProps) => { const webcamRef = React.useRef(null); const [previewState, setPreviewState] = useState(true); const [imgSrc, setImgSrc] = React.useState(null); const [isLoading, setIsLoading] = useState(false); const [selectedOption, setSelectedOption] = React.useState(null); //const [documentType,setDocumentType] = useState() const [documentTypes, setDocumentTypes] = React.useState([]); const [imgData, setImgData] = useState(null); const [options, setOptions] = React.useState([]); const [error, setError] = React.useState(); const [documentPreview, setDocumentPreview] = useState(null); const [uploadedFile, setUploadedFile] = React.useState(null); const s3Key = "document.jpg"; useEffect(() => { async function getDocumentTypes() { // Get the document types const docTypes = await API.get(apiName, "/api/document/doctypes", {}); setDocumentTypes(docTypes); console.log(docTypes); const selectOptions = docTypes.map((dt: DocumentType) => { return {label: dt.Name, value: dt.Code}; }); console.log(`Document type options - ${JSON.stringify(selectOptions)}`); setOptions(selectOptions); } getDocumentTypes(); }, []); const useUploadedFile = (element: any) => { if (!selectedOption) { setError("Document type not selected."); return; } if (uploadedFile === null) { setError("No file selected."); return; } setIsLoading(true); console.log(`Using file ${uploadedFile}`); const documentType = documentTypes.find( (o) => o.Code === selectedOption.value ); console.log(`Document type - ${documentType.Code}`); // call api here getPresignedUrl().then((url) => { axios .put(url, uploadedFile, { headers: { "Content-Type": uploadedFile.type, }, }) .then(() => { API.post(apiName, "/api/session/document", { queryStringParameters: { sessionId: props.sessionId, s3Key: s3Key, expectedDocumentType: documentType.Code, }, }) .then(() => { setIsLoading(false); props.onDocumentImageTaken(documentType.Code); }) .catch((err) => { console.log(err.response.data.error); if (err.response) setError(err.response.data.error); else setError("An error occurred uploading the selfie."); retake(); }); }); }); }; const capture = React.useCallback(() => { const imageSrc = webcamRef.current.getScreenshot(); setImgSrc(imageSrc); setPreviewState(false); setError(null); }, [webcamRef, setImgSrc]); const retake = React.useCallback(() => { setPreviewState(true); setError(null); setIsLoading(false); }, [webcamRef]); const uploadDocument = async () => { if (!selectedOption) { setError("Document type not selected."); return; } setIsLoading(true); const documentType = documentTypes.find( (o) => o.Code === selectedOption.value ); console.log(`Document type - ${documentType.Code}`); // Upload getPresignedUrl().then((url) => { Utils.uploadFile(url, imgSrc, "image/*").then(() => { API.post(apiName, "/api/session/document", { queryStringParameters: { sessionId: props.sessionId, s3Key: s3Key, expectedDocumentType: documentType.Code, }, }).then(() => { setIsLoading(false); props.onDocumentImageTaken(documentType.Code); }); }); }); }; const onFileUploadChanged = (event: any) => { if (event.target.files[0]) { setUploadedFile(event.target.files[0]); console.log("picture: ", event.target.files); setDocumentPreview(event.target.files[0]); const reader = new FileReader(); reader.addEventListener("load", () => { setImgData(reader.result); }); reader.readAsDataURL(event.target.files[0]); } }; const onChange = (event: any) => setSelectedOption(options.find((o) => o.value === event.target.value)); const onChangeDocumentType = React.useCallback( (event: any) => { const ssOption = options.find((o) => o.value === event.target.value); console.log(`Setting document type to ${event.target.value}`); setSelectedOption(ssOption); }, [selectedOption] ); const getPresignedUrl = async () => { const response = await API.get(apiName, "/api/session/url", { queryStringParameters: { sessionId: props.sessionId, s3Key: s3Key, }, }); return response; }; return (
); }; export default DocumentTaker;