Created
August 3, 2020 17:20
-
-
Save lucascardial/82aac0f9b90d4f0dacb6c970f3315d72 to your computer and use it in GitHub Desktop.
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
import React, { useCallback, useRef, useEffect } from 'react'; | |
import { | |
Image, | |
View, | |
ScrollView, | |
KeyboardAvoidingView, | |
Platform, | |
TextInput, | |
Alert, | |
Text, | |
} from 'react-native'; | |
import { Form } from '@unform/mobile'; | |
import { FormHandles } from '@unform/core'; | |
import * as Yup from 'yup'; | |
import { useSelector, useDispatch } from 'react-redux'; | |
import getValidationErrors from '../../utils/get-validation-errors'; | |
import DeliveryMotorcycle from '../../assets/img/logo.png'; | |
import Input from '../../components/input'; | |
import Button from '../../components/button'; | |
import { navigate } from '../../config/navigation'; | |
import { | |
Container, | |
Title, | |
ForgotPassword, | |
ForgotPasswordText, | |
TextError, | |
} from './styles'; | |
import { SignInFormData } from './signin.props'; | |
import { RootState } from 'src/store/reducers'; | |
import { requestAuthAction } from '../../store/modules/auth/actions'; | |
const SignIn: React.FC = () => { | |
const formRef = useRef<FormHandles>(null); | |
const passwordInputRef = useRef<TextInput>(null); | |
const dispatch = useDispatch(); | |
const handleSignIn = useCallback(async (data: SignInFormData) => { | |
try { | |
formRef.current?.setErrors({}); | |
const schemaValidator = Yup.object().shape({ | |
email: Yup.string() | |
.required('E-mail obrigatório') | |
.email('Digite um e-mail válido'), | |
password: Yup.string().min(4, 'No mínimo 4 dígitos'), | |
}); | |
await schemaValidator.validate(data, { | |
abortEarly: false, | |
}); | |
dispatch(requestAuthAction(data)); | |
} catch (error) { | |
const errors = getValidationErrors(error); | |
formRef.current?.setErrors(errors); | |
console.log('unform:', { errors }); | |
Alert.alert( | |
'Erro na autenticação', | |
'Ocorreu um erro ao fazer login, cheque suas credenciais', | |
); | |
} | |
}, []); | |
const authState = (state: RootState) => state.auth; | |
const auth = useSelector(authState); | |
useEffect(() => { | |
if (auth.errors && !auth.loading) { | |
formRef.current?.setErrors({ | |
email: '!', | |
password: '!', | |
}); | |
} | |
}, [auth]); | |
return ( | |
<> | |
<KeyboardAvoidingView | |
style={{ flex: 1 }} | |
behavior={Platform.OS === 'ios' ? 'padding' : undefined} | |
enabled | |
> | |
<ScrollView | |
contentContainerStyle={{ flex: 1 }} | |
keyboardShouldPersistTaps="handled" | |
> | |
<Container> | |
<Image source={DeliveryMotorcycle} /> | |
<View> | |
<Title>Login do Entregador</Title> | |
</View> | |
{!!auth.errors && ( | |
<View> | |
<TextError> {auth.errors}</TextError> | |
</View> | |
)} | |
<Form ref={formRef} onSubmit={handleSignIn}> | |
<Input | |
name="email" | |
icon="mail" | |
placeholder="E-mail" | |
autoCapitalize="none" | |
autoCorrect={false} | |
keyboardType="email-address" | |
returnKeyType="next" | |
onSubmitEditing={() => { | |
passwordInputRef.current?.focus(); | |
}} | |
/> | |
<Input | |
ref={passwordInputRef} | |
name="password" | |
icon="lock" | |
placeholder="Senha" | |
secureTextEntry | |
returnKeyType="send" | |
onSubmitEditing={() => { | |
formRef.current?.submitForm(); | |
}} | |
/> | |
</Form> | |
<Button | |
isLoading={auth.loading} | |
onPress={() => { | |
formRef.current?.submitForm(); | |
}} | |
> | |
Entrar | |
</Button> | |
<ForgotPassword onPress={() => navigate('ForgotPage')}> | |
<ForgotPasswordText> | |
Esqueci minha senha | |
</ForgotPasswordText> | |
</ForgotPassword> | |
</Container> | |
</ScrollView> | |
</KeyboardAvoidingView> | |
</> | |
); | |
}; | |
export default SignIn; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment