Cleans up unneeded Firebase query code.

This commit is contained in:
Aaron Louie 2020-09-11 17:38:53 -04:00
parent 7a904d6ec0
commit eadc30335f
2 changed files with 20 additions and 62 deletions

76
App.tsx
View File

@ -1,20 +1,20 @@
// @refresh reset
import AsyncStorage from '@react-native-community/async-storage';
import NetInfo, {NetInfoState, NetInfoStateType} from '@react-native-community/netinfo';
import {format, parse} from 'date-fns';
import {format} from 'date-fns';
import {BarCodeEvent, BarCodeScanner, PermissionResponse} from 'expo-barcode-scanner';
import * as firebase from 'firebase';
import 'firebase/firestore';
import React, {ReactElement, useCallback, useEffect, useState} from 'react';
import {AppRegistry, SafeAreaView, View, YellowBox} from 'react-native';
import {Appbar, DefaultTheme, Provider as PaperProvider, Snackbar, Title,} from 'react-native-paper';
import {Appbar, Provider as PaperProvider, Snackbar,} from 'react-native-paper';
import {expo as appExpo} from './app.json';
import {CancelButton} from './components/Common';
import {InputLineCountButton, InputLineCountScreen} from './components/LineCount';
import {BarCodeDisplay, PrintButton, PrintingMessage} from './components/Print';
import {IdNumberInput, InputIdButton, ScanButton, Scanner} from './components/Scan';
import {SettingsScreen} from './components/Settings';
import {colors, styles} from './components/Styles';
import {styles, theme} from './components/Styles';
import {sendDataToFirebase, SyncMessage} from './components/Sync';
import {dateFormat, firebaseConfig} from './config/default';
import {BarcodeScannerAppState} from './models/BarcodeScannerAppState';
@ -22,25 +22,20 @@ import {CameraType, ElementProps, StateProps} from './models/ElementProps';
import {LineCount} from './models/LineCount';
import {Sample} from './models/Sample';
// Initialize Firebase if not already initialized.
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
YellowBox.ignoreWarnings([
'Setting a timer for a long period of time', // Ignore Firebase timer warnings
'Remote debugger is in a background tab', // Ignore remote debugger warnings
]);
// Initialize Firebase if not already initialized.
if (firebase.apps.length === 0) {
firebase.initializeApp(firebaseConfig);
}
const db = firebase.firestore();
const samplesCollection = db.collection('samples');
const countsCollection = db.collection('counts');
const theme = {
...DefaultTheme,
colors: colors,
}
export default function Main() {
const [appState, setAppState] = useState<BarcodeScannerAppState>(BarcodeScannerAppState.INITIAL);
const [sampleId, setSampleId] = useState<string>('');
@ -61,8 +56,9 @@ export default function Main() {
};
useEffect(() => {
// Retrieve previous stored settings, if they exist.
AsyncStorage.multiGet(Object.keys(defaultsInitializers)).then(storedDefaults => {
console.log('storedDefaults', storedDefaults);
storedDefaults.forEach(d => {
if (d[1] !== null) {
// @ts-ignore
@ -71,12 +67,14 @@ export default function Main() {
});
});
// Watch for changes to internet connectivity.
NetInfo.addEventListener((state: NetInfoState) => {
if (state.type === NetInfoStateType.wifi) {
setIsConnected(!!(state.isConnected && state.isInternetReachable));
}
});
// Ask for permission to use the camera.
BarCodeScanner.requestPermissionsAsync().then((value: PermissionResponse) => {
if (value.granted) {
setAppState(BarcodeScannerAppState.DEFAULT);
@ -84,43 +82,10 @@ export default function Main() {
setAppState(BarcodeScannerAppState.ERROR);
}
});
const unsubscribeSamples = samplesCollection.onSnapshot(querySnapshot => {
// Transform and sort the data returned from Firebase
const samplesFirestore = querySnapshot
.docChanges()
.filter(({type}) => type === 'added')
.map(({doc}) => {
const sample = doc.data();
return {...sample, createdAt: sample.createdAt.toDate()} as Sample;
})
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
appendSamples(samplesFirestore);
});
const unsubscribeCounts = countsCollection.onSnapshot(querySnapshot => {
// Transform and sort the data returned from Firebase
const lineCountsFirestore = querySnapshot
.docChanges()
.filter(({type}) => type === 'added')
.map(({doc}) => {
const lineCount = doc.data();
return {...lineCount, createdAt: lineCount.createdAt.toDate()} as LineCount;
})
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());
appendLineCounts(lineCountsFirestore);
});
return () => {
unsubscribeSamples()
unsubscribeCounts()
}
}, []);
const _doNothing = () => {
};
// State event handlers
const _doNothing = () => {};
const _scan = () => {
setErrorMessage('');
setAppState(BarcodeScannerAppState.SCANNING);
@ -172,15 +137,6 @@ export default function Main() {
sendDataToFirebase([newData], countsCollection);
}
const appendSamples = useCallback((newSamples) => {
setSamples((previousSamples) => previousSamples.concat(newSamples));
}, [samples]);
const appendLineCounts = useCallback((newLineCounts) => {
setLineCounts((previousLineCounts) => previousLineCounts.concat(newLineCounts));
}, [lineCounts]);
const ErrorMessage = (props: ElementProps): ReactElement => {
return <View style={styles.fullScreen}>
<View style={styles.container}>
@ -275,10 +231,6 @@ export default function Main() {
setNumCopies(newNumCopies);
setLocationStr(newLocationStr);
console.log(newCameraType);
console.log(newLocationStr);
console.log(newNumCopies);
AsyncStorage.multiSet([
['default.cameraType', newCameraType as string],
['default.locationStr', newLocationStr],

View File

@ -9,6 +9,12 @@ export const colors = {
notification: '#E57200',
};
// Set up the Material UI theme
export const theme = {
...DefaultTheme,
colors: colors,
}
const _common = StyleSheet.create({
container: {
...StyleSheet.absoluteFillObject,