feat: devWeb - active notification on profile page

This commit is contained in:
kmitresse
2024-04-27 14:04:39 +02:00
parent b52f2509c9
commit 65a4f28327
4 changed files with 51 additions and 42 deletions
@@ -15,7 +15,6 @@ public class ProfileBean {
private String email;
private String oldPassword;
private String password;
private String oldGender;
private String gender;
private User user;
private HttpResponse error;
@@ -34,29 +33,41 @@ public class ProfileBean {
public boolean validate() {
EntityManager entityManager = EntityManagerProvider.getInstance();
entityManager.getTransaction().begin();
try{
DAO<User> userDAO = new Game_JPA_DAO_Factory().getDAOUser();
DAO<User> userDAO;
try {
userDAO= new Game_JPA_DAO_Factory().getDAOUser();
//Check if the user is valid
user = userDAO.findById(Integer.parseInt(id));
if (user == null) {
error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Utilisateur non trouvé");
entityManager.getTransaction().rollback();
return false;
}
//Check if the email is not already taken
User[] users = userDAO.findByField("email", email);
if (!oldEmail.equals(email) && users.length > 0) {
error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Cet email est déjà utilisé");
entityManager.getTransaction().rollback();
return false;
}
//Check if the old password is correct
if (!oldPassword.equals("") && user.verifyPassword(oldPassword) == false) {
//Check if the oldPassword is correct
if(!oldPassword.equals("") && !user.verifyPassword(oldPassword)) {
error = new HttpResponse(HttpResponseCode.UNAUTHORIZED, "Ancien mot de passe incorrect");
entityManager.getTransaction().rollback();
return false;
}
} catch (DAOException e) {
error = new HttpResponse(HttpResponseCode.INTERNAL_SERVER_ERROR, "Une erreur est survenue (DB_CONNECTION_ERROR:002)");
entityManager.getTransaction().rollback();
return false;
}
//Update the user
user.setEmail(email);
if (!password.equals("")) {
user.setPassword(password);
}
user.setGender(User.Gender.valueOf(gender));
try {
userDAO.update(user);
entityManager.getTransaction().commit();
return true;
@@ -44,6 +44,7 @@ public class ProfileServlet extends HttpServlet {
.setId(request.getParameter("id"))
.setOldEmail(request.getParameter("oldEmail"))
.setEmail(request.getParameter("email"))
.setOldPassword(request.getParameter("oldPassword"))
.setPassword(request.getParameter("password"))
.setGender(request.getParameter("gender"))
;
@@ -11,6 +11,10 @@
<%@taglib prefix="form" tagdir="/WEB-INF/tags/forms" %>
<layout:base title="Profil">
<jsp:attribute name="head">
<link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/form.css"/>
</jsp:attribute>
<jsp:body>
<component:hero>
<div class="columns is-centered">
<div class="column is-5-tablet is-5-desktop is-5-widescreen">
@@ -29,4 +33,5 @@
</div>
</div>
</component:hero>
</jsp:body>
</layout:base>
@@ -105,19 +105,10 @@
const password = profileForm.querySelector("input[name='password']");
const repassword = profileForm.querySelector("input[name='repeat-password']");
// Check if the password and the confirmation password are the same
if (oldPassword.value !== "") {
if("${User.hashPassword(oldPassword.value)}" !== "${user.password}"){
onError(new Error("L'ancien mot de passe ne corresponds pas"));
return;
}
if(password.value !== repassword.value) {
if (oldPassword.value !== "" && password.value !== repassword.value) {
onError(new Error("Les mots de passe ne correspondent pas"));
return;
}
}
else {
password.value = "${user.password}";
};
const {action, method} = profileForm;
@@ -144,7 +135,7 @@
* @param error {Error} - Error of the form submission
*/
function onError(error) {
console.log("Error:", error)
console.log(error)
// Input fields in red
inputs.forEach(input => input.classList.add("is-danger"));
@@ -169,7 +160,7 @@
notification.appendChild(notificationTitle);
notification.appendChild(notificationMessage);
document.body.appendChild(notification);
console.log("je suis bien dans la fonction mais la notification ne s'affiche pas")
setTimeout(() => notification.remove(), 5010);
}
@@ -177,7 +168,7 @@
* Handle the success of the form submission
*/
function onSuccess() {
console.log("Succes:", "Modifications effectuées avec succès")
console.log("Succès:", "Modifications effectuées avec succès")
// Notification
const notification = document.createElement("div");
@@ -189,7 +180,7 @@
const notificationIcon = document.createElement("span");
notificationIcon.classList.add("icon");
notificationIcon.innerHTML = "<i class='fas fa-exclamation-triangle'></i>";
notificationIcon.innerHTML = "<i class='fa-solid fa-check'></i>";
const notificationMessage = document.createElement("p");
notificationMessage.classList.add("subtitle", "is-6");
@@ -199,6 +190,7 @@
notification.appendChild(notificationTitle);
notification.appendChild(notificationMessage);
document.body.appendChild(notification);
console.log("je suis bien dans la fonction mais la notification ne s'affiche pas")
setTimeout(() => notification.remove(), 5010);
}