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