diff --git a/index.php b/index.php index d189965..314c3b3 100644 --- a/index.php +++ b/index.php @@ -15,7 +15,7 @@
Doct'ISEN
diff --git a/src/js/AJAX/index.js b/src/js/AJAX/index.js index 38aaa3c..1356416 100644 --- a/src/js/AJAX/index.js +++ b/src/js/AJAX/index.js @@ -4,6 +4,12 @@ console.log("index.js loaded"); //Felix Part +document.addEventListener("DOMContentLoaded", function() { + if(sessionStorage.getItem("token") !== null) { + displayHomeTopBar(); + } +}); + function displaySearchResults(data) { let container = document.getElementById("content"); container.innerHTML = ""; @@ -65,7 +71,7 @@ function removeSearchTopBar(){ topbar.innerHTML = ""; topbar.innerHTML = `Doct'ISEN
@@ -210,7 +216,7 @@ function displayHomeTopBar() { if(sessionStorage.getItem("token") === null) { topbar.innerHTML = `Doct'ISEN
@@ -230,7 +236,7 @@ function displayHomeTopBar() { if(user.type === "patient") { topbar.innerHTML = ` `; + attachDisconnectEventListener(); } else if (user.type === "medecin") { topbar.innerHTML = ` `; + attachDisconnectEventListener(); } else { topbar.innerHTML = ` @@ -270,7 +278,7 @@ function displayHomeTopBar() { } } -function displayHome() { +function displayHome(text) { removeSearchTopBar(); displayHomeTopBar(); let container = document.getElementById("content"); @@ -320,7 +328,9 @@ function displayHome() { `; attachSearchEventListener(); attachLoginUserDisplayEventListener(); - displayAlert("Rendez-vous pris avec succès"); + if(text !== ""){ + displayAlert(text); + } } function performSearch(event) { @@ -390,7 +400,7 @@ function displayLoginUser() { ajaxRequest('POST', "src/API/requests.php/api/login/patient", function (data) { if (data.message === "Login success") { sessionStorage.setItem("token", data.token); - displayHome(); + displayHome("Vous êtes connecté"); } else { alert("Erreur lors de la connexion"); } @@ -441,7 +451,7 @@ function displayLoginPraticien() { ajaxRequest('POST', "src/API/requests.php/api/login/medecin", function (data) { if (data.message === "Login success") { sessionStorage.setItem("token", data.token) - displayHome(); + displayHome("Vous êtes connecté"); } else { alert("Erreur lors de la connexion"); } @@ -630,6 +640,11 @@ function displaySignUpUser() { }); } +function disconnect() { + sessionStorage.removeItem("token"); + displayHome("Vous êtes déconnecté"); +} + function attachSearchEventListener() { try{ document.getElementById("recherche").addEventListener("click", function (event) { @@ -660,15 +675,20 @@ function attachPrendreRDVEventListener() { let buttons = document.querySelectorAll("button[id^='rdv-id-']"); buttons.forEach(function (button) { button.addEventListener("click", function (event) { - let RDVid = event.target.id.split("-")[2]; - let data = "rdv_id=" + RDVid + "&patient_id=" + 1; - ajaxRequest('PUT', "src/API/requests.php/api/rdv", function (returnData){ - if (returnData.success) { - displayHome(); - } else { - alert("Erreur lors de la prise de rendez-vous"); - } - }, data); + if(sessionStorage.getItem("token") === null) { + displayHome("Vous devez être connecté pour prendre un rendez-vous"); + } + else{ + let RDVid = event.target.id.split("-")[2]; + let data = "rdv_id=" + RDVid + "&patient_id=" + 1; + ajaxRequest('PUT', "src/API/requests.php/api/rdv", function (returnData){ + if (returnData.success) { + displayHome("Rendez-vous pris avec succès"); + } else { + alert("Erreur lors de la prise de rendez-vous"); + } + }, data); + } }); }); } @@ -697,6 +717,28 @@ function attachLoginPraticienDisplayEventListener() { } } +function attachReturnHomeEventListener() { + try { + document.getElementById("home").addEventListener("click", function (event) { + displayHome(""); + }); + } + catch (e) { + //Do nothing + //console.error(e); + } +} + +function attachDisconnectEventListener() { + try { + document.getElementById("user-name-disconect").addEventListener("click", function (event) { + disconnect(); + }); + } catch (e) { + //Do nothing + } +} + //Yanis Part @@ -761,4 +803,6 @@ function ButtonShowRdvPraticient() { ButtonShowRdvPraticient(); attachSearchEventListener(); attachLoginUserDisplayEventListener(); -attachLoginPraticienDisplayEventListener(); \ No newline at end of file +attachLoginPraticienDisplayEventListener(); +attachReturnHomeEventListener(); +attachDisconnectEventListener(); \ No newline at end of file