Advertisement
Guest User

Untitled

a guest
Oct 18th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.64 KB | None | 0 0
  1. /**
  2. * @format
  3. * @flow
  4. */
  5.  
  6. import * as React from 'react';
  7. import {
  8. StyleSheet,
  9. View,
  10. Text,
  11. SafeAreaView,
  12. ActivityIndicator,
  13. } from 'react-native';
  14. import {NavigationScreenProp} from 'react-navigation';
  15. import SelfieLive from '@krealo/third-party/react-native-selfie-live';
  16. import Button from '@krealo/commons/components/ui/button/Button';
  17. import Colors from '@krealo/commons/platform/Colors';
  18. import FaceTarget from '@krealo/commons/components/face/FaceTarget';
  19. import Heading from '@krealo/commons/components/ui/heading/Heading';
  20. import Paragraph from '@krealo/commons/components/ui/paragraph';
  21.  
  22. import storageService from '../../../../../commons/storage/storage';
  23. import Camera from '../../../../../components/camera/Camera';
  24. import SelfieService from '../../../commons/services/SelfieService';
  25.  
  26. type NavigationState = {
  27. };
  28.  
  29. type Props = {
  30. navigation: NavigationScreenProp<NavigationState>,
  31. i18n: Object,
  32. };
  33. type State = {
  34. selfie: string,
  35. isLoading: boolean,
  36. errorTOC: string,
  37. };
  38.  
  39. // TODO: casos de fallo a implementarse posteriormente
  40. const codeError = {
  41. 0: (nav) => {
  42. nav.navigate('YoungYearScreen');
  43. },
  44. 1: (nav) => {
  45. nav.navigate('BlackList');
  46. },
  47. };
  48.  
  49. function errorHanding (code, nav) {
  50. if (codeError.hasOwnProperty(code)) {
  51. codeError[code](nav);
  52. }
  53. }
  54. export default class SelfieCameraScreen extends React.Component<Props, State> {
  55. camera: ?Camera;
  56.  
  57. state = {
  58. selfie: '',
  59. isLoading: false,
  60. errorTOC: '',
  61. };
  62.  
  63. componentWillMount () {
  64. this.sendInformation.bind(this);
  65. }
  66.  
  67. sendInformation = () => {
  68. const {navigation} = this.props;
  69. const {selfie} = this.state;
  70.  
  71. const {sidefront, sideback} = navigation.getParam('sides', {});
  72.  
  73. const front = sidefront.split('\n').join('');
  74. const back = sideback.split('\n').join('');
  75. const face = selfie.split('\n').join('');
  76.  
  77. this.setState({isLoading: true});
  78.  
  79. SelfieService.postSendSelfie({
  80. face,
  81. back,
  82. front,
  83. })
  84. .then((res) => {
  85. storageService.setItem('@MineStore:userId', res.data.id).then(() => {
  86. this.setState({isLoading: false});
  87. navigation.navigate('PersonalInfo', {
  88. dataUser: res.data,
  89. });
  90. });
  91. })
  92. .catch((error) => {
  93. this.setState({errorTOC: error.response.data.message});
  94. // TODO: Implementar navegación en caso de fallos posteriormente
  95. })
  96. .then((res) => {
  97. this.setState({isLoading: false});
  98. });
  99. }
  100.  
  101. onPressTakePhoto = async () => {
  102. if (!this.camera) {
  103. return;
  104. }
  105.  
  106. this.setState({isLoading: true});
  107.  
  108. const options = {
  109. quality: 1, base64: true, width: 1280, height: 720,
  110. };
  111.  
  112. const data = await this.camera.getCameraRef().takePictureAsync(options);
  113.  
  114. this.setState({photo: data.base64, isLoading: false}, () => {
  115. this.sendInformation();
  116. });
  117. }
  118.  
  119. onPressSelfieLive = async () => {
  120. try {
  121. // TODO: Inyectar API KEY de sandbox a TOC
  122. const response = await SelfieLive.getSelfieLive('12192c583c5240f88e0f4db584d938f6');
  123.  
  124. this.setState({selfie: response}, () => {
  125. this.sendInformation();
  126. });
  127. } catch (e) {
  128. // TODO: Implementar error en caso falle el llamdo a selfielivew con UI
  129. console.warn('error :(', e);
  130. }
  131. }
  132.  
  133. onPressReRegister = () => {
  134. const {navigation} = this.props;
  135. navigation.navigate('DocumentScan');
  136. }
  137.  
  138. render () {
  139. const {i18n} = this.props;
  140. const {isLoading, selfie, errorTOC} = this.state;
  141. const hasError = !!errorTOC
  142.  
  143. return (
  144. <SafeAreaView
  145. accessibilityLabel="selfiecamerascreen.wrapper"
  146. style={styles.container}
  147. >
  148.  
  149. {selfie ? (
  150. <View
  151. accessibilityLabel="selfiecamerascreen.containerwrapper.response"
  152. style={styles.containerViewResponse}
  153. >
  154.  
  155. {isLoading && (
  156. <>
  157. <View
  158. accessibilityLabel="selfiecamerascreen.containerstyleactivityindicator"
  159. style={styles.activityIndicator}
  160. >
  161. <ActivityIndicator
  162. accessibilityLabel="selfiecamerascreen.activityindicator"
  163. color={Colors.bluemine}
  164. size="large"
  165. />
  166. </View>
  167. <Text
  168. accessibilityLabel="selfiecamerascreen.messagewait"
  169. style={styles.descriptionInformation}
  170. >
  171. {i18n.t('onboardingDocumentMessageWait')}
  172. </Text>
  173. </>
  174. )}
  175.  
  176. {!isLoading && !hasError && (
  177. <View
  178. accessibilityLabel="selfiecamerascreen.containerwrapperstyle.responsewithstyle"
  179. style={styles.containerViewResponseStyle}
  180. >
  181.  
  182. <View
  183. accessibilityLabel="selfiecamerascreen.containerwrappercircle.responseerror"
  184. style={styles.containerViewCircle}
  185. >
  186. <Heading
  187. accessibilityLabel="selfiecamerascreen.circleinformation.responseerrortext"
  188. type="h1"
  189. style={styles.circleInformation}
  190. >
  191. {i18n.t('onboardingDocumentSelfieErroSymbol')}
  192. </Heading>
  193. </View>
  194.  
  195. <Heading
  196. accessibilityLabel="selfiecamerascreen.informationtitle"
  197. type="h1"
  198. style={styles.titleInformation}
  199. >
  200. {i18n.t('onboardingDocumentSelfieErroTitle')}
  201. </Heading>
  202.  
  203. <Paragraph
  204. accessibilityLabel="selfiecamerascreen.informationdescription"
  205. type="default"
  206. align="center"
  207. style={styles.descriptionInformation}
  208. >
  209. {errorTOC}
  210. </Paragraph>
  211. <Button
  212. accessibilityLabel="selfiecamerascreen.buttonreregiter"
  213. title={i18n.t('onboardingDocumentReRegisterButton')}
  214. type="secondaryOnboarding"
  215. size="block"
  216. onPress={this.onPressReRegister}
  217. style={styles.buttonreregister}
  218. />
  219.  
  220. </View>
  221. )}
  222.  
  223. </View>
  224. ) : (
  225. <>
  226. <View
  227. accessibilityLabel="selfiecamerascreen.containerwrappersvg"
  228. style={styles.containerFaceSvg}
  229. >
  230. <FaceTarget
  231. accessibilityLabel="selfiecamerascreen.facetarget.resourcesvg"
  232. />
  233. </View>
  234.  
  235. <Button
  236. accessibilityLabel="selfiecamerascreen.buttonselfielive.opensdkTOC"
  237. title={i18n.t('onboardingDocumentTakeSelfieButton')}
  238. type="darkOnboarding"
  239. size="md"
  240. onPress={this.onPressSelfieLive}
  241. disabled={isLoading}
  242. style={styles.buttonselfie}
  243. />
  244. </>
  245. )}
  246.  
  247. </SafeAreaView>
  248. );
  249. }
  250. }
  251.  
  252. const styles = StyleSheet.create({
  253. container: {
  254. flex: 1,
  255. backgroundColor: Colors.selfieBackground,
  256. },
  257. containerViewResponse: {
  258. flex: 1,
  259. justifyContent: 'center',
  260. },
  261. containerViewResponseStyle: {
  262. flex: 1,
  263. justifyContent: 'center',
  264. alignItems: 'center',
  265. margin: 20,
  266. },
  267. containerViewCircle: {
  268. borderRadius: 25,
  269. width: 50,
  270. height: 50,
  271. backgroundColor: Colors.errorTOC,
  272. justifyContent: 'center',
  273. alignItems: 'center',
  274. },
  275. circleInformation: {
  276. fontSize: 20,
  277. fontWeight: 'bold',
  278. color: 'white',
  279. },
  280. titleInformation: {
  281. fontSize: 24,
  282. marginTop: 16,
  283. color: 'white',
  284. },
  285. descriptionInformation: {
  286. fontSize: 14,
  287. color: 'white',
  288. textAlign: 'center',
  289. },
  290. containerFaceSvg: {
  291. position: 'absolute',
  292. top: 0,
  293. left: 0,
  294. width: '100%',
  295. height: '100%',
  296. alignItems: 'center',
  297. justifyContent: 'space-around',
  298. zIndex: 0,
  299. },
  300. activityIndicator: {
  301. backgroundColor: Colors.activityIndicatorOnBoarding,
  302. width: 50,
  303. height: 50,
  304. alignItems: 'center',
  305. justifyContent: 'center',
  306. borderRadius: 25,
  307. alignSelf: 'center',
  308. marginBottom: 20,
  309. },
  310. buttonreregister: {
  311. marginTop: 10,
  312. alignSelf: 'center',
  313. bottom: 0,
  314. },
  315. buttonselfie: {
  316. marginBottom: 20,
  317. alignSelf: 'center',
  318. position: 'absolute',
  319. bottom: 0,
  320. },
  321. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement