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 {defaults} from '../config/default'; 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 numCopies = props.numCopies; // Number of copies to print const units = 'mm'; const pageSize = 28.6; const pageWidth = `${pageSize}${units}`; const sideTextWidth = `4${units}`; const sideTextTop = `11${units}`; const sideTextMargin = `1.5${units}`; const topTextMargin = `3${units}`; const bottomTextMargin = `2.5${units}`; const fontSize = `${6 * 0.3528}${units}`; // 6pt * ( 0.3528 mm / pt ) const styleHtml = ` `; // Repeat the page HTML for as many copies as we need. const pagesArray = []; for (let i=0; i
${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}
`; pagesArray.push(pageHtml); } const pagesHtml = pagesArray.join('\n'); const html = ` ${styleHtml} ${pagesHtml} `; return Print.printAsync({html}); } 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: {format(props.date, defaults.dateDisplayFormat)} Location #: {props.location} ; }