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

13.33% Statements 2/15
0% Branches 0/10
0% Functions 0/8
13.33% Lines 2/15

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              3x                                                                                                                                   3x                                                                                                                
import React from 'react';
import { Controller, useForm } from 'react-hook-form';
import { StyleSheet, TextInput, TouchableOpacity, View } from 'react-native';
import 'react-native-get-random-values';
import { Text } from 'react-native-paper';
import { useRegistrationFormState } from '../../../contexts/RegisterContext';
 
const RegisterCredentials = ({changeStep}) => {
  const { dispatch, state: { password, } } = useRegistrationFormState();
  const { control, handleSubmit, watch, formState: {errors} } = useForm({
    mode: 'all'
  });
 
  const goBack = (formData) => {
    changeStep(1, "Let's start with some basic account information");
  }
 
  const next = (formData) => {
    dispatch({ type: "PASSWORD_CHANGE", payload: formData.password });
    changeStep(3, "Will you be using a Sagitta?");
  }
 
  return (
        <View>
          <View style={{margin: 20}}>
            <Controller 
              defaultValue={password}
              control={control}
              render={({ field: { onChange, value }}) => (
                <TextInput 
                  mode="outlined" 
                  style={[styles.textInput, errors.username && styles.errorInput]} 
                  placeholder="Password" 
                  autoCompleteType="password"
                  secureTextEntry={true}
                  value={value}
                  onChangeText={value => onChange(value)} />
              )}
              rules={{
                required: true, 
                pattern: 
                {
                  value: /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[\w~@#$%^&*+=`|{}:;!.?\"()\[\]-]{8,}$/
                }}}
              name="password"/>
              {errors.password?.type === "required" && <Text style={styles.errorText}>Password is required.</Text>}
              {errors.password?.type === "pattern" && <Text style={styles.errorText}>Password must be at least 8 characters and contain one lowercase letter, one captial letter, and one number.</Text>}
            <Controller 
              defaultValue={password}
              control={control}
              render={({ field: { onChange, value }}) => (
                <TextInput 
                  mode="outlined" 
                  style={[styles.textInput, errors.username && styles.errorInput]} 
                  placeholder="Confirm password" 
                  secureTextEntry={true}
                  value={value}
                  onChangeText={value => onChange(value)} />
            )}
            rules={{validate: value => value === watch('password')}}
            name="confirmPassword"/>
            {errors.confirmPassword && <Text style={styles.errorText}>Passwords do not match.</Text>}
          </View>
          <TouchableOpacity onPress={handleSubmit(next)} style={styles.nextBtn} >
              <Text style={{ color: 'white', fontSize: 20 }}>Next</Text>
          </TouchableOpacity>
          <TouchableOpacity onPress={goBack} style={styles.backBtn} >
              <Text style={{ color: 'white', fontSize: 20 }}>Back</Text>
          </TouchableOpacity>
      </View>
  )
}
 
const styles = StyleSheet.create({backBtn: {
    marginHorizontal: 20,
    marginBottom: 10,
    borderRadius: 50,
    borderColor: 'white',
    borderWidth: 2,
    justifyContent: 'center',
    backgroundColor: 'transparent',
    margin: 5,
    height: 50,
    alignItems: 'center'
  },
    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,
      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 RegisterCredentials;