Skip to content
Snippets Groups Projects
Commit aa96666e authored by Yulong Wang's avatar Yulong Wang
Browse files

Merge branch...

Merge branch '36-as-a-commonadmin-i-hope-i-can-find-a-my-information-on-page-so-that-i-can-know-my-role-and' into 'main'

Updated Login Displays the user id

Closes #36

See merge request !62
parents 18351e1e af383d27
Branches
No related tags found
1 merge request!62Updated Login Displays the user id
...@@ -5,6 +5,7 @@ import com.cardiff.client_project.service.CommonAdminHospitalService; ...@@ -5,6 +5,7 @@ import com.cardiff.client_project.service.CommonAdminHospitalService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
...@@ -37,6 +38,18 @@ public class CommonAdminHospitalController { ...@@ -37,6 +38,18 @@ public class CommonAdminHospitalController {
} }
} }
@GetMapping("/currentUser")
public ResponseEntity<String> getCurrentUser() {
try {
String username = SecurityContextHolder.getContext().getAuthentication().getName();
return ResponseEntity.ok(username);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body("Unable to fetch current user.");
}
}
@PutMapping("/update") @PutMapping("/update")
......
...@@ -15,7 +15,7 @@ body { ...@@ -15,7 +15,7 @@ body {
padding: 1rem; padding: 1rem;
display: flex; display: flex;
flex-direction: column; flex-direction: column;
justify-content: space-between; gap: 0.5rem; /* 为按钮之间增加间距 */
} }
.sidebar .text-lg { .sidebar .text-lg {
font-size: 1.25rem; font-size: 1.25rem;
......
...@@ -11,9 +11,12 @@ ...@@ -11,9 +11,12 @@
<div class="text-lg font-semibold">System</div> <div class="text-lg font-semibold">System</div>
<a href="#" data-content="hospital">Hospital</a> <a href="#" data-content="hospital">Hospital</a>
<a href="#" data-content="patient">Nursing staff</a> <a href="#" data-content="patient">Nursing staff</a>
<a href="#" data-content="logout">LogOut</a> <a><div onclick="chooseOut()" >Sign Out</div></a>
</nav> </nav>
<div class="content"> <div class="content">
<div id="user-info" style="text-align: right; padding: 10px; font-weight: bold;"></div>
<div id="dynamic-content"> <div id="dynamic-content">
<h1>Welcome to the Management System</h1> <h1>Welcome to the Management System</h1>
<p>Select an option from the sidebar to get started.</p> <p>Select an option from the sidebar to get started.</p>
...@@ -42,9 +45,36 @@ ...@@ -42,9 +45,36 @@
</div> </div>
<script> <script>
const dynamicContent = document.getElementById("dynamic-content"); const dynamicContent = document.getElementById("dynamic-content");
// Load the current login user information
function loadCurrentUser() {
fetch('/commonAdmin/hospital/currentUser')
.then(response => {
if (!response.ok) {
throw new Error("Failed to fetch current user.");
}
return response.text();
})
.then(username => {
if (!username) {
throw new Error("No username received from server.");
}
document.getElementById('user-info').innerText = `Logged in as: ${username}`;
})
.catch(error => {
console.error("Error fetching current user:", error);
document.getElementById('user-info').innerText = "Unable to fetch user info.";
});
}
function chooseOut(){
window.location.href="/logout"
}
// Initialize load user information
loadCurrentUser();
function loadHospitalInterface() { function loadHospitalInterface() {
dynamicContent.innerHTML = ` dynamicContent.innerHTML = `
<div id="toolbar"> <div id="toolbar">
...@@ -89,7 +119,6 @@ ...@@ -89,7 +119,6 @@
loadHospitals(); loadHospitals();
} }
function openAddHospitalModal() { function openAddHospitalModal() {
document.getElementById('modal-title').innerText = "Add New Hospital"; document.getElementById('modal-title').innerText = "Add New Hospital";
document.getElementById('hospital-modal').style.display = 'flex'; document.getElementById('hospital-modal').style.display = 'flex';
...@@ -121,8 +150,6 @@ ...@@ -121,8 +150,6 @@
availableBeds: parseInt(availableBeds, 10), availableBeds: parseInt(availableBeds, 10),
}; };
console.log("Sending data to backend:", newHospital); // Print the sent data
fetch('/commonAdmin/hospital/add', { fetch('/commonAdmin/hospital/add', {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'Content-Type': 'application/json' },
...@@ -138,14 +165,11 @@ ...@@ -138,14 +165,11 @@
} }
}) })
.catch((error) => { .catch((error) => {
console.error(error); // Print error message console.error(error);
alert("An error occurred while adding the hospital."); alert("An error occurred while adding the hospital.");
}); });
} }
// Load hospital list
function loadHospitals(filters = {}) { function loadHospitals(filters = {}) {
const query = new URLSearchParams(filters).toString(); const query = new URLSearchParams(filters).toString();
fetch(`/commonAdmin/hospital/all?${query}`) fetch(`/commonAdmin/hospital/all?${query}`)
...@@ -181,69 +205,7 @@ ...@@ -181,69 +205,7 @@
.catch((error) => console.error(error)); .catch((error) => console.error(error));
} }
// Processing editing hospital // Initializes the loading of the hospital management interface
function handleEditHospital(event) {
const id = event.target.dataset.id;
fetch(`/commonAdmin/hospital/all`)
.then((response) => response.json())
.then((data) => {
const hospital = data.find((h) => h.id === parseInt(id, 10));
if (hospital) {
document.getElementById('modal-title').innerText = "Edit Hospital";
document.getElementById('modal-name').value = hospital.name;
document.getElementById('modal-location').value = hospital.location;
document.getElementById('modal-phone').value = hospital.phone;
document.getElementById('modal-totalBeds').value = hospital.totalBeds;
document.getElementById('modal-availableBeds').value = hospital.availableBeds;
document.getElementById('hospital-modal').style.display = 'flex';
document.getElementById('save-hospital-btn').onclick = () => saveEditedHospital(id);
}
})
.catch((error) => console.error('Error loading hospital data:', error));
}
// Save the edited hospital information
function saveEditedHospital(id) {
const updatedHospital = {
id: parseInt(id, 10),
name: document.getElementById('modal-name').value,
location: document.getElementById('modal-location').value,
phone: document.getElementById('modal-phone').value,
totalBeds: parseInt(document.getElementById('modal-totalBeds').value, 10),
availableBeds: parseInt(document.getElementById('modal-availableBeds').value, 10),
};
fetch('/commonAdmin/hospital/update', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(updatedHospital),
})
.then(() => {
alert('Hospital updated successfully!');
document.getElementById('hospital-modal').style.display = 'none';
loadHospitals();
})
.catch((error) => console.error('Error updating hospital:', error));
}
// Delete hospital
function handleDeleteHospital(event) {
const id = event.target.dataset.id;
fetch(`/commonAdmin/hospital/delete/${id}`, { method: 'DELETE' })
.then(() => loadHospitals())
.catch((error) => console.error(error));
}
// Close the mode box
document.getElementById('close-hospital-modal').addEventListener('click', () => {
document.getElementById('hospital-modal').style.display = 'none';
});
// Initializes sidebar navigation events
document.querySelectorAll(".sidebar a").forEach(link => { document.querySelectorAll(".sidebar a").forEach(link => {
link.addEventListener("click", function (event) { link.addEventListener("click", function (event) {
event.preventDefault(); event.preventDefault();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment