aboutsummaryrefslogtreecommitdiff
path: root/src/screens/onboarding/PasswordResetRequest.tsx
blob: 8f987721ef3d8368b7ff62f24c35542c6e22b251 (plain)
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
import {RouteProp} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import React, {useState} from 'react';
import {
  Alert,
  KeyboardAvoidingView,
  Platform,
  StatusBar,
  StyleSheet,
  Text,
  TouchableOpacity,
  View,
} from 'react-native';
import {trackPromise} from 'react-promise-tracker';
import {
  ArrowButton,
  Background,
  LoadingIndicator,
  TaggInput,
} from '../../components';
import {emailRegex, usernameRegex} from '../../constants';
import {OnboardingStackParams} from '../../routes';
import {handlePasswordResetRequest} from '../../services';
import {BackgroundGradientType} from '../../types';

type PasswordResetRequestRouteProp = RouteProp<
  OnboardingStackParams,
  'PasswordResetRequest'
>;
type PasswordResetRequestNavigationProp = StackNavigationProp<
  OnboardingStackParams,
  'PasswordResetRequest'
>;
interface PasswordResetRequestProps {
  route: PasswordResetRequestRouteProp;
  navigation: PasswordResetRequestNavigationProp;
}
/**
 * Password reset request page for getting username / email
 * @param navigation react-navigation navigation object
 */
const PasswordResetRequest: React.FC<PasswordResetRequestProps> = ({
  navigation,
}) => {
  const [form, setForm] = useState({
    value: '',
    isValid: false,
    attemptedSubmit: false,
  });

  const handleValueUpdate = (value: string) => {
    value = value.trim();

    //Entered field should either be a valid username or a valid email
    let isValid: boolean = emailRegex.test(value) || usernameRegex.test(value);

    setForm({
      ...form,
      value,
      isValid,
    });
  };

  const goToPasswordCodeVerification = async () => {
    if (!form.attemptedSubmit) {
      setForm({
        ...form,
        attemptedSubmit: true,
      });
    }
    try {
      if (form.isValid) {
        const success = await trackPromise(
          handlePasswordResetRequest(form.value),
        );
        if (success) {
          navigation.navigate('PasswordVerification', {
            id: form.value,
          });
        }
      } else {
        setForm({...form, attemptedSubmit: false});
        setTimeout(() => setForm({...form, attemptedSubmit: true}));
      }
    } catch (error) {
      Alert.alert(
        'Looks like our servers are down. 😓',
        "Try again in a couple minutes. We're sorry for the inconvenience.",
      );
      return {
        name: 'Send OTP error',
        description: error,
      };
    }
  };

  const Footer = () => (
    <View style={styles.footer}>
      <ArrowButton
        direction="backward"
        onPress={() => navigation.navigate('Login')}
      />
      <TouchableOpacity onPress={goToPasswordCodeVerification}>
        <ArrowButton
          direction="forward"
          disabled={!form.isValid}
          onPress={goToPasswordCodeVerification}
        />
      </TouchableOpacity>
    </View>
  );

  return (
    <Background
      style={styles.container}
      gradientType={BackgroundGradientType.Light}>
      <StatusBar barStyle="light-content" />
      <KeyboardAvoidingView
        behavior={Platform.OS === 'ios' ? 'padding' : 'height'}
        style={styles.container}>
        <View>
          <Text style={styles.description}>Enter your registered username</Text>
        </View>
        <TaggInput
          accessibilityHint="Enter a username"
          accessibilityLabel="Input field."
          placeholder="Username"
          autoCompleteType="username"
          textContentType="username"
          autoCapitalize="none"
          returnKeyType="go"
          onSubmitEditing={goToPasswordCodeVerification}
          onChangeText={handleValueUpdate}
          valid={form.isValid}
          invalidWarning={'You must enter a valid username / email'}
          attemptedSubmit={form.attemptedSubmit}
          width={280}
        />
        <LoadingIndicator />
      </KeyboardAvoidingView>
      <Footer />
    </Background>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  header: {
    ...Platform.select({
      ios: {
        top: 50,
      },
      android: {
        bottom: 40,
      },
    }),
  },
  formHeader: {
    color: '#fff',
    fontSize: 20,
    fontWeight: 'bold',
    alignSelf: 'flex-start',
    marginBottom: '6%',
    marginHorizontal: '10%',
  },
  load: {
    top: '5%',
  },
  description: {
    color: '#fff',
    fontWeight: '600',
    fontSize: 17,
    marginHorizontal: '10%',
  },
  footer: {
    width: '100%',
    flexDirection: 'row',
    justifyContent: 'space-around',
    ...Platform.select({
      ios: {
        bottom: '20%',
      },
      android: {
        bottom: '10%',
      },
    }),
  },
});

export default PasswordResetRequest;