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'; import AsyncStorage from '@react-native-community/async-storage'; import * as Print from 'expo-print'; import {format} from 'date-fns' 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 = (props: PrintingProps): Promise => { console.log('props.svg', props.svg); return Print.printAsync({ html: `

ID#: ${props.barCodeId}

Date: ${props.date.toLocaleDateString()} ${props.date.toLocaleTimeString()}

Loc#: ${props.location}

${props.svg}

${props.id}

`, }); } 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 => { console.log('BarCodeDisplay props.svg', props.svg); return ID#: {props.id} Date: {props.date.toLocaleDateString()}, {props.date.toLocaleTimeString()} Location {props.location} ; }