All files / atlas-mobile-ts/src/components/Auth RegisterMain.tsx

0% Statements 0/35
0% Branches 0/13
0% Functions 0/7
0% Lines 0/34

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                                                                                                                                                                                                                                                                                                                                                     
/* Copyright (C) Click & Push Accessibility, Inc - All Rights Reserved
 * Unauthorized copying of this file, via any medium is strictly prohibited
 * Proprietary and confidential
 * Written and maintained by the Click & Push Development team 
 * <dev@clicknpush.ca>, January 2022
 */
 
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 { UnAuthStackNavigationProp } from '../../navigation/UnauthorizedNavigator';
import { testTag } from '../../utils/GlobalUtils';
import { PrimaryButton } from '../Buttons';
import UnauthorizedLayout from './AuthLayout';
import RegisterCredentials from './RegistrationSteps/RegisterCredential';
import RegisterImage from './RegistrationSteps/RegisterImage';
import RegisterMeasurements from './RegistrationSteps/RegisterMeasurements';
import RegisterPassword from './RegistrationSteps/RegisterPassword';
 
/**
 * {@link RegisterMain} component props.
 */
export interface RegisterProps {
  /**The navigation object used to interact with the {@link Auth} navigator.*/
  navigation: UnAuthStackNavigationProp
}
 
export interface RegisterStepProps {
  changeStep: (stepNumber: number, stepSubtitleText: string) => void;
  setFormValues: (formValues: RegisterFormValues) => void
  formValues: RegisterFormValues
}
 
export interface RegisterFormValues {
  username?: string;
  email?: string;
  password?: string;
  confirmPassword?: string;
  sagitta?: false
  height?: string;
  weight?: string;
}
 
/**
 * The main registration component. Acts as a backdrop for all intermediate registration steps. 
 * **Note: The registration components make heavy use of a library called Formik in order to created validated forms. It's recommended to check out the docs for that 
 * (https://formik.org/docs/guides/react-native)**
 * @category Unauthorized
 * @subcategory Registration
 * @component
 */
export const RegisterMain : React.FC<RegisterProps> = ({navigation}) => {
  
  /**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<string>(subTitleState);
 
  /**
   * An object containing all the current form values
   */
  const [formValues, setFormValues] = useState<RegisterFormValues>({}); 
 
  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) => {
        // make sure we aren't at the last step for some reason
        Iif (step != 5) {    
          e.preventDefault();
          
          // display an "Are you sure?" alert
           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: () => {
                  // if they are sure, reset the form and take them back to the intro
                  setFormValues({});
                  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);
    console.log("[Registration]: Changing to registration step " + step + " with subtitle:" + subtitle)
  }
 
  /**
   * Returns to the intro screen.
   */
  const finish = () => {
    navigation.pop();
    console.log("[Registration]:  Going back to intro screen from registration")
  }
 
  return (
    <UnauthorizedLayout>
      <View style={styles.brandContainer} {...testTag('registerWelcomeContainer')}>
        {step != 5 ?
        <Text style={styles.title} >Welcome!</Text> : null}
        <Text style={styles.subtitle}>{subtitle}</Text>
      </View>
      <View>{
        step == 1 ? <RegisterCredentials setFormValues={setFormValues} formValues={formValues} changeStep={changeStep}/> : 
        step == 2 ? <RegisterPassword setFormValues={setFormValues} formValues={formValues} changeStep={changeStep}/> : 
        step == 3 ? <RegisterMeasurements setFormValues={setFormValues} formValues={formValues} changeStep={changeStep}/> : 
        step == 4 ? <RegisterImage setFormValues={setFormValues} formValues={formValues} changeStep={changeStep} /> :
        step == 5 ? <PrimaryButton style={{marginTop: 30}} text="Okay" onPress={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
  },
});