Created
January 15, 2019 15:46
-
-
Save Jalson1982/51a4e8aa0c2edb147e178b59c9ac2788 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { View } from "react-native"; | |
import { connect } from "react-redux"; | |
import { RNCamera } from "react-native-camera"; | |
import BarcodeFinder from "../components/BarcodeFinder"; | |
import CustomModal from "../components/Modal"; | |
import searchItem from "../actions/searchItem"; | |
import CompleteButton from "../components/CompleteButton"; | |
class ScanItem extends Component { | |
constructor(props) { | |
super(props); | |
this.camera = null; | |
this.state = { | |
play: false, | |
camera: { | |
type: RNCamera.Constants.Type.back, | |
flashMode: RNCamera.Constants.FlashMode.auto, | |
barcodeFinderVisible: true | |
}, | |
modalOpen: false, | |
isBarcodeScannerEnabled: true | |
}; | |
} | |
// add item in item details local state and close modal. | |
addItem = (item) => { | |
this.props.addAlternativeItem(item); | |
this.setState({modalOpen:false,isBarcodeScannerEnabled:true}) | |
} | |
// When scan barcode this will set results and open modal. | |
onBarCodeRead = scanResult => { | |
if (scanResult.data !== null && this.state.isBarcodeScannerEnabled) { | |
this.props.searchItem(scanResult.data); | |
this.setState({ modalOpen: true, isBarcodeScannerEnabled:false }); | |
} | |
return; | |
}; | |
render() { | |
return ( | |
<View style={styles.container}> | |
<RNCamera | |
ref={ref => { | |
this.camera = ref; | |
}} | |
captureAudio={false} | |
barcodeFinderVisible={true} | |
barcodeFinderWidth={100} | |
barcodeFinderHeight={60} | |
barcodeFinderBorderColor="black" | |
barcodeFinderBorderWidth={2} | |
defaultTouchToFocus | |
mirrorImage={false} | |
onBarCodeRead={value => this.onBarCodeRead(value)} | |
style={styles.preview} | |
type={this.state.camera.type} | |
/> | |
<BarcodeFinder | |
width={280} | |
height={220} | |
borderColor="#00803C" | |
borderWidth={2} | |
/> | |
<CompleteButton title="Close scanner" onPress={this.props.switchScanner}/> | |
{this.state.modalOpen && ( | |
<CustomModal | |
isOpen={this.state.modalOpen} | |
onClose={() => this.setState({ modalOpen: false,isBarcodeScannerEnabled:true })} | |
quantity={this.props.quantity} | |
addItem={this.addItem} | |
/> | |
)} | |
</View> | |
); | |
} | |
} | |
const mapDispatchToProps = dispatch => { | |
return { | |
searchItem: ean => dispatch(searchItem(ean)) | |
}; | |
}; | |
const styles = { | |
container: { | |
flex: 1 | |
}, | |
preview: { | |
flex: 1, | |
alignItems: "center", | |
justifyContent: "space-between" | |
} | |
}; | |
export default connect( | |
null, | |
mapDispatchToProps | |
)(ScanItem); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment