From a43b011387621303f2c5429f60743fc4ca45cf58 Mon Sep 17 00:00:00 2001 From: Hugo Falcao Date: Mon, 18 Apr 2022 21:17:25 -0300 Subject: [PATCH] Page cadastro finalizada --- src/pages/Cadastro.css | 4 ++ src/pages/Cadastro.tsx | 126 +++++++++++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 36 deletions(-) diff --git a/src/pages/Cadastro.css b/src/pages/Cadastro.css index 912ec27..80598b6 100644 --- a/src/pages/Cadastro.css +++ b/src/pages/Cadastro.css @@ -1,3 +1,7 @@ #nome-sobrenome{ display: flex; +} + +.toast-notification{ + --background: rgb(255, 0, 0); } \ No newline at end of file diff --git a/src/pages/Cadastro.tsx b/src/pages/Cadastro.tsx index bb62f33..653280b 100644 --- a/src/pages/Cadastro.tsx +++ b/src/pages/Cadastro.tsx @@ -1,4 +1,4 @@ -import { IonProgressBar, IonItem, IonLabel, IonInput, IonBackButton, IonButton, IonButtons, IonCardTitle, IonCol, IonContent, IonGrid, IonHeader, IonPage, IonRow, IonToolbar } from '@ionic/react'; +import { IonToast, IonProgressBar, IonItem, IonLabel, IonInput, IonBackButton, IonButton, IonButtons, IonCardTitle, IonCol, IonContent, IonGrid, IonHeader, IonPage, IonRow, IonToolbar } from '@ionic/react'; import { arrowBack, logoFacebook, mail } from 'ionicons/icons'; import { Action } from '../components/Action'; import { useEffect, useState } from 'react'; @@ -10,55 +10,100 @@ import * as UsersService from '../services/users' const Cadastro: React.FC = () => { const history = useHistory(); - const [ errors, setErrors ] = useState(false); + const [showToast, setShowToast] = useState(false); + const [messageToast, setMessageToast ] = useState(''); - const [email, setEmail] = useState({}); + const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const [confirmPassword, setConfirmPassword] = useState(''); const [firstName, setFirstName] = useState(''); const [lastName, setLastName] = useState(''); const [birthDate, setBirthDate] = useState(''); + const [lResult, setlResult] = useState({ + error: '', + success: true + }); - const handleSubmit = async () => { - // setDisableSubmitButton(true) - // event.preventDefault(); - // const data = new FormData(event.currentTarget); + const emailValidate = () => { + var usuario = email.substring(0, email.indexOf("@")); + var dominio = email.substring(email.indexOf("@") + 1, email.length); - const signUpForm = { - name: firstName +' '+ lastName, - email: email, - birth_date: birthDate, - password: password + if ((usuario.length >= 1) && + (dominio.length >= 3) && + (usuario.search("@") == -1) && + (dominio.search("@") == -1) && + (usuario.search(" ") == -1) && + (dominio.search(" ") == -1) && + (dominio.search(".") != -1) && + (dominio.indexOf(".") >= 1) && + (dominio.lastIndexOf(".") < dominio.length - 1)) + { + return true; + } else { + return false; } + }; - console.log(signUpForm); + const clearResult = () => { + lResult.error = ''; + lResult.success = true; + } - await UsersService.create(signUpForm).catch(error => { - if (!error.response) return + const fieldValidate = async () => { + clearResult(); - if (error.response.data.message) { - // setAlertContent(error.response.data.message); - } else { - // setAlertContent('Houve um erro ao realizar o cadastro.'); + if(!emailValidate()) { + lResult.error = 'O EMAIL é inválido!'; + lResult.success = false; + return lResult; + } else if(password.length < 7 || password.length > 12) { //TODO: validar de acordo com a documentação + lResult.error = 'A senha deve ter de 7 a 12 caracteres!'; + lResult.success = false; + return lResult; } - // setAlertSeverity('error'); - // setAlert(true); - // setDisableSubmitButton(false) - }).then(response => { - if (!response) return; - - history.push( - { - pathname: '/home', - state: { - detail: 'signedUp', - alertSeverity: 'success', - alertContent: 'Usuário cadastrado com sucesso!' - } + return lResult; + }; + + const handleSubmit = async () => { + + if(name != '' && email != '' && birthDate != '' && password != '' && confirmPassword != '') { + if(password === confirmPassword){ + const signUpForm = { + name: firstName +' '+ lastName, + email: email, + birth_date: birthDate, + password: password } - ) - }); + + let result = fieldValidate(); + if((await result).success) { + + let retorno = await UsersService.create(signUpForm); + console.log(retorno); + if(retorno.token) { + // let signIn = await Api.signIn(email, passwordField); + // if(signIn.token) { + // await AsyncStorage.setItem('token', signIn.token); + // await AsyncStorage.setItem('cpf', retorno.cpf); + history.push('home'); + + } else { + setMessageToast(retorno.message); + setShowToast(true); + } + } else{ + setMessageToast(lResult.error); + setShowToast(true); + } + } else { + setMessageToast('As senhas devem ser iguais!'); + setShowToast(true); + } + } else { + setMessageToast('Nenhum campo pode ser nulo!'); + setShowToast(true); + } }; const { name } = useParams<{ name: string; }>(); @@ -76,7 +121,8 @@ const Cadastro: React.FC = () => { - Como você deseja se cadastrar? + {/* Como você deseja se cadastrar? */} + Cadastro {/* @@ -154,6 +200,14 @@ const Cadastro: React.FC = () => { {/*
*/} + setShowToast(false)} + message={messageToast} + duration={2500} + /> );