-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: convert class to functional component
- Loading branch information
1 parent
9c87f0a
commit 52c8c1d
Showing
1 changed file
with
69 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,95 @@ | ||
import React, { Component, Fragment } from 'react' | ||
import { Text, SafeAreaView, View, StyleSheet } from 'react-native' | ||
import { Formik } from 'formik' | ||
import * as Yup from 'yup' | ||
import FormInput from '../components/FormInput' | ||
import FormButton from '../components/FormButton' | ||
import ErrorMessage from '../components/ErrorMessage' | ||
import { withFirebaseHOC } from '../config/Firebase' | ||
import React from "react"; | ||
import { Text, SafeAreaView, View, StyleSheet } from "react-native"; | ||
import { Formik } from "formik"; | ||
import * as Yup from "yup"; | ||
import FormInput from "../components/FormInput"; | ||
import FormButton from "../components/FormButton"; | ||
import ErrorMessage from "../components/ErrorMessage"; | ||
import { withFirebaseHOC } from "../config/Firebase"; | ||
|
||
const validationSchema = Yup.object().shape({ | ||
email: Yup.string() | ||
.label('Email') | ||
.email('Enter a valid email') | ||
.required('Please enter a registered email') | ||
}) | ||
.label("Email") | ||
.email("Enter a valid email") | ||
.required("Please enter a registered email") | ||
}); | ||
|
||
class ForgotPassword extends Component { | ||
handlePasswordReset = async (values, actions) => { | ||
const { email } = values | ||
function ForgotPassword({ navigation, firebase }) { | ||
async function handlePasswordReset(values, actions) { | ||
const { email } = values; | ||
|
||
try { | ||
await this.props.firebase.passwordReset(email) | ||
console.log('Password reset email sent successfully') | ||
this.props.navigation.navigate('Login') | ||
await firebase.passwordReset(email); | ||
console.log("Password reset email sent successfully"); | ||
navigation.navigate("Login"); | ||
} catch (error) { | ||
actions.setFieldError('general', error.message) | ||
actions.setFieldError("general", error.message); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<SafeAreaView style={styles.container}> | ||
<Text style={styles.text}>Forgot Password?</Text> | ||
<Formik | ||
initialValues={{ email: '' }} | ||
onSubmit={(values, actions) => { | ||
this.handlePasswordReset(values, actions) | ||
}} | ||
validationSchema={validationSchema}> | ||
{({ | ||
handleChange, | ||
values, | ||
handleSubmit, | ||
errors, | ||
isValid, | ||
touched, | ||
handleBlur, | ||
isSubmitting | ||
}) => ( | ||
<Fragment> | ||
<FormInput | ||
name='email' | ||
value={values.email} | ||
onChangeText={handleChange('email')} | ||
placeholder='Enter email' | ||
autoCapitalize='none' | ||
iconName='ios-mail' | ||
iconColor='#2C384A' | ||
onBlur={handleBlur('email')} | ||
return ( | ||
<SafeAreaView style={styles.container}> | ||
<Text style={styles.text}>Forgot Password?</Text> | ||
<Formik | ||
initialValues={{ email: "" }} | ||
onSubmit={(values, actions) => { | ||
handlePasswordReset(values, actions); | ||
}} | ||
validationSchema={validationSchema} | ||
> | ||
{({ | ||
handleChange, | ||
values, | ||
handleSubmit, | ||
errors, | ||
isValid, | ||
touched, | ||
handleBlur, | ||
isSubmitting | ||
}) => ( | ||
<> | ||
<FormInput | ||
name="email" | ||
value={values.email} | ||
onChangeText={handleChange("email")} | ||
placeholder="Enter email" | ||
autoCapitalize="none" | ||
iconName="ios-mail" | ||
iconColor="#2C384A" | ||
onBlur={handleBlur("email")} | ||
/> | ||
<ErrorMessage errorValue={touched.email && errors.email} /> | ||
<View style={styles.buttonContainer}> | ||
<FormButton | ||
buttonType="outline" | ||
onPress={handleSubmit} | ||
title="Send Email" | ||
buttonColor="#039BE5" | ||
disabled={!isValid || isSubmitting} | ||
/> | ||
<ErrorMessage errorValue={touched.email && errors.email} /> | ||
<View style={styles.buttonContainer}> | ||
<FormButton | ||
buttonType='outline' | ||
onPress={handleSubmit} | ||
title='Send Email' | ||
buttonColor='#039BE5' | ||
disabled={!isValid || isSubmitting} | ||
/> | ||
</View> | ||
<ErrorMessage errorValue={errors.general} /> | ||
</Fragment> | ||
)} | ||
</Formik> | ||
</SafeAreaView> | ||
) | ||
} | ||
</View> | ||
<ErrorMessage errorValue={errors.general} /> | ||
</> | ||
)} | ||
</Formik> | ||
</SafeAreaView> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
backgroundColor: '#fff', | ||
backgroundColor: "#fff", | ||
marginTop: 150 | ||
}, | ||
text: { | ||
color: '#333', | ||
color: "#333", | ||
fontSize: 24, | ||
marginLeft: 25 | ||
}, | ||
buttonContainer: { | ||
margin: 25 | ||
} | ||
}) | ||
}); | ||
|
||
export default withFirebaseHOC(ForgotPassword) | ||
export default withFirebaseHOC(ForgotPassword); |