|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function getUserData(userId) {
|
|
|
const response = await fetch(`/api/users/${userId}`);
|
|
|
return await response.json();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function updateUserProfile(userId, data) {
|
|
|
|
|
|
const currentData = await getUserData(userId);
|
|
|
|
|
|
|
|
|
const updatedData = { ...currentData, ...data };
|
|
|
|
|
|
|
|
|
await fetch(`/api/users/${userId}`, {
|
|
|
method: 'PUT',
|
|
|
headers: { 'Content-Type': 'application/json' },
|
|
|
body: JSON.stringify(updatedData)
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function displayUserInfo(userId) {
|
|
|
try {
|
|
|
const userData = await getUserData(userId);
|
|
|
document.getElementById('userName').textContent = userData.name;
|
|
|
document.getElementById('userEmail').textContent = userData.email;
|
|
|
} catch (error) {
|
|
|
console.error('Failed to display user info:', error);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async function deleteUserAccount(userId) {
|
|
|
|
|
|
const userData = await getUserData(userId);
|
|
|
console.log(`Deleting account for ${userData.name}`);
|
|
|
|
|
|
await fetch(`/api/users/${userId}`, {
|
|
|
method: 'DELETE'
|
|
|
});
|
|
|
}
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', () => {
|
|
|
const userId = sessionStorage.getItem('currentUserId');
|
|
|
if (userId) {
|
|
|
displayUserInfo(userId);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
|
|
|
export { getUserData, updateUserProfile, displayUserInfo, deleteUserAccount };
|
|
|
|
|
|
|