RegisterImage.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { useEffect, useState } from 'react';
  2. import { ImageBackground, StyleSheet, View, Dimensions, TouchableOpacity, Image, Alert } from 'react-native';
  3. import { ActivityIndicator, Text, } from 'react-native-paper'
  4. import 'react-native-get-random-values';
  5. import { useRegistrationFormState } from '../contexts/RegisterContext';
  6. import { launchImageLibrary, launchCamera } from 'react-native-image-picker'
  7. import axios from 'axios';
  8. import Globals from '../globals';
  9. const RegisterImage = ({navigation}) => {
  10. const { dispatch, state } = useRegistrationFormState();
  11. const [photoFile, setPhoto] = useState(null);
  12. const uploadImage = () => {
  13. launchImageLibrary({
  14. mediaType: 'photo',
  15. cameraType: 'back',
  16. includeBase64: true
  17. }, (photo) => {
  18. setPhoto(photo)
  19. }
  20. );
  21. }
  22. const register = async () => {
  23. dispatch({ type: "SUBMIT"})
  24. let formData = new FormData();
  25. formData.append('userInfo', JSON.stringify({
  26. username: state.username,
  27. email: state.email,
  28. password: state.password,
  29. height: state.height,
  30. weight: state.weight,
  31. }));
  32. formData.append('avatar', photoFile);
  33. let resultMessage = '';
  34. try {
  35. const response = await axios({
  36. method: 'post',
  37. url: Globals.API_HOST + '/api/register/',
  38. timeout: 5000,
  39. data: formData,
  40. });
  41. resultMessage = response.statusText;
  42. } catch (error) {
  43. resultMessage = error.toString();
  44. }
  45. dispatch({ type: "SUBMISSION_RECIEVED"});
  46. navigation.navigate('RegisterResult', {result: resultMessage});
  47. }
  48. return (
  49. <ImageBackground style={styles.container} source={require('../assets/cover-dark.png')}>
  50. { !state.isSubmitLoading ?
  51. <View>
  52. <View style={styles.brandContainer}>
  53. <Text style={styles.title} >Lastly, you can choose upload a profile picture.</Text>
  54. </View>
  55. <View style={{justifyContent: 'center', }}>
  56. { photoFile != null ?
  57. <Image style={{alignSelf: 'center', borderRadius: 125, width: 250, height: 250, marginBottom: 50}} source={{uri: 'data:image/png;base64,' + photoFile.base64}} /> :
  58. <Image style={{alignSelf: 'center', borderRadius: 125, width: 250, height: 250, marginBottom: 50}} source={require('../assets/default-pfp.png')} />
  59. }
  60. </View>
  61. <View style={{margin: 20}}>
  62. <TouchableOpacity onPress={uploadImage} style={styles.nextBtn} >
  63. <Text style={{ color: 'white', fontSize: 20 }}>Choose photo</Text>
  64. </TouchableOpacity>
  65. <TouchableOpacity onPress={register} style={styles.nextBtn} >
  66. <Text style={{ color: 'white', fontSize: 20 }}>Register</Text>
  67. </TouchableOpacity>
  68. </View>
  69. </View>
  70. :
  71. <View>
  72. <Text style={[{marginTop: 70, marginLeft: 20}, styles.title]} >Hang tight for a moment while we create your account.</Text>
  73. <ActivityIndicator style={{marginTop: 200}} size="large" animating={true} color="red"/>
  74. </View>}
  75. </ImageBackground>
  76. )
  77. }
  78. const styles = StyleSheet.create({
  79. container: {
  80. position: 'absolute',
  81. width: Dimensions.get('window').width,
  82. height: Dimensions.get('window').height,
  83. flex: 1,
  84. flexDirection: 'column',
  85. resizeMode: 'cover',
  86. },
  87. brandContainer: {
  88. marginTop: 70,
  89. height: 50,
  90. marginBottom: 70,
  91. marginLeft: 25,
  92. flexDirection: 'column',
  93. justifyContent: 'space-evenly'
  94. },
  95. title: {
  96. fontFamily: 'RacingSansOne-Regular',
  97. color: 'white',
  98. fontSize: 20,
  99. },
  100. subtitle: {
  101. fontFamily: 'RacingSansOne-Regular',
  102. color: 'white',
  103. fontSize: 15
  104. },
  105. title: {
  106. fontFamily: 'RacingSansOne-Regular',
  107. color: 'white',
  108. fontSize: 20
  109. },
  110. subtitle: {
  111. fontFamily: 'RacingSansOne-Regular',
  112. color: 'white',
  113. fontSize: 15,
  114. margin: 20
  115. },
  116. textContainer: {
  117. marginHorizontal: 20,
  118. marginBottom: 30,
  119. borderRadius: 50,
  120. },
  121. textInput: {
  122. marginVertical: 7,
  123. paddingLeft: 30,
  124. borderRadius: 50,
  125. overflow: 'hidden',
  126. backgroundColor: 'white',
  127. height: 50
  128. },
  129. errorText: {
  130. alignSelf: 'flex-end',
  131. color: 'red',
  132. fontSize: 15
  133. },
  134. errorInput: {
  135. borderWidth: 2,
  136. borderColor: 'red'
  137. },
  138. nextBtn: {
  139. marginHorizontal: 20,
  140. borderRadius: 50,
  141. justifyContent: 'center',
  142. backgroundColor: '#df3f3f',
  143. margin: 5,
  144. height: 50,
  145. alignItems: 'center'
  146. },
  147. backBtn: {
  148. marginHorizontal: 20,
  149. marginBottom: 10,
  150. borderRadius: 50,
  151. borderColor: 'white',
  152. borderWidth: 2,
  153. justifyContent: 'center',
  154. backgroundColor: 'transparent',
  155. margin: 5,
  156. height: 50,
  157. alignItems: 'center'
  158. },
  159. })
  160. export default RegisterImage;