Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
222
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. import * as React from 'react';
  2. import {InjectedFormProps, reduxForm} from 'redux-form';
  3. import {Text} from '../../_universal/Text/Text.style';
  4. import AuthenticationScreenWrapper
  5. from '../../../screens/AuthenticationScreens/_Components/AuthenticationScreenWrapper';
  6. import copy from '../../../shared/copy/index';
  7. import {SignUpHeader} from '../../../screens/AuthenticationScreens/SignUp/SignUp.styles';
  8. import * as Location from 'expo-location';
  9. import * as Permissions from 'expo-permissions';
  10. import {SignUp} from '../../../models/SignUp';
  11. import {Logger} from '../../../utils/Logger';
  12. import {Alert} from 'react-native';
  13. import {NavigationActions, StackActions} from 'react-navigation';
  14. import {Location as LocationUtils} from '../../../shared/functions/Localization';
  15. import STYLE = Logger.STYLE;
  16.  
  17. export namespace Localization {
  18. export interface Props {
  19. formValues?: formValues;
  20. navigate?: (routeName: any) => void;
  21. updateForm?: (
  22. form: string,
  23. field: string,
  24. value: any,
  25. touch?: boolean,
  26. persistentSubmitErrors?: boolean,
  27. ) => void;
  28. dispatchSignUp?:(data:SignUp) => Promise<boolean>;
  29. }
  30.  
  31. export type formValues = {
  32. [fieldName: string]: any;
  33. }
  34. }
  35.  
  36. class Localization extends React.Component<Localization.Props & InjectedFormProps<Localization.formValues>> {
  37.  
  38. state = {
  39. ...copy.localization.permissionApproved,
  40. permission: true,
  41. };
  42.  
  43. private _resetNavigation = () => {
  44. const {navigate} = this.props;
  45. const resetAction = StackActions.reset({
  46. index: 0,
  47. actions: [NavigationActions.navigate({routeName: 'Other'})],
  48. });
  49. navigate && navigate('Other');
  50. };
  51.  
  52. private _getLocationAsync = async () => {
  53. const {updateForm, navigate, dispatchSignUp, formValues} = this.props;
  54.  
  55. let {status} = await Permissions.askAsync(Permissions.LOCATION);
  56. if (status !== 'granted') {
  57. this.setState({
  58. errorMessage: 'Permission to access location was denied',
  59. });
  60. Logger.log('LOCALIZATION', status, undefined);
  61. this._setContent();
  62.  
  63. return;
  64. }
  65.  
  66. Logger.log('LOCALIZATION', status, undefined);
  67.  
  68. await Location.getCurrentPositionAsync()
  69. .then((res: Location.LocationData) => {
  70. return LocationUtils.parseAddressAsync(res)
  71. .then((readableLocation) => {
  72. updateForm && updateForm('sign_up_form', 'location', {
  73. coords: [res.coords.latitude, res.coords.longitude],
  74. readableLocation: readableLocation
  75. });
  76.  
  77. return {
  78. coords: [res.coords.latitude, res.coords.longitude],
  79. readableLocation: readableLocation
  80. };
  81. });
  82. })
  83. .then(async (locationData: any) => {
  84. if (formValues) {
  85. console.log('locationData', locationData)
  86. const signUpObj: SignUp = {
  87. email: formValues['email'].toLowerCase(),
  88. username: formValues['username'],
  89. password: formValues['password'],
  90. birthday: new Date(formValues['birthday']),
  91. verificationCode: formValues['verificationCode'],
  92. location: locationData,
  93. };
  94.  
  95. dispatchSignUp && await dispatchSignUp(signUpObj)
  96. .then(() => {
  97. this._resetNavigation();
  98. })
  99. .catch((err:any)=>{
  100. Alert.alert('Error', err)
  101. });
  102.  
  103. return signUpObj;
  104. }
  105. })
  106. .catch(err => Logger.log('GET_CURRENT_POSITION_ERROR', err, STYLE.ERROR));
  107.  
  108. };
  109.  
  110. private _setContent = () => {
  111. this.setState({...copy.localization.permissionDenied, permission: false});
  112. };
  113.  
  114. public render() {
  115. const {formValues, handleSubmit, submitting, valid, invalid} = this.props;
  116. const {header, content, button, permission} = this.state;
  117.  
  118. return (
  119. <AuthenticationScreenWrapper
  120. handleSubmit={() => this._getLocationAsync}
  121. invalid={false}
  122. valid={true}
  123. submitting={false}
  124. header={
  125. permission ?
  126. <SignUpHeader>
  127. {formValues && formValues['username'] + header}
  128. </SignUpHeader> :
  129. <SignUpHeader>
  130. {header}
  131. </SignUpHeader>
  132. }
  133. buttonTitle={button}
  134. buttonWidth={'100%'}
  135. >
  136. <Text style={{textAlign: 'center'}}>{content}</Text>
  137. </AuthenticationScreenWrapper>
  138. );
  139. }
  140. }
  141.  
  142. export const LocalizationForm = reduxForm<Localization.Props, Localization.formValues>({
  143. form: 'sign_up_form',
  144. destroyOnUnmount: false,
  145. })(Localization);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement