0.2.1 • Published 2 years ago

@ar-identification/react v0.2.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@ar-identification/react

An scanner for Argentinian DNI's.

Installation

yarn add @zxing/browser@^0.1.1 @zxing/library@^0.19.1 @ar-identification/decode@0.1.1 @ar-identification/react@0.2.1

Usage/Examples

The library includes a component called Scanner which scans only the front of the DNI at the moment. Also has the ability to read and validate QR codes.

Props

proptype signaturedescription
onScanSuccess(event: SuccessEvent) => voidcallback triggered on a successful scan
onScanError(error: Error) => voidcallback triggered on any error
classNamestring \| undefinedan optional string containing css classes
allowQRbooleanan optional boolean to allow QR codes reading
QRValidationFn(data:string) => Promise<boolean>an optional async fn to validate the QR data

The DNI Object

proptype signaturedescription
namestringNombre / Name
surnamestringApellido / Surname
dateOfBirthDateFecha de nacimiento / Date of birth
dateOfIssueDateFecha de emisión / Date of issue
dateOfExpiryDateFecha de vencimiento / Date of expiry
copystringEjemplar
sex"MALE" \| "FEMALE" \| "NON_BINARY"Sexo / Sex
dnistringDocumento / Document
cuilstringCUIL
idstringTramite N° / Of. Ident

Implementation

import React, { useState } from "react";
import { DNI } from "@ar-identification/decode";
import { Scanner, ScannerProps } from "@ar-identification/react";

const App = () => {
  const [dni, setDni] = useState<DNI>();
  const [error, setError] = useState<string>();
  const [qr, setQR] = useState<string>();
  const handleOnScanError = (e: Error) => setError(e.message);

  const handleRetry = () => {
    setError(undefined);
    setDni(undefined);
    setQR(undefined);
  };

  const handleSuccess: ScannerProps["onScanSuccess"] = ({ type, data }) => {
    if (type === "DNI") {
      setDni(data as DNI);
    } else {
      setQR(data as string);
    }
  };

  const qrValidator = async (data: string) => {
    try {
      const parsedJSON = JSON.parse(data) as any;
      return !!parsedJSON.test;
    } catch (e) {
      console.log(e);
      return false;
    }
  };

  return (
    <div>
      {error && <p>{error}</p>}
      {dni && <pre style={{ fontFamily: "monospace" }}>{JSON.stringify(dni, null, 2)}</pre>}
      {qr && <pre style={{ fontFamily: "monospace" }}>{JSON.stringify(JSON.parse(qr), null, 2)}</pre>}
      {!dni && !error && !qr && (
        <Scanner onScanSuccess={handleSuccess} onScanError={handleOnScanError} allowQR QRValidationFn={qrValidator} />
      )}
      {(error || dni || qr) && <button onClick={handleRetry}>Retry</button>}
    </div>
  );
};

export default App;