Merge branch 'master' of https://github.com/CloudAlb/tcc-vamos-frontend into feature/buscar-passageiro

This commit is contained in:
Hugo Falcao
2022-05-08 18:17:28 -03:00
13 changed files with 354 additions and 169 deletions

View File

@@ -1,11 +1,11 @@
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 { Action } from '../../components/Action';
import { useEffect, useState } from 'react';
import { useHistory, useParams } from 'react-router';
import './Cadastro.css';
import ModalExample from '../components/Email';
import * as UsersService from '../services/users'
import ModalExample from '../../components/Email';
import * as UsersService from '../../services/users'
const Cadastro: React.FC = () => {
const history = useHistory();

195
src/pages/Login.tsx Normal file
View File

@@ -0,0 +1,195 @@
import {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar
} from "@ionic/react";
import React, { useState } from "react";
import { IonGrid, IonRow, IonCol, IonToast } from "@ionic/react";
import { personCircle } from "ionicons/icons";
import { useHistory } from "react-router-dom";
import {
IonItem,
IonLabel,
IonInput,
IonButton,
IonIcon,
IonAlert,
} from "@ionic/react";
import * as sessionRoutes from '../services/session';
import LocalStorage from '../LocalStorage';
import { Action } from "../components/Action";
const Page: React.FC = () => {
const [showToast, setShowToast] = useState(false);
const [messageToast, setMessageToast ] = useState('');
const history = useHistory();
const [email, setEmail] = useState<string>("matheusalb3213@gmail.com");
const [password, setPassword] = useState<string>("123456");
const [isError, setIsError] = useState<boolean>(false);
const [message, setMessage] = useState<string>("");
function validateEmail(email: string) {
const re =
/^((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))$/;
return re.test(String(email).toLowerCase());
}
const validateForm = () => {
if (!email) {
setMessageToast("Por favor, informe o e-mail");
setShowToast(true);
return false;
}
if (!validateEmail(email)) {
setMessageToast("E-mail inválido");
setShowToast(true);
return false;
}
if (!password) {
setMessageToast("Por favor, digite a sua senha");
setShowToast(true);
return false;
}
if(password.length < 7 || password.length > 12) {
setMessageToast("A senha deve conter entre 7 e 12 dígitos");
setShowToast(true);
return false;
}
return true;
}
const handleLogin = async () => {
if (!validateForm()) {
return
}
const singinForm = {
login: email,
password: password,
};
await sessionRoutes.create(singinForm).then(response => {
if (response.status === 'error') {
setMessageToast(response.message);
setShowToast(true);
return
}
const { token } = response.token
LocalStorage.setToken(token);
history.push({ pathname: '/home' });
}).catch(error => {
// if (!error.response) return
// se o backend retornou uma mensagem de erro customizada
// if (error.response.data.message) {
console.dir('Houve um erro: ', { error })
alert('Houve um erro')
})
};
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Login</IonTitle>
</IonToolbar>
</IonHeader>
<IonRow>
<IonCol>
<IonIcon
style={{ fontSize: "70px", color: "#0040ff" }}
icon={personCircle}
/>
</IonCol>
</IonRow>
<IonContent fullscreen>
<IonHeader collapse="condense">
<IonToolbar>
<IonTitle size="large">Login</IonTitle>
</IonToolbar>
</IonHeader>
<IonGrid>
<IonRow>
<IonCol>
<IonAlert
isOpen={isError}
onDidDismiss={() => setIsError(false)}
cssClass="my-custom-class"
header={"Error!"}
message={message}
buttons={["Dismiss"]}
/>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<IonItem>
<IonLabel position="floating"> Email</IonLabel>
<IonInput
type="email"
value={email}
onIonChange={(e) => setEmail(e.detail.value!)}
></IonInput>
</IonItem>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<IonItem>
<IonLabel position="floating"> Senha</IonLabel>
<IonInput
type="password"
value={password}
onIonChange={(e) => setPassword(e.detail.value!)}
></IonInput>
</IonItem>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<p style={{ fontSize: "small" }}>
Clicando no botão de "Login", você concorda com a nossa{" "}
<a href="#">política de termos e serviços</a>
</p>
<IonButton expand="block" onClick={handleLogin}>
Login
</IonButton>
<p style={{ fontSize: "medium" }}>
<Action message="Ainda não possui uma conta?" text="Cadastre-se aqui!" link="/cadastro" />
</p>
</IonCol>
</IonRow>
</IonGrid>
<IonToast
// cssClass={"toast-notification"}
color='danger'
isOpen={showToast}
onDidDismiss={() => setShowToast(false)}
message={messageToast}
duration={2500}
/>
</IonContent>
</IonPage>
);
};
export default Page;

View File

@@ -1,162 +0,0 @@
import {
IonContent,
IonHeader,
IonPage,
IonTitle,
IonToolbar,
IonButtons,
} from "@ionic/react";
import React, { useState } from "react";
import axios from "axios";
import { IonGrid, IonRow, IonCol } from "@ionic/react";
import { personCircle } from "ionicons/icons";
import { useHistory } from "react-router-dom";
import {
IonItem,
IonLabel,
IonInput,
IonButton,
IonIcon,
IonAlert,
} from "@ionic/react";
function validateEmail(email: string) {
const re =
/^((?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\]))$/;
return re.test(String(email).toLowerCase());
}
const Page: React.FC = () => {
const history = useHistory();
const [email, setEmail] = useState<string>("matheusalb3213@gmail.com");
const [password, setPassword] = useState<string>("1234");
const [isError, setIsError] = useState<boolean>(false);
const [message, setMessage] = useState<string>("");
const handleLogin = () => {
// validação de inputs
if (!email) {
setMessage("Por favor, informe um e-mail válido");
setIsError(true);
return;
}
if (validateEmail(email) === false) {
setMessage("E-mail inválido");
setIsError(true);
return;
}
if (!password || password.length < 6) {
setMessage("Por favor, digite a sua senha");
setIsError(true);
return;
}
const loginData = {
email: email,
password: password,
};
const api = axios.create({
baseURL: `https://625dc16c4c36c7535779792c.mockapi.io/api/v1`,
});
api
// .post("/login", loginData)
.get("/users/2")
.then((res) => {
// login bem-sucedido
history.push("/dashboard/" + email);
})
.catch((error) => {
setMessage("Falha na autenticação! Por favor, crie uma conta");
setIsError(true);
});
};
return (
<IonPage>
<IonHeader>
<IonToolbar>
<IonTitle>Login</IonTitle>
</IonToolbar>
</IonHeader>
<IonRow>
<IonCol>
<IonIcon
style={{ fontSize: "70px", color: "#0040ff" }}
icon={personCircle}
/>
</IonCol>
</IonRow>
<IonContent fullscreen>
<IonHeader collapse="condense">
<IonToolbar>
<IonTitle size="large">Login</IonTitle>
</IonToolbar>
</IonHeader>
<IonGrid>
<IonRow>
<IonCol>
<IonAlert
isOpen={isError}
onDidDismiss={() => setIsError(false)}
cssClass="my-custom-class"
header={"Error!"}
message={message}
buttons={["Dismiss"]}
/>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<IonItem>
<IonLabel position="floating"> Email</IonLabel>
<IonInput
type="email"
value={email}
onIonChange={(e) => setEmail(e.detail.value!)}
></IonInput>
</IonItem>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<IonItem>
<IonLabel position="floating"> Senha</IonLabel>
<IonInput
type="password"
value={password}
onIonChange={(e) => setPassword(e.detail.value!)}
></IonInput>
</IonItem>
</IonCol>
</IonRow>
<IonRow>
<IonCol>
<p style={{ fontSize: "small" }}>
Clicando no botão de "LOGIN", você concorda com a nossa{" "}
<a href="#">política de termos e serviços</a>
</p>
<IonButton expand="block" onClick={handleLogin}>
Login
</IonButton>
<p style={{ fontSize: "medium" }}>
Ainda não possui uma conta? <a href="cadastro">Cadastre-se aqui!</a>
</p>
</IonCol>
</IonRow>
</IonGrid>
</IonContent>
</IonPage>
);
};
export default Page;