diff --git a/src/components/fieldsSignUp/FormInput.jsx b/src/components/fieldsSignUp/FormInput.jsx index 972941d6e002c28c81e30e2c520750edd7408d06..3fa0c7ebbb9a0de822b597f283ead8ecb672bb03 100644 --- a/src/components/fieldsSignUp/FormInput.jsx +++ b/src/components/fieldsSignUp/FormInput.jsx @@ -21,7 +21,6 @@ function FormInput(props) { : "text" } autoComplete="off" - onFocusOut={props.verify} ></TextField> ); } diff --git a/src/contexts/useForm.js b/src/contexts/useForm.js index d7b17c235c1c1d4421f0157d9fcc4c4a3c572f23..e349fad3fc2e407aacbec311e366c97e3d5fe8f7 100644 --- a/src/contexts/useForm.js +++ b/src/contexts/useForm.js @@ -8,7 +8,7 @@ import { selectOptionsTesting, testSubformSchema, selectOptionTextTesting, - testTextValidation + testTextValidation, } from "../components/fieldsDisplayForm/utils/schemas"; import { pushTitle, @@ -16,7 +16,7 @@ import { pushSelect, pushRadio, pushCheckbox, - pushSubform + pushSubform, } from "../components/fieldsDisplayForm/utils/FormComposition"; import api from "../api"; @@ -238,7 +238,7 @@ const useForm = () => { */ async function setId() { const fetchData = async () => { - await api.get(`/form/${routeId}`).then(async function(res) { + await api.get(`/form/${routeId}`).then(async function (res) { let backForm = createFrontendForm(res.data); for (let i = 1; i < backForm.length; i++) { for (let j = 1; j < form.length; j++) { @@ -248,13 +248,13 @@ const useForm = () => { "question", "description", "options", - "subformId" + "subformId", ]) !== JSON.stringify(form[j], [ "question", "description", "options", - "subformId" + "subformId", ]) || differentValidation(backForm[i], form[j]) ) { @@ -279,14 +279,14 @@ const useForm = () => { const post_response = await api .put(`/form/${routeId}`, data, { headers: { - authorization: `bearer ${window.sessionStorage.getItem("token")}` - } + authorization: `bearer ${window.sessionStorage.getItem("token")}`, + }, }) - .then(function(error) { + .then(function (error) { if (!error.response) alert("Seu formulário foi atualizado com sucesso."); }) - .catch(function(error) { + .catch(function (error) { if (error.response.status === 401) { let path = `/signin`; history.push(path); @@ -304,15 +304,13 @@ const useForm = () => { const post_response = await api .post(`/form`, await createBackendForm(form), { headers: { - authorization: `bearer ${window.sessionStorage.getItem("token")}` - } + authorization: `bearer ${window.sessionStorage.getItem("token")}`, + }, }) - .then(function(error) { + .then(function (error) { if (!error.response) alert("Seu formulário foi criado com sucesso."); - else console.log("ERROR NO POST_RESPONSE", error); }) - .catch(function(error) { - console.log("ERROR NO POST RESPONSE", error.response); + .catch(function (error) { alert("Um erro ocorreu."); }); } @@ -338,7 +336,7 @@ const useForm = () => { setValidationValue, removeValidation, onDragEnd, - submit + submit, }; }; diff --git a/src/pages/SignIn.js b/src/pages/SignIn.js index c3c924465b6161aa82e649bdf5917fd8a6955d81..c86bc000112568a5b870ab5d72e29d15ccf8a975 100644 --- a/src/pages/SignIn.js +++ b/src/pages/SignIn.js @@ -9,7 +9,7 @@ import FormInput from "../components/fieldsSignUp/FormInput"; import Paper from "@material-ui/core/Paper"; import api from "../api"; -const useStyles = makeStyles(theme => ({ +const useStyles = makeStyles((theme) => ({ register: { maxWidth: "1000px", background: "#ffffff", @@ -17,22 +17,22 @@ const useStyles = makeStyles(theme => ({ padding: "2% 1%", margin: "0 auto", marginTop: "9%", - width: "95%" + width: "95%", }, custom_strong: { fontSize: "25px", textAlign: "center", display: "block", - color: "#46525d" + color: "#46525d", }, strong_description: { fontSize: "14px", - color: "#c2c6ca" + color: "#c2c6ca", }, form: { marginTop: "3%", alignItems: "center", - textAlign: "center" + textAlign: "center", }, button: { type: "submit", @@ -43,12 +43,12 @@ const useStyles = makeStyles(theme => ({ padding: "10px 20px", fontSize: "18px", "&:hover": { - backgroundColor: "rgb(25, 109, 23)" + backgroundColor: "rgb(25, 109, 23)", }, ["@media (max-width:550px)"]: { - width: "55%" - } - } + width: "55%", + }, + }, })); export default function SignIn() { const classes = useStyles(); @@ -56,13 +56,13 @@ export default function SignIn() { const [values, setValues] = React.useState({ email: "", password: "", - emailError: false + emailError: false, }); async function update(prop, event) { await setValues({ ...values, [prop]: event.target.value }); } - const handleChange = prop => event => { + const handleChange = (prop) => (event) => { if (!checkEmail()) { values.emailError = true; } else { @@ -71,10 +71,11 @@ export default function SignIn() { update(prop, event); }; function checkEmail() { - if (/^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[A-Za-z]+$/.test(values.email)) { - return true; - } - return false; + return values.email + ? /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(values.email) + ? true + : false + : true; } function verifyValues() { if (values.email && values.password) { @@ -95,9 +96,9 @@ export default function SignIn() { const response = await api .post(`/user/signIn`, { email: values.email, - hash: values.password + hash: values.password, }) - .then(function(response) { + .then(function (response) { if (!response.data.error) { window.sessionStorage.setItem("token", response.data.token); window.sessionStorage.setItem("userId", response.data.id); @@ -105,7 +106,7 @@ export default function SignIn() { history.push(path); } }) - .catch(function(error) { + .catch(function (error) { if (error.response) { alert( "Falha de autenticação. Certifique-se que email e senha estão corretos." @@ -123,19 +124,19 @@ export default function SignIn() { const theme = createMuiTheme({ overrides: { root: { - color: "white" + color: "white", }, MuiInput: { underline: { "&:before": { - borderBottom: "1px solid #35c7fc" + borderBottom: "1px solid #35c7fc", }, "&:after": { - borderBottom: "1px solid #3f51b5" - } - } - } - } + borderBottom: "1px solid #3f51b5", + }, + }, + }, + }, }); return ( <MuiThemeProvider theme={theme}> @@ -152,8 +153,7 @@ export default function SignIn() { label="E-mail" param="email" onUpdate={handleChange} - verify={checkEmail} - error={values.emailError} + error={!checkEmail()} /> </Grid> <Grid> diff --git a/src/pages/SignUp.js b/src/pages/SignUp.js index 1a894a0880234669f88c75ac08f52ca7835f9777..f4706397fb004cda5f34e7fafff0c221a2ecd342 100644 --- a/src/pages/SignUp.js +++ b/src/pages/SignUp.js @@ -1,4 +1,4 @@ -import React from "react"; +import React, { useEffect } from "react"; import Grid from "@material-ui/core/Grid"; import { createMuiTheme, MuiThemeProvider } from "@material-ui/core"; import IconButton from "@material-ui/core/IconButton"; @@ -9,7 +9,7 @@ import FormInput from "../components/fieldsSignUp/FormInput"; import Paper from "@material-ui/core/Paper"; import api from "../api"; -const useStyles = makeStyles(theme => ({ +const useStyles = makeStyles((theme) => ({ register: { maxWidth: "1000px", background: "#ffffff", @@ -17,21 +17,21 @@ const useStyles = makeStyles(theme => ({ padding: "2% 1%", marginTop: "3%", margin: "0 auto", - width: "95%" + width: "95%", }, custom_strong: { fontSize: "25px", textAlign: "center", display: "block", - color: "#46525d" + color: "#46525d", }, strong_description: { fontSize: "14px", - color: "#c2c6ca" + color: "#c2c6ca", }, form: { alignItems: "center", - textAlign: "center" + textAlign: "center", }, button: { type: "submit", @@ -43,45 +43,34 @@ const useStyles = makeStyles(theme => ({ padding: "10px 20px", fontSize: "18px", "&:hover": { - backgroundColor: "rgb(25, 109, 23)" + backgroundColor: "rgb(25, 109, 23)", }, ["@media (max-width:550px)"]: { - width: "55%" - } - } + width: "55%", + }, + }, })); export default function SignUp() { const history = useHistory(); const classes = useStyles(); + const [values, setValues] = React.useState({ name: "", email: "", password: "", password_confirm: "", nameError: false, - emailError: false + emailError: false, }); - async function update(prop, event) { - await setValues({ ...values, [prop]: event.target.value }); - } + useEffect(() => { + !checkName() ? (values.nameError = true) : (values.nameError = false); + }, [values.name]); + useEffect(() => { + !checkEmail() ? (values.emailError = true) : (values.emailError = false); + }, [values.email]); - const handleChange = prop => event => { - switch (prop) { - case "name": - if (!checkName()) { - values.nameError = true; - } else { - values.nameError = false; - } - break; - case "email": - if (!checkEmail()) { - values.emailError = true; - } else { - values.emailError = false; - } - } - update(prop, event); + const handleChange = (prop) => (event) => { + setValues({ ...values, [prop]: event.target.value }); }; function checkPassword() { if (values.password !== values.password_confirm) { @@ -91,20 +80,19 @@ export default function SignUp() { return true; } function checkName() { - if ( - /^[a-zA-Z ]+$/.test(values.name) && - !/^$|\s+/.test(values.name) && - values.name.length <= 225 - ) { - return true; - } - return false; + return values.name + ? /^[A-Za-záàâãéèêíïóôõöúçñÁÀÂÃÉÈÍÏÓÔÕÖÚÇÑ ]+$/.test(values.name) && + values.name.length <= 225 + ? true + : false + : true; } function checkEmail() { - if (/^[a-zA-Z0-9._-]+@[a-zA-Z0-9]+\.[A-Za-z]+$/.test(values.email)) { - return true; - } - return false; + return values.email + ? /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(values.email) + ? true + : false + : true; } function verifyValues() { if ( @@ -113,7 +101,9 @@ export default function SignUp() { values.password && values.password_confirm ) { - return true; + if (verifyValuesContent()) { + return true; + } else return false; } return false; } @@ -136,15 +126,15 @@ export default function SignUp() { .post(`/user/signUp`, { email: values.email, name: values.name, - hash: values.password + hash: values.password, }) - .then(function(error) { + .then(function (error) { if (!error.response) { let path = `signin`; history.push(path); } }) - .catch(function(error) { + .catch(function (error) { if (error.response) { switch (error.response.data.error) { case 'duplicate key value violates unique constraint "form_user_name_key"': @@ -166,27 +156,25 @@ export default function SignUp() { } function submit() { if (verifyValues()) { - if (verifyValuesContent()) { - handleSubmit(); - } + handleSubmit(); } } const theme = createMuiTheme({ overrides: { root: { - color: "white" + color: "white", }, MuiInput: { underline: { "&:before": { - borderBottom: "1px solid #35c7fc" + borderBottom: "1px solid #35c7fc", }, "&:after": { - borderBottom: "1px solid #3f51b5" - } - } - } - } + borderBottom: "1px solid #3f51b5", + }, + }, + }, + }, }); return ( <MuiThemeProvider theme={theme}> @@ -203,7 +191,7 @@ export default function SignUp() { label="Nome Completo" param="name" onUpdate={handleChange} - error={values.nameError} + error={!checkName()} /> </Grid> <Grid> @@ -211,8 +199,7 @@ export default function SignUp() { label="E-mail" param="email" onUpdate={handleChange} - verify={checkEmail} - error={values.emailError} + error={!checkEmail()} /> </Grid> <Grid> @@ -223,6 +210,13 @@ export default function SignUp() { label="Confirmar Senha" param="password_confirm" onUpdate={handleChange} + error={ + values.password_confirm + ? values.password === values.password_confirm + ? false + : true + : false + } /> </Grid> <Grid>