Advertisement
roopa2

Untitled

Dec 12th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.53 KB | None | 0 0
  1. import React from 'react'
  2. import { StyleSheet, Text, Alert } from 'react-native'
  3. import glamorous from 'glamorous-native'
  4.  
  5. import {
  6. BarCodeScanner,
  7. Permissions,
  8. Constants,
  9. BarCodeReadCallback,
  10. } from 'expo'
  11.  
  12. import {
  13. NavigationScreenProp,
  14. NavigationStackScreenOptions,
  15. } from 'react-navigation'
  16.  
  17. import BackgroundWrapper from '../../components/wrappers/Background'
  18. import Api, * as ApiShapes from '../../helpers/Api'
  19. import CardItem from '../../components/cards/CardItem'
  20. import LoadingArrayHijack from '../../components/wrappers/LoadingArrayHijack'
  21.  
  22. interface State {
  23. hasCameraPermission: any
  24. showCamera: boolean
  25. barCodeType: string
  26. barCodeData: string
  27. loading: boolean
  28. errorMessage: string
  29. newCard?: ApiShapes.Card
  30. }
  31.  
  32. interface Props {}
  33.  
  34. interface InjectedProps extends Props {
  35. navigation: NavigationScreenProp<any, any>
  36. }
  37.  
  38. export default class Logo extends React.Component<Props> {
  39. static navigationOptions: NavigationStackScreenOptions = {
  40. title: 'QR Scan',
  41. headerBackTitle: 'Back',
  42. headerBackground: '#0057A8',
  43. }
  44.  
  45. state: State = {
  46. hasCameraPermission: '',
  47. showCamera: true,
  48. barCodeType: '',
  49. barCodeData: '',
  50. errorMessage: '',
  51. loading: false,
  52. }
  53.  
  54. async componentDidMount() {
  55. let { status } = await Permissions.askAsync(Permissions.CAMERA)
  56. this.setState({ hasCameraPermission: status === 'granted' })
  57. }
  58.  
  59. handleBarCodeRead: BarCodeReadCallback = ({ type, data }) => {
  60. //Alert.alert(`Barcode '${data}' of type '${type}' was scanned.`)
  61. this.setState({ showCamera: false, barCodeType: type, barCodeData: data })
  62.  
  63. this.fetchNewCard()
  64. }
  65.  
  66. async fetchNewCard() {
  67. try {
  68. this.setState({ loading: true })
  69. const { card } = await Api<
  70. ApiShapes.getQRCodeReq,
  71. ApiShapes.getQRCodeResp
  72. >('cardByQrCode', { code: this.state.barCodeData })
  73. this.setState({ newCard: card, loading: false })
  74. } catch (e) {
  75. console.error('Api error:', e)
  76. this.setState({ loading: false, errorMessage: e.msg })
  77. }
  78. }
  79.  
  80. render() {
  81. const { newCard } = this.state
  82.  
  83. const { hasCameraPermission } = this.state
  84. if (hasCameraPermission === null) {
  85. return <Text>Requesting for camera permission</Text>
  86. }
  87. if (hasCameraPermission === false) {
  88. return <Text>No access to camera</Text>
  89. }
  90.  
  91. if (this.state.showCamera) {
  92. return (
  93. <glamorous.View flex={1} style={styles.container}>
  94. <BackgroundWrapper />
  95.  
  96. <BarCodeScanner
  97. onBarCodeRead={this.handleBarCodeRead}
  98. style={{ ...StyleSheet.absoluteFillObject }}
  99. />
  100. </glamorous.View>
  101. )
  102. } else {
  103. return (
  104. <glamorous.View flex={1}>
  105. <BackgroundWrapper />
  106.  
  107. <TextField>
  108. The Type is {this.state.barCodeType} with data{' '}
  109. {this.state.barCodeData}
  110. </TextField>
  111.  
  112. <CardContainer>
  113. <CardButton>
  114. <CardItem
  115. card={newCard}
  116. height={270}
  117. style={{ marginHorizontal: 16, marginBottom: 32 }}
  118. />
  119. </CardButton>
  120. </CardContainer>
  121. </glamorous.View>
  122. )
  123. }
  124. }
  125. }
  126.  
  127. const styles = StyleSheet.create({
  128. container: {
  129. flex: 1,
  130. alignItems: 'center',
  131. justifyContent: 'center',
  132. paddingTop: Constants.statusBarHeight,
  133. backgroundColor: '#ecf0f1',
  134. },
  135. })
  136.  
  137. const TextField = glamorous.text({
  138. color: '#FFFFFF',
  139. fontSize: 18,
  140. })
  141.  
  142. const CardButton = glamorous.touchableOpacity({
  143. justifyContent: 'center',
  144. alignItems: 'center',
  145. flexBasis: '50%',
  146. overflow: 'hidden',
  147. })
  148.  
  149. const CardContainer = glamorous.view({
  150. flexDirection: 'row',
  151. flexWrap: 'wrap',
  152. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement