2020-09-03 22:27:44 +00:00
|
|
|
import AsyncStorage from '@react-native-community/async-storage';
|
|
|
|
import {format} from 'date-fns';
|
|
|
|
import * as Print from 'expo-print';
|
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';
|
2020-09-03 22:27:44 +00:00
|
|
|
|
|
|
|
const qrcode = require('qrcode');
|
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
|
|
|
}
|
|
|
|
|
2020-09-03 22:27:44 +00:00
|
|
|
const _print = async (props: PrintingProps): Promise<void> => {
|
2020-09-10 14:07:58 +00:00
|
|
|
const numCopies = 50; // 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 )
|
2020-09-03 22:27:44 +00:00
|
|
|
|
2020-09-09 12:39:31 +00:00
|
|
|
const styleHtml = `
|
|
|
|
<style>
|
|
|
|
@media print {
|
|
|
|
@page {
|
2020-09-10 14:07:58 +00:00
|
|
|
size: ${pageWidth};
|
2020-09-09 12:39:31 +00:00
|
|
|
margin: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
padding: 0;
|
2020-09-09 12:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
html, body {
|
2020-09-10 14:07:58 +00:00
|
|
|
height: ${100 * numCopies}vh;
|
|
|
|
margin: 0 !important;
|
|
|
|
padding: 0 !important;
|
|
|
|
overflow: hidden;
|
2020-09-09 12:39:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
.page-container {
|
2020-09-10 14:07:58 +00:00
|
|
|
display: block;
|
2020-09-09 12:39:31 +00:00
|
|
|
position: relative;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
width: ${pageWidth};
|
|
|
|
height: ${pageWidth};
|
2020-09-09 12:39:31 +00:00
|
|
|
}
|
2020-09-01 03:33:49 +00:00
|
|
|
|
2020-09-09 12:39:31 +00:00
|
|
|
.circle {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
width: ${pageWidth};
|
|
|
|
height: ${pageWidth};
|
2020-09-09 12:39:31 +00:00
|
|
|
color: #000;
|
|
|
|
text-align: center;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
border-radius: ${pageWidth};
|
2020-09-09 12:39:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-10 14:07:58 +00:00
|
|
|
.page-container .date,
|
|
|
|
.page-container .time,
|
|
|
|
.page-container .location,
|
|
|
|
.page-container .barCodeId {
|
2020-09-09 12:39:31 +00:00
|
|
|
position: absolute;
|
|
|
|
margin: 0;
|
|
|
|
padding: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
font-size: ${fontSize};
|
|
|
|
font-weight: bold;
|
2020-09-09 12:39:31 +00:00
|
|
|
font-family: monospace;
|
|
|
|
text-align: center;
|
|
|
|
line-height: 1;
|
|
|
|
}
|
2020-09-03 22:27:44 +00:00
|
|
|
|
2020-09-10 14:07:58 +00:00
|
|
|
.page-container .date { top: ${topTextMargin}; left: 0; width: 100%; }
|
|
|
|
.page-container .time { top: ${sideTextTop}; left: ${sideTextMargin}; width: ${sideTextWidth}; }
|
|
|
|
.page-container .location { top: ${sideTextTop}; right: ${sideTextMargin}; width: ${sideTextWidth}; }
|
|
|
|
.page-container .barCodeId { bottom: ${bottomTextMargin}; left: 0; width: 100%; }
|
2020-09-09 12:39:31 +00:00
|
|
|
|
|
|
|
svg {
|
|
|
|
position: absolute;
|
|
|
|
top: 0;
|
|
|
|
left: 0;
|
2020-09-10 14:07:58 +00:00
|
|
|
width: ${pageWidth};
|
|
|
|
height: ${pageWidth};
|
2020-09-01 02:12:32 +00:00
|
|
|
}
|
2020-09-09 12:39:31 +00:00
|
|
|
}
|
|
|
|
</style>
|
|
|
|
`;
|
|
|
|
|
2020-09-10 14:07:58 +00:00
|
|
|
// Repeat the page HTML for as many copies as we need.
|
|
|
|
const pagesArray = [];
|
|
|
|
|
|
|
|
for (let i=0; i<numCopies; i++) {
|
|
|
|
// TODO: Comment out these two lines, because this is just for testing.
|
|
|
|
const fakeBarcodeId = `${i}`.padStart(9, '0');
|
|
|
|
const fakeDate = new Date();
|
|
|
|
const fakeId = fakeBarcodeId + '-' + format(fakeDate, 'yyyyMMddHH') + '-' + `${i}`.padStart(2, '0') + '-' + props.location;
|
|
|
|
|
|
|
|
const svgString = await qrcode.toString(fakeId, {
|
|
|
|
width: 72, // 20mm
|
|
|
|
height: 72,
|
|
|
|
margin: 10,
|
|
|
|
errorCorrectionLevel: 'high',
|
|
|
|
type: 'svg',
|
|
|
|
color: {
|
|
|
|
light: '#ffffff00',
|
|
|
|
dark: '#000',
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const pageHtml = `
|
|
|
|
<div class="page-container">
|
|
|
|
<div class="circle"></div>
|
|
|
|
${svgString}
|
|
|
|
<div class="date">${format(fakeDate, 'yyyy-MM-dd')}</div>
|
|
|
|
<div class="time">
|
|
|
|
T<br />
|
|
|
|
${format(fakeDate, 'HH')}<br />
|
|
|
|
${`${i}`.padStart(2, '0')}
|
|
|
|
</div>
|
|
|
|
<div class="location">
|
|
|
|
L<br />
|
|
|
|
${props.location.slice(0, 2)}<br />
|
|
|
|
${props.location.slice(2)}<br />
|
|
|
|
</div>
|
|
|
|
<div class="barCodeId">#${fakeBarcodeId}</div>
|
2020-09-01 03:33:49 +00:00
|
|
|
</div>
|
2020-09-10 14:07:58 +00:00
|
|
|
`;
|
2020-09-09 12:39:31 +00:00
|
|
|
|
2020-09-10 14:07:58 +00:00
|
|
|
pagesArray.push(pageHtml);
|
|
|
|
}
|
|
|
|
|
|
|
|
const pagesHtml = pagesArray.join('\n');
|
|
|
|
const html = `
|
|
|
|
${styleHtml}
|
|
|
|
${pagesHtml}
|
|
|
|
`;
|
2020-09-09 12:39:31 +00:00
|
|
|
|
2020-09-10 14:07:58 +00:00
|
|
|
return Print.printAsync({html});
|
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}
|
|
|
|
/>
|
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-03 22:27:44 +00:00
|
|
|
<RetryButton/>
|
2020-09-01 02:12:32 +00:00
|
|
|
<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-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-03 22:27:44 +00:00
|
|
|
<QRCode value={props.id}/>
|
2020-08-29 03:40:55 +00:00
|
|
|
</View>;
|
|
|
|
}
|