Moves barcode pattern into config.

This commit is contained in:
Aaron Louie 2020-09-14 13:51:17 -04:00
parent 93c7cdd008
commit 3e56f7dbfe
6 changed files with 19 additions and 15 deletions

15
App.tsx
View File

@ -15,8 +15,8 @@ import {BarCodeDisplay, PrintButton, PrintingMessage} from './components/Print';
import {IdNumberInput, InitialsInput, InputIdButton, ScanButton, Scanner} from './components/Scan';
import {SettingsScreen} from './components/Settings';
import {styles, theme} from './components/Styles';
import {sendDataToFirebase, SyncMessage} from './components/Sync';
import {firebaseConfig, defaults} from './config/default';
import {SyncMessage} from './components/Sync';
import {defaults, firebaseConfig} from './config/default';
import {BarcodeScannerAppState} from './models/BarcodeScannerAppState';
import {CameraType, ElementProps, StateProps} from './models/ElementProps';
import {LineCount} from './models/LineCount';
@ -87,7 +87,8 @@ export default function Main() {
}, []);
// State event handlers
const _doNothing = () => {};
const _doNothing = () => {
};
const _scan = () => {
setErrorMessage('');
setAppState(BarcodeScannerAppState.SCANNING);
@ -107,13 +108,9 @@ export default function Main() {
const handleBarCodeScanned = (e: BarCodeEvent) => {
// Make sure the data is the right length.
// Scanned barcodes will be exactly 14 digits long.
// Manually-entered ID numbers will be exactly 9 digits long.
const barCodeString = e.data;
const pattern = /^[\d]{14}$|^[\d]{9}$/;
console.log('barCodeString', barCodeString);
if (pattern.test(barCodeString)) {
const cardId = e.data.slice(0, 9);
if (defaults.barCodeRegex.test(barCodeString)) {
const cardId = e.data.slice(0, defaults.barCodeNumLength);
const newSampleDate = new Date();
setBarCodeId(cardId);
setSampleDate(newSampleDate);

View File

@ -1,8 +1,7 @@
import {ButtonProps, InputLineCountScreenProps} from '../models/ElementProps';
import React, {ReactElement, useState} from 'react';
import {View} from 'react-native';
import {DefaultTheme, Subheading, Title, RadioButton, Paragraph, TextInput, HelperText, Button} from 'react-native-paper';
import {TextInput as NumberInput} from 'react-native';
import {Button, DefaultTheme, HelperText, Subheading, TextInput, Title} from 'react-native-paper';
import {ButtonProps, InputLineCountScreenProps} from '../models/ElementProps';
import {colors, styles} from './Styles';
export const InputLineCountButton = (props: ButtonProps): ReactElement => {

View File

@ -2,6 +2,7 @@ import {BarCodeScanner} from 'expo-barcode-scanner';
import React, {ReactElement, useState} from 'react';
import {Text, View} from 'react-native';
import {Button, DefaultTheme, HelperText, Subheading, TextInput, Title} from 'react-native-paper';
import {defaults} from '../config/default';
import {ButtonProps, ElementProps, InputInitialsProps, ScannerProps} from '../models/ElementProps';
import {colors, styles} from './Styles';
@ -56,9 +57,8 @@ export const InputIdButton = (props: ButtonProps): ReactElement => {
export const IdNumberInput = (props: ScannerProps): ReactElement => {
const [inputStr, setInputStr] = useState<string>('');
const pattern = /^[\d]{9}$/;
const hasErrors = () => {
return !pattern.test(inputStr);
return !defaults.barCodeRegex.test(inputStr);
};
const onSubmit = () => {
@ -81,7 +81,7 @@ export const IdNumberInput = (props: ScannerProps): ReactElement => {
keyboardType="numeric"
/>
<HelperText type="error" visible={hasErrors()}>
ID number must be exactly 9 digits. No other characters are allowed.
ID number must be exactly {defaults.barCodeNumLength} digits. No other characters are allowed.
</HelperText>
<Button
icon="check"

View File

@ -23,4 +23,7 @@ export const defaults: AppDefaults = {
locationId: '0000', // Default location ID. Can be overridden by user setting.
lineCountRegex: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records.
qrCodeRegex: /^[\d]{9}-[a-zA-Z]+-[\d]{12}-[\d]{4}$/, // ID format for QR Code records.
barCodeNumLength: 9, // Number of digits in Bar Code.
barCodeRegex: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long.
// Manually-entered ID numbers will be exactly 9 digits long.
}

View File

@ -27,4 +27,7 @@ export const defaults: AppDefaults = {
locationId: '0000', // Default location ID. Can be overridden by user setting.
lineCountRegex: /^[\d]{4}-[\d]{12}$/, // ID format for Line Count records.
qrCodeRegex: /^[\d]{9}-[a-zA-Z]+-[\d]{12}-[\d]{4}$/, // ID format for QR Code records.
barCodeNumLength: 9, // Number of digits in Bar Code.
barCodeRegex: /^[\d]{14}$|^[\d]{9}$/, // Pattern for Bar Code data. Scanned barcodes will be either 9 or 14 digits long.
// Manually-entered ID numbers will be exactly 9 digits long.
}

View File

@ -10,4 +10,6 @@ export interface AppDefaults {
locationId: string;
lineCountRegex: RegExp;
qrCodeRegex: RegExp;
barCodeRegex: RegExp;
barCodeNumLength: number;
}