diff --git a/README.md b/README.md index 606797dc..35cdc740 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Sentinela 2024.1 - FRONTEND +# Sentinela 2024.2 - FRONTEND ## Comandos para rodar o projeto diff --git a/src/Pages/Protected/Users/ViewMembershipPage/index.jsx b/src/Pages/Protected/Users/ViewMembershipPage/index.jsx index b6a7f0ee..06d48a4c 100644 --- a/src/Pages/Protected/Users/ViewMembershipPage/index.jsx +++ b/src/Pages/Protected/Users/ViewMembershipPage/index.jsx @@ -25,6 +25,7 @@ import { // import { mascaraCPF, mascaraRg, mascaraTelefone, mascaraCEP, validateField } from "../../../../Utils/mask"; import AddCircleOutlineIcon from "@mui/icons-material/AddCircleOutline"; import SecondaryButton from "../../../../Components/SecondaryButton"; +import { validarCPF } from "../../../../Utils/validators"; const ViewMembershipPage = () => { const { state } = useLocation(); @@ -215,16 +216,22 @@ const ViewMembershipPage = () => { throw new Error("missing fields"); } - const message = await updateMembership(membershipId, formData); - if (message) { - throw new Error(message); + const response = await updateMembership(membershipId, formData); + console.log("resposta: ", response); + if(response.status === 200){ + alert("Usuário atualizado com sucesso!"); + } else { + alert(response.data.mensagem); } setOpenSuccessDialog(false); - alert("usuário atualizado com sucesso!"); } catch (error) { setOpenSuccessDialog(false); alert("Erro ao atualizar usuário"); + console.error( + "Erro ao atualizar usuário", + error + ); } }; @@ -261,6 +268,7 @@ const ViewMembershipPage = () => { if (!sex) missingFields.push("Sexo"); if (!rg) missingFields.push("RG"); if (!cpf) missingFields.push("CPF"); + if (!validarCPF(cpf)) missingFields.push("CPF"); if (!email) missingFields.push("Email"); if (!phone) missingFields.push("Celular"); if (!ufAddress) missingFields.push("UF do Endereço"); diff --git a/src/Pages/Protected/Users/userUpdate/index.jsx b/src/Pages/Protected/Users/userUpdate/index.jsx index f4a87272..41e2b3e0 100644 --- a/src/Pages/Protected/Users/userUpdate/index.jsx +++ b/src/Pages/Protected/Users/userUpdate/index.jsx @@ -1,5 +1,6 @@ import { useState, useEffect } from "react"; import { useLocation, useNavigate } from "react-router-dom"; +import { Button } from "@mui/material"; import FieldSelect from "../../../../Components/FieldSelect"; import FieldText from "../../../../Components/FieldText"; import Modal from "../../../../Components/Modal"; @@ -16,6 +17,7 @@ import { isValidCelular, isValidEmail, mascaraTelefone, + validaSenha, } from "../../../../Utils/validators"; export default function UserUpdatePage() { @@ -39,7 +41,8 @@ export default function UserUpdatePage() { const [isEmailValid, setIsEmailValid] = useState(true); const [isCelularValid, setIsCelularValid] = useState(true); const [isUserVisible, setIsUserVisible] = useState(true); - const [isNewPasswordValid] = useState(true); + const [isNewPasswordValid, setNewPasswordValid] = useState(true); + const [passwordMessage, setPasswordMessage] = useState(" "); const passwordsMatch = newPassword === confirmPassword; @@ -74,8 +77,6 @@ export default function UserUpdatePage() { fetchUser(); }, [userId]); - console.log(userId); - const handleSave = async () => { const trimmedCelular = celular.replace(/\D/g, ""); const { isValid: isValidNumber, message: celularMessage } = @@ -110,20 +111,27 @@ export default function UserUpdatePage() { console.error(`Erro ao atualizar usuário com ID ${userId}:`, error); } }; - + const handleSavePassword = async () => { + if(!passwordsMatch || !isNewPasswordValid){ + return; + } + const updatedUserPassword = { old_password: oldPassword, new_password: newPassword, }; + try { await changePasswordInProfile(updatedUserPassword).then((data) => { - console.log("caraleo", data); - if (data && data.response.status != 200) { + if (data && data.status != 200) { alert(data.response.data.mensagem); - } + } + else if(data && data.status == 200) { + handleSavePasswordModal(); + } }); - handleSavePasswordModal(); + } catch (error) { console.error( `Erro ao atualizar senha do usuário com ID ${userId}:`, @@ -135,6 +143,7 @@ export default function UserUpdatePage() { const handleChangeLogin = (event) => setLogin(event.target.value); const handleSaveModal = () => setShowSaveModal(true); + const handleSavePasswordModal = () => setShowPasswordSaveModal(true); const handleSaveCloseDialog = () => { @@ -146,6 +155,19 @@ export default function UserUpdatePage() { const showUserDiv = () => setIsUserVisible(true); const showPasswordDiv = () => setIsUserVisible(false); + const handleNavigateToContributions = () => { + navigate(`/movimentacoes/contribuicoes/${nomeCompleto}`, { + state: { + userId, + nomeCompleto, + celular, + email, + login, + perfilSelecionado, + }, + }); + }; + return (
@@ -193,6 +215,13 @@ export default function UserUpdatePage() { )} + + @@ -219,20 +248,26 @@ export default function UserUpdatePage() { label="Nova senha" value={newPassword} type={showPasswords ? "text" : "password"} - onChange={(e) => setNewPassword(e.target.value)} + onChange={(e) => { setNewPassword(e.target.value); + const isValidPassword = validaSenha(e.target.value); + setNewPasswordValid(isValidPassword.status); + setPasswordMessage(isValidPassword.message); + } + } /> - {!isNewPasswordValid && ( - - )} setConfirmNewPassword(e.target.value)} /> - +
- {!passwordsMatch && confirmPassword && ( + {!isNewPasswordValid && ( + {passwordMessage} + )} + + {isNewPasswordValid && !passwordsMatch && confirmPassword && ( As senhas não coincidem )}
diff --git a/src/Pages/Public/MemberShip/index.jsx b/src/Pages/Public/MemberShip/index.jsx index 35597b75..57906842 100644 --- a/src/Pages/Public/MemberShip/index.jsx +++ b/src/Pages/Public/MemberShip/index.jsx @@ -14,9 +14,12 @@ import SecondaryButton from "../../../Components/SecondaryButton"; import Modal from "../../../Components/Modal"; import { useNavigate } from "react-router-dom"; import { listOrgans } from "../../../Services/organService"; -import { isValidEmail } from "../../../Utils/validators"; -import { mascaraTelefone } from "../../../Utils/validators"; -import { mascaraCEP } from "../../../Utils/validators"; +import { + isValidEmail, + mascaraTelefone, + mascaraCEP, + validarCPF + } from "../../../Utils/validators"; const MemberShip = () => { const [email, setEmail] = useState(""); @@ -336,7 +339,8 @@ const MemberShip = () => { //VALIDAÇÕES if (isValidEmail(email) === false) erros.email = 1; - if (cpf.length < 14) erros.cpf = 1; + //if (cpf.length < 14) erros.cpf = 1; + if (!validarCPF(cpf)) erros.cpf = 1; if (rg.length < 7) erros.rg = 1; //if (cep.length < 9) erros.cep = 1; //if (telefone.length < 14) erros.telefone = 1; diff --git a/src/Services/memberShipService.js b/src/Services/memberShipService.js index 9fcad826..ef9f4527 100644 --- a/src/Services/memberShipService.js +++ b/src/Services/memberShipService.js @@ -51,13 +51,12 @@ export const updateMemberStatus = async (memberId, formData) => { export const updateMembership = async (memberId, formData) => { try { - await APIUsers.patch(`membership/update/${memberId}`, { + const response = await APIUsers.patch(`/membership/update/${memberId}`, { formData, }); - - return false; + return response; } catch (error) { - return error.response.data.erro; + return error.response; } }; diff --git a/src/Services/userService.js b/src/Services/userService.js index 3cd0c07c..42e92a39 100644 --- a/src/Services/userService.js +++ b/src/Services/userService.js @@ -226,7 +226,7 @@ export const changePasswordById = async (newPassword, id) => { // ); export const changePasswordInProfile = async (passwords) => { try { - await APIUsers.patch( + const response = await APIUsers.patch( `/users/renew-password`, { ...passwords }, { @@ -235,6 +235,7 @@ export const changePasswordInProfile = async (passwords) => { }, } ); + return response; } catch (error) { return error; } diff --git a/src/Utils/validators.js b/src/Utils/validators.js index 437dd7ba..d3b1a2b6 100644 --- a/src/Utils/validators.js +++ b/src/Utils/validators.js @@ -81,6 +81,48 @@ export const mascaraCEP = (cep) => { return formattedCEP.replace(/(\d{5})(\d)/, "$1-$2"); }; +export const validarCPF = (cpf) => { + cpf = cpf.replace(/[^\d]+/g, ""); + + if (cpf.length !== 11) { + console.error("Erro: CPF deve conter 11 d�gitos"); + return false; + } + + if (/^(\d)\1+$/.test(cpf)) { + console.error("Erro: CPF n�o pode conter todos d�gitos iguais"); + return false; + } + + let soma = 0; + for (let i = 0; i < 9; i++) { + soma += parseInt(cpf.charAt(i)) * (10 - i); + } + let resto = (soma * 10) % 11; + if (resto === 10 || resto === 11) { + resto = 0; + } + if (resto !== parseInt(cpf.charAt(9))) { + console.error("Erro: Primeiro d�gito verificador do CPF � inv�lido"); + return false; + } + + soma = 0; + for (let i = 0; i < 10; i++) { + soma += parseInt(cpf.charAt(i)) * (11 - i); + } + resto = (soma * 10) % 11; + if (resto === 10 || resto === 11) { + resto = 0; + } + if (resto !== parseInt(cpf.charAt(10))) { + console.error("Erro: Segundo d�gito verificador do CPF � inv�lido"); + return false; + } + + return true; +}; + export const handleCpfCnpjInput = (value) => { const numericValue = value.replace(/\D/g, ""); if (numericValue.length <= 11) { @@ -122,3 +164,43 @@ export const mascaraCelular = (celular) => { .replace(/^(\d{2})(\d)/g, "($1) $2") .replace(/(\d{5})(\d{4})$/, "$1-$2"); }; + +export const validaSenha = (novaSenha) => { + const comprimentoMinimo = 8; + const temMaiuscula = /[A-Z]/.test(novaSenha); + const temMinuscula = /[a-z]/.test(novaSenha); + const temNumero = /\d/.test(novaSenha); + + if (novaSenha.length < comprimentoMinimo) { + return { + status: false, + message: `A senha deve ter pelo menos ${comprimentoMinimo} caracteres`, + }; + } + + if (!temMaiuscula) { + return { + status: false, + message: "A senha deve ter pelo menos uma letra maiuscula", + }; + } + + if (!temMinuscula) { + return{ + status: false, + message: "A senha deve ter pelo menos uma letra minuscula", + }; + } + + if (!temNumero) { + return{ + status: false, + message: "A senha deve ter pelo menos um numero", + }; + } + + return{ + status: true, + message: "Senha válida", + }; +}; \ No newline at end of file