2020-09-01 02:12:32 +00:00
|
|
|
import React, {ReactElement, useEffect, useState} from 'react';
|
|
|
|
import {Text, View} from 'react-native';
|
2020-09-01 19:55:23 +00:00
|
|
|
import {Button, Title} from 'react-native-paper';
|
2020-09-02 21:09:49 +00:00
|
|
|
import QRCode from 'react-native-qrcode-svg';
|
2020-08-29 03:40:55 +00:00
|
|
|
import {BarCodeProps, ButtonProps, PrintingProps} from '../models/ElementProps';
|
2020-09-02 21:09:49 +00:00
|
|
|
import {Sample} from '../models/Sample';
|
2020-09-01 02:12:32 +00:00
|
|
|
import {colors, styles} from './Styles';
|
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
|
|
|
import * as Print from 'expo-print';
|
2020-09-02 16:13:16 +00:00
|
|
|
import {format} from 'date-fns'
|
2020-09-01 02:12:32 +00:00
|
|
|
|
|
|
|
enum PrintStatus {
|
|
|
|
SAVING = 'SAVING',
|
|
|
|
PRINTING = 'PRINTING',
|
|
|
|
DONE = 'DONE',
|
|
|
|
}
|
|
|
|
|
|
|
|
const _save = (props: PrintingProps): Promise<void> => {
|
2020-09-02 21:09:49 +00:00
|
|
|
const storageVal: Sample = {
|
2020-09-01 02:12:32 +00:00
|
|
|
id: props.id,
|
2020-09-02 21:09:49 +00:00
|
|
|
barcodeId: props.barCodeId,
|
|
|
|
createdAt: props.date,
|
|
|
|
locationId: props.location,
|
2020-09-01 02:12:32 +00:00
|
|
|
};
|
2020-09-02 21:09:49 +00:00
|
|
|
return AsyncStorage.setItem(props.id, JSON.stringify(storageVal));
|
2020-09-01 02:12:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const _print = (props: PrintingProps): Promise<void> => {
|
2020-09-02 21:09:49 +00:00
|
|
|
console.log('props.svg', props.svg);
|
2020-09-01 02:12:32 +00:00
|
|
|
return Print.printAsync({
|
|
|
|
html: `
|
|
|
|
<style>
|
2020-09-01 03:33:49 +00:00
|
|
|
@media print {
|
|
|
|
@page {
|
|
|
|
size: 2in 1.25in;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
html, body {
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
div.box {
|
|
|
|
width: 2in;
|
|
|
|
height: 1.25in;
|
|
|
|
color: #000;
|
|
|
|
text-align: center;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
div.box p {
|
|
|
|
font-size: 10pt;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
svg {
|
|
|
|
position: absolute;
|
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
}
|
2020-09-01 02:12:32 +00:00
|
|
|
}
|
|
|
|
</style>
|
2020-09-01 03:33:49 +00:00
|
|
|
<div class="box">
|
2020-09-02 21:09:49 +00:00
|
|
|
<p>ID#: ${props.barCodeId}</p>
|
2020-09-01 03:33:49 +00:00
|
|
|
<p>Date: ${props.date.toLocaleDateString()} ${props.date.toLocaleTimeString()}</p>
|
|
|
|
<p>Loc#: ${props.location}</p>
|
2020-09-02 21:09:49 +00:00
|
|
|
${props.svg}
|
|
|
|
<p>${props.id}</p>
|
2020-09-01 03:33:49 +00:00
|
|
|
</div>
|
2020-09-01 02:12:32 +00:00
|
|
|
`,
|
|
|
|
});
|
|
|
|
}
|
2020-08-29 03:40:55 +00:00
|
|
|
|
|
|
|
export const PrintButton = (props: ButtonProps): ReactElement => {
|
|
|
|
return <Button
|
2020-09-01 02:12:32 +00:00
|
|
|
icon="printer"
|
|
|
|
mode="contained"
|
|
|
|
color={colors.accent}
|
2020-08-29 03:40:55 +00:00
|
|
|
onPress={props.onClicked}
|
2020-09-01 02:12:32 +00:00
|
|
|
style={styles.btnLg}
|
|
|
|
labelStyle={styles.btnLg}
|
|
|
|
>Print Labels</Button>;
|
2020-08-29 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const PrintingMessage = (props: PrintingProps): ReactElement => {
|
2020-09-01 02:12:32 +00:00
|
|
|
const [statusStr, setStatusStr] = useState<string>('Saving data...');
|
|
|
|
const [printStatus, setPrintStatus] = useState<PrintStatus>(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 <Button
|
|
|
|
icon="reload"
|
|
|
|
onPress={() => _print(props)}
|
|
|
|
color={colors.onBackground}
|
|
|
|
style={styles.btnLg}
|
|
|
|
labelStyle={styles.btnLg}
|
|
|
|
>Print again</Button>
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return <View style={styles.container}>
|
|
|
|
<View style={styles.preview}>
|
2020-09-02 21:09:49 +00:00
|
|
|
<BarCodeDisplay
|
|
|
|
id={props.id}
|
|
|
|
barCodeId={props.barCodeId}
|
|
|
|
date={props.date}
|
|
|
|
location={props.location}
|
|
|
|
svg={props.svg}
|
|
|
|
/>
|
2020-09-01 02:12:32 +00:00
|
|
|
</View>
|
|
|
|
<View style={styles.container}>
|
2020-09-01 19:55:23 +00:00
|
|
|
<Title style={styles.heading}>{statusStr}</Title>
|
2020-09-01 02:12:32 +00:00
|
|
|
<RetryButton />
|
|
|
|
<Button
|
|
|
|
icon="cancel"
|
|
|
|
mode={printStatus === PrintStatus.DONE ? 'contained' : 'text'}
|
|
|
|
color={printStatus === PrintStatus.DONE ? colors.accent : colors.onBackground}
|
|
|
|
onPress={props.onCancel}
|
|
|
|
style={styles.btnLg}
|
|
|
|
labelStyle={styles.btnLg}
|
|
|
|
>{printStatus === PrintStatus.DONE ? 'Done' : 'Cancel'}</Button>
|
|
|
|
</View>
|
2020-08-29 03:40:55 +00:00
|
|
|
</View>;
|
|
|
|
}
|
|
|
|
|
2020-09-01 02:12:32 +00:00
|
|
|
|
2020-08-29 03:40:55 +00:00
|
|
|
export const BarCodeDisplay = (props: BarCodeProps): ReactElement => {
|
2020-09-02 21:09:49 +00:00
|
|
|
console.log('BarCodeDisplay props.svg', props.svg);
|
2020-09-01 02:12:32 +00:00
|
|
|
return <View style={styles.printPreview}>
|
2020-08-29 03:40:55 +00:00
|
|
|
<Text style={styles.label}>ID#: {props.id}</Text>
|
|
|
|
<Text style={styles.label}>Date: {props.date.toLocaleDateString()}, {props.date.toLocaleTimeString()}</Text>
|
|
|
|
<Text style={styles.label}>Location {props.location}</Text>
|
2020-09-02 21:09:49 +00:00
|
|
|
<QRCode value={props.id} />
|
2020-08-29 03:40:55 +00:00
|
|
|
</View>;
|
|
|
|
}
|