All files / atlas/src/components/Auth/Registration RegisterImage.js

6.67% Statements 2/30
0% Branches 0/8
0% Functions 0/6
6.67% Lines 2/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202                    3x                                                                                                                                                                                             3x                                                                                                                                                                                                
import React, { useEffect, useState } from 'react';
import { StyleSheet, View, Dimensions, TouchableOpacity, Image, } from 'react-native';
import {API_URL, reportAxiosError} from '../../../globals';
import { ActivityIndicator, Text, } from 'react-native-paper'
import 'react-native-get-random-values';
import { useRegistrationFormState } from '../../../contexts/RegisterContext';
import * as ImagePicker from 'expo-image-picker';
import axios from 'axios';
//import Icons from '../constants';
 
const RegisterImage = ({changeStep}) => {
  const { dispatch, state } = useRegistrationFormState();
  const [photoFile, setPhoto] = useState(null);
 
  const goBack = (formData) => {
    changeStep(3, "Will you be using a Sagitta?")
  }
 
  useEffect(() => {
    (async () => {
      if (Platform.OS !== 'web') {
        const { status } = await ImagePicker.requestMediaLibraryPermissionsAsync();
        if (status !== 'granted') {
          alert('Sorry, we need camera roll permissions to make this work!');
        }
      }
    })();
  }, []);
 
  const uploadImage = async () => {
    let result = await ImagePicker.launchImageLibraryAsync({
      mediaTypes: ImagePicker.MediaTypeOptions.Images,
      allowsEditing: true,
      aspect: [4, 3],
      quality: 1,
    });
 
    console.log(result)
  }
 
  const register = async () => {
    changeStep(4, 'Hang tight for a moment while we create your account.')
    dispatch({ type: "SUBMIT"})
    let formData = new FormData();
    formData.append('userInfo', JSON.stringify({
      username: state.username,
      email: state.email,
      password: state.password,
      height: state.height,
      weight: state.weight,
    }));
 
    formData.append('avatar', photoFile);
 
    let resultMessage = '';
 
    try {
      const response = await axios({
        method: 'post',
        url: API_URL + '/api/register/',
        timeout: 50000,
        data: formData,
      });
      resultMessage = response.statusText;
    } catch (error) {
      resultMessage = 'Something went wrong when trying to set up your account. Please try again.'; // TODO: add contact details?
      reportAxiosError('Something went wrong with user registration', error);
    }
 
    dispatch({ type: "SUBMISSION_RECIEVED"});
    changeStep(5, resultMessage);    
  }
 
  return (
    <View>
      { !state.isSubmitLoading ?
      <View>
        <View style={{justifyContent: 'center', }}>
          { photoFile != null ?
          <Image style={{alignSelf: 'center', borderRadius: 100, width: 200, height: 200, marginBottom: 5, marginTop: 30,}} source={{uri: 'data:image/png;base64,' + photoFile.base64}} /> :
          <Image style={{alignSelf: 'center', borderRadius: 100, width: 200, height: 200, marginBottom: 5, marginTop: 30,}} source={require('../../../../assets/default-pfp.png')} /> 
          }   
        </View>
        <View style={{margin: 20}}>
            <TouchableOpacity onPress={uploadImage} style={[styles.nextBtn, {marginBottom: 20}]} >
                <Text style={{ color: 'white', fontSize: 20, }}>Choose photo</Text>
            </TouchableOpacity>
            <View style={{height: 1, borderBottomWidth: 1, borderColor: 'lightgray', marginHorizontal: 20}}></View>
            <TouchableOpacity onPress={register} style={[styles.nextBtn,  {marginTop: 20}]} >
                <Text style={{ color: 'white', fontSize: 20,  }}>Register</Text>
            </TouchableOpacity>
            <TouchableOpacity onPress={back} style={styles.backBtn} >
                <Text style={{ color: 'white', fontSize: 20 }}>Back</Text>
            </TouchableOpacity>
        </View> 
      </View>
      : 
      <View>
          <ActivityIndicator style={{marginTop: 200}} size="large" animating={true} color="red"/>
      </View>}
    </View>
    
  )
}
 
const styles = StyleSheet.create({
    container: {
      position: 'absolute',
      width: Dimensions.get('window').width,
      height: Dimensions.get('window').height,
      flex: 1,
      flexDirection: 'column',
      resizeMode: 'cover',
    },
  
    brandContainer: {
      marginTop: 70,  
      height: 50,
      marginBottom: 70,
      marginLeft: 25,
      flexDirection: 'column',
      justifyContent: 'space-evenly'
    },
  
    title: {
      
      color: 'white',
      fontSize: 20,
    },
 
    subtitle: {
        
        color: 'white',
        fontSize: 15
      },
  
    title: {
      
      color: 'white',
      fontSize: 20
    },
 
    subtitle: {
        
        color: 'white',
        fontSize: 15,
        margin: 20
      },
  
    textContainer: { 
      marginHorizontal: 20,
      marginBottom: 30,
      borderRadius: 50,
    },
  
    textInput: {
      marginVertical: 7,
      paddingLeft: 30,
      borderRadius: 50,
      overflow: 'hidden',
      backgroundColor: 'white',
      height: 50
    },
 
    errorText: {
      alignSelf: 'flex-end',
      color: 'red',
      fontSize: 15
    },
 
    errorInput: {
      borderWidth: 2,
      borderColor: 'red'
    },
  
    nextBtn: {
      marginHorizontal: 20,
      marginBottom: 5,
      borderRadius: 50,
      justifyContent: 'center',
      backgroundColor: '#df3f3f',
      margin: 5,
      height: 50,
      alignItems: 'center'
    },
 
    backBtn: {
      marginHorizontal: 20,
      marginBottom: 10,
      borderRadius: 50,
      borderColor: 'white',
      borderWidth: 2,
      justifyContent: 'center',
      backgroundColor: 'transparent',
      margin: 5,
      height: 50,
      alignItems: 'center'
    },
  })
 
export default RegisterImage;