import AsyncStorage from '@react-native-community/async-storage'; import {format} from 'date-fns'; import * as Print from 'expo-print'; import React, {ReactElement, useEffect, useState} from 'react'; import {Text, View} from 'react-native'; import {Button, Title} from 'react-native-paper'; import QRCode from 'react-native-qrcode-svg'; import {BarCodeProps, ButtonProps, PrintingProps} from '../models/ElementProps'; import {Sample} from '../models/Sample'; import {colors, styles} from './Styles'; const qrcode = require('qrcode'); enum PrintStatus { SAVING = 'SAVING', PRINTING = 'PRINTING', DONE = 'DONE', } const _save = (props: PrintingProps): Promise => { const storageVal: Sample = { id: props.id, barcodeId: props.barCodeId, createdAt: props.date, locationId: props.location, }; return AsyncStorage.setItem(props.id, JSON.stringify(storageVal)); } const _print = async (props: PrintingProps): Promise => { const svgString = await qrcode.toString(props.id, { width: 72, // 20mm height: 72, margin: 10, errorCorrectionLevel: 'high', type: 'svg', color: { light: '#ffffff00', dark: '#000', } }); return Print.printAsync({ html: `
${svgString}
${format(props.date, 'yyyy-MM-dd')}
T
${format(props.date, 'HH')}
${format(props.date, 'mm')}
L
${props.location.slice(0, 2)}
${props.location.slice(2)}
#${props.barCodeId}
`, }); } export const PrintButton = (props: ButtonProps): ReactElement => { return ; } export const PrintingMessage = (props: PrintingProps): ReactElement => { const [statusStr, setStatusStr] = useState('Saving data...'); const [printStatus, setPrintStatus] = useState(PrintStatus.SAVING); useEffect(() => { _save(props).finally(() => { setPrintStatus(PrintStatus.PRINTING); setStatusStr('Data saved. Printing...') _print(props).finally(() => { setPrintStatus(PrintStatus.DONE); setStatusStr('Data sent to printer.'); }); }); }, []); const RetryButton = (): ReactElement | null => { if (printStatus === PrintStatus.DONE) { return } else { return null; } } return {statusStr} ; } export const BarCodeDisplay = (props: BarCodeProps): ReactElement => { return ID#: {props.id} Date: {props.date.toLocaleDateString()}, {props.date.toLocaleTimeString()} Location {props.location} ; }