import {saveAs} from 'file-saver'; import React, {FormEvent, useEffect, useRef, useState} from "react"; import {BrowserQRCodeReader, IScannerControls} from "@zxing/browser"; import {Result} from "@zxing/library"; import {useTranslation} from 'next-i18next'; import Link from 'next/link'; import Card from "./Card"; import Alert from "./Alert"; import Check from './Check'; import {PayloadBody} from "../src/payload"; import {getPayloadBodyFromFile} from "../src/process"; import {PassData} from "../src/pass"; import {Photo} from "../src/photo"; import {COLORS} from "../src/colors"; import Colors from './Colors'; import {isIOS, isMacOs, isSafari} from 'react-device-detect'; function Form(): JSX.Element { const {t} = useTranslation(['index', 'errors', 'common']); // Whether camera is open or not const [isCameraOpen, setIsCameraOpen] = useState(false); // Currently selected color const [selectedColor, setSelectedColor] = useState(COLORS.WHITE); // Global camera controls const [globalControls, setGlobalControls] = useState(undefined); // Currently selected QR Code / File. Only one of them is set. const [qrCode, setQrCode] = useState(undefined); const [file, setFile] = useState(undefined); const [errorMessage, _setErrorMessage] = useState(undefined); const [loading, setLoading] = useState(false); // Check if there is a translation and replace message accordingly const setErrorMessage = (message: string) => { if (message == undefined) { _setErrorMessage(undefined); return; } const translation = t('errors:'.concat(message)); _setErrorMessage(translation !== message ? translation : message); }; // File Input ref const inputFile = useRef(undefined) // Add event listener to listen for file change events useEffect(() => { if (inputFile && inputFile.current) { inputFile.current.addEventListener('input', () => { let selectedFile = inputFile.current.files[0]; if (selectedFile !== undefined) { setQrCode(undefined); setFile(selectedFile); } }); } // checkBrowserType(); }, [inputFile]) // Show file Dialog async function showFileDialog() { inputFile.current.click(); } async function gotoOntarioHealth() { window.location.href = 'https://covid19.ontariohealth.ca'; } // Hide camera view async function hideCameraView() { if (globalControls !== undefined) { globalControls.stop(); } setIsCameraOpen(false); } // Show camera view async function showCameraView() { // Create new QR Code Reader const codeReader = new BrowserQRCodeReader(); // Needs to be called before any camera can be accessed let deviceList: MediaDeviceInfo[]; try { deviceList = await BrowserQRCodeReader.listVideoInputDevices(); } catch (e) { setErrorMessage('noCameraAccess'); return; } // Check if camera device is present if (deviceList.length == 0) { setErrorMessage("noCameraFound"); return; } // Get preview Element to show camera stream const previewElem: HTMLVideoElement = document.querySelector('#cameraPreview'); // Set Global controls setGlobalControls( // Start decoding from video device await codeReader.decodeFromVideoDevice(undefined, previewElem, (result, error, controls) => { if (result !== undefined) { setQrCode(result); setFile(undefined); controls.stop(); // Reset setGlobalControls(undefined); setIsCameraOpen(false); } if (error !== undefined) { setErrorMessage(error.message); } } ) ); setIsCameraOpen(true); } // Add Pass to wallet async function addToWallet(event: FormEvent) { event.preventDefault(); setLoading(true); if (navigator.userAgent.match('CriOS')) { setErrorMessage('safariSupportOnly'); setLoading(false); return; } if (!file && !qrCode) { setErrorMessage('noFileOrQrCode') setLoading(false); return; } const color = selectedColor; let payloadBody: PayloadBody; try { if (file) { console.log('> generatePass'); payloadBody = await getPayloadBodyFromFile(file, color); let pass = await PassData.generatePass(payloadBody); const passBlob = new Blob([pass], {type: "application/vnd.apple.pkpass"}); saveAs(passBlob, 'covid.pkpass'); setLoading(false); } } catch (e) { setErrorMessage(e.message); setLoading(false); } } async function saveAsPhoto() { setLoading(true); if (!file && !qrCode) { setErrorMessage('noFileOrQrCode') setLoading(false); return; } let payloadBody: PayloadBody; try { payloadBody = await getPayloadBodyFromFile(file, null); let photoBlob = await Photo.generatePass(payloadBody); saveAs(photoBlob, 'pass.png'); // need to clean up const qrcodeElement = document.getElementById('qrcode'); const svg = qrcodeElement.firstChild; qrcodeElement.removeChild(svg); const body = document.getElementById('pass-image'); body.hidden = true; setLoading(false); } catch (e) { setErrorMessage(e.message); setLoading(false); } } async function checkBrowserType() { if (isMacOs || (isIOS && isSafari)) { document.getElementById('download').hidden = false; } else { document.getElementById('download').hidden = true; } } return (

{t('index:visit')}  {t('index:ontarioHealth')}   {t('index:downloadSignedPDF')}

}/>

{t('index:selectCertificateDescription')}

{(qrCode || file) &&
{ qrCode && t('index:foundQrCode') } { file && file.name }
} }/>

{t('index:dataPrivacyDescription')} {/* {t('index:privacyPolicy')} . */}

    {/* */}
    
}/> { errorMessage && setErrorMessage(undefined)}/> } ) } export default Form;