master-brain-api / static /js /profile.js
Dilip8756's picture
Upload 100 files
58c1398 verified
/**
* Profile Page Logic - Aadhaar Pro
*/
document.addEventListener('DOMContentLoaded', () => {
initProfilePage();
});
function initProfilePage() {
loadProfile();
setupProfileForm();
}
/**
* Fetch user info and populate both card and form
*/
async function loadProfile() {
try {
const res = await fetch('/api/user/info');
const user = await res.json();
// Update Summary Card
const summaryName = document.getElementById('left-user-name');
const summaryRole = document.getElementById('left-user-role');
const summaryEmail = document.getElementById('left-user-email');
const summaryPhone = document.getElementById('left-user-phone');
const summaryAddress = document.getElementById('left-user-address');
if (summaryName) summaryName.innerText = user.name || 'User';
if (summaryRole) summaryRole.innerText = (user.role || 'retailer').toUpperCase();
if (summaryEmail) summaryEmail.innerText = user.email || 'Email not set';
if (summaryPhone) summaryPhone.innerText = user.phone || 'N/A';
if (summaryAddress) summaryAddress.innerText = user.address || 'Address not set';
// Update Edit Form
const inputName = document.getElementById('prof-name');
const inputEmail = document.getElementById('prof-email');
const inputPhone = document.getElementById('prof-phone');
const inputRole = document.getElementById('prof-role');
const inputAddress = document.getElementById('prof-address');
if (inputName) inputName.value = user.name || '';
if (inputEmail) inputEmail.value = user.email || '';
if (inputPhone) inputPhone.value = user.phone || '';
if (inputRole) inputRole.value = (user.role || '').toUpperCase();
if (inputAddress) inputAddress.value = user.address || '';
} catch (err) {
console.error('Error loading profile:', err);
}
}
/**
* Handle form submission
*/
function setupProfileForm() {
const form = document.getElementById('profile-form');
if (!form) return;
form.addEventListener('submit', async (e) => {
e.preventDefault();
const data = {
name: document.getElementById('prof-name').value,
email: document.getElementById('prof-email').value,
address: document.getElementById('prof-address').value
};
try {
const res = await fetch('/api/profile/update', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
});
const resData = await res.json();
if (resData.success) {
alert('Profile updated successfully!');
loadProfile(); // Refresh UI
} else {
alert('Failed to update profile: ' + (resData.error || 'Unknown error'));
}
} catch (err) {
console.error('Update fail:', err);
alert('An error occurred during profile update.');
}
});
}