All files / atlas/src/components/Auth/Registration RegisterMain.tsx

9.52% Statements 2/21
0% Branches 0/14
0% Functions 0/7
9.52% Lines 2/21

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                                                        3x                                                                                                                                                               3x                                                    
import { StackNavigationProp } from '@react-navigation/stack';
import React, { useEffect, useState } from 'react';
import { Alert, Dimensions, StyleSheet, View } from 'react-native';
import 'react-native-get-random-values';
import { Text } from 'react-native-paper';
import { useRegistrationFormState } from '../../../contexts/RegisterContext';
import { AuthStackNavigationProp, AuthStackParamList } from '../../../navigation/UnauthorizedNavigator';
import UnauthorizedLayout from '../AuthLayout';
import RegisterCredentials from './RegisterCredentials';
import RegisterImage from './RegisterImage';
import RegisterMeasurements from './RegisterMeasurements';
import RegisterPassword from './RegisterPassword';
import RegisterResult from './RegistrationResult';
 
/**
 * {@link RegisterMain} component props.
 */
export interface RegisterProps {
  /**The navigation object used to interact with the {@link Auth} navigator.*/
  navigation: AuthStackNavigationProp
}
 
/**
 * The main registration component. Acts as a backdrop for all intermediate registration steps.
 * @category Unauthorized
 * @subcategory Registration
 * @component
 */
export const RegisterMain : React.FC<RegisterProps> = ({navigation}) => {
  const { dispatch, state } = useRegistrationFormState();
  /**Holds an integer value that maps to a step in the registration process. 
   * The mapping is as follows:
   * - 1 -> Username and Email 
   * - 2 -> Password
   * - 3 -> Sagitta and Measurements
   * - 4 -> Profile picture and submission
   * - 5 -> Result
   */
  const stepState = 1;
  const [step, setStep] = useState(stepState);
  /**
   * Holds the current subtitle text.
   */
  const subTitleState = "Let's start with some basic account information.";
  const [subtitle, setSubtitle] = useState(subTitleState);
 
  useEffect(() => {
      /**
       * Handler that intercepts a request to leave the registration screen and asks the user to confirm that they want to leave.
       * If they confirm they want to leave, all registration state will reset and the user will be directed back to {@link Intro}.
       * @memberOf RegisterMain
       */
      const preventExitFromRegistration = navigation.addListener('beforeRemove', (e) => {
        if (step != 5) {    
        e.preventDefault();
        
        Alert.alert(
          'Going so soon?',
          'Are you sure you want to cancel registration?',
          [
            { text: "Don't leave", style: 'cancel', onPress: () => {} },
            {
              text: 'Discard',
              style: 'destructive',
              onPress: () => {
                dispatch({ type: "RESET" });
                navigation.dispatch(e.data.action)
              },
            },
          ]
        );
      }})
      return preventExitFromRegistration;
    }, [navigation, step]);
 
  /**
   * Changes the registration step to the given value.
   */
  const changeStep = (step: any, subtitle: any) => {
    setStep(step);
    setSubtitle(subtitle);
  }
 
  /**
   * Returns to the intro screen.
   */
  const finish = () => {
    navigation.pop();
  }
 
  return (
    <UnauthorizedLayout>
      <View style={styles.brandContainer}>
        {step != 5 ?
        <Text style={styles.title} >Welcome!</Text> : null}
        <Text style={styles.subtitle} >{subtitle}</Text>
      </View>
      <View>
        {step == 1 ? <RegisterCredentials changeStep={changeStep}/> : 
          step == 2 ? <RegisterPassword changeStep={changeStep}/> : 
          step == 3 ? <RegisterMeasurements changeStep={changeStep}/> : 
          step == 4 ? <RegisterImage changeStep={changeStep} /> :
          step == 5 ? <RegisterResult finish={finish}/> : null}
      </View>
    </UnauthorizedLayout>
  )
}
 
const styles = StyleSheet.create({
  container: {
    position: 'absolute',
    width: Dimensions.get('window').width,
    height: Dimensions.get('window').height,
    flex: 1,
    justifyContent: 'flex-start',
    resizeMode: 'cover',
  },
  brandContainer: {
    marginTop: 60,  
    marginHorizontal: 25,
    flexDirection: 'column',
    justifyContent: 'space-evenly'
  },
  title: {
    color: 'white',
    fontSize: 20
  },
 
  subtitle: {
    color: 'white',
    fontSize: 15,
    marginTop: 20
  },
});