Your Profile
Locked
Locked

Use the same details you want recruiters to see. If you are a fresher, the years field stays hidden automatically.

const saveProfileBtn = document.getElementById('saveProfileBtn'); const profileStatus = document.getElementById('profileStatus'); let placementSectors = []; let placementProfiles = []; const globalLoader = document.getElementById('globalLoader'); function getAuth() { return sessionStorage.getItem(TOKEN_KEY); } function setStatus(message, type = 'info') { profileStatus.className = `status-message show ${type}`; profileStatus.textContent = message; } function clearStatus() { profileStatus.className = 'status-message'; profileStatus.textContent = ''; } function setBusy(isBusy) { saveProfileBtn.classList.toggle('btn-loading', isBusy); saveProfileBtn.disabled = isBusy; globalLoader.classList.toggle('show', isBusy); globalLoader.setAttribute('aria-hidden', String(!isBusy)); } function syncExperienceFields() { const experienceType = document.getElementById('pf_experienceType').value; const yearsWrap = document.getElementById('pf_yearsWrap'); const yearsInput = document.getElementById('pf_experienceYears'); if (experienceType === 'experienced') { yearsWrap.style.display = 'block'; yearsInput.required = true; } else { yearsWrap.style.display = 'none'; yearsInput.required = false; yearsInput.value = ''; } } function hydrateExperience(profile) { const typeFromProfile = profile.experienceType; const yearsFromProfile = profile.experienceYears; const experienceText = (profile.experience || '').toLowerCase(); let selectedType = typeFromProfile || ''; if (!selectedType && experienceText.includes('fresher')) selectedType = 'fresher'; if (!selectedType && (experienceText.includes('year') || experienceText.includes('exp'))) selectedType = 'experienced'; document.getElementById('pf_experienceType').value = selectedType; document.getElementById('pf_experienceYears').value = yearsFromProfile ?? ''; syncExperienceFields(); } function populatePlacementSelects() { const secSel = document.getElementById('pf_sector_select'); const profSel = document.getElementById('pf_profile_select'); if (secSel) { const seenUnassigned = new Set(); const sectorOptions = placementSectors .filter((sector) => { const name = String(sector.name || '').trim().toLowerCase(); if (name !== 'unassigned') return true; if (seenUnassigned.has(name)) return false; seenUnassigned.add(name); return true; }) .map((sector) => ``) .join(''); secSel.innerHTML = sectorOptions; } if (profSel) { profSel.innerHTML = placementProfiles.map(p => ``).join(''); } // when sector changes, filter profiles secSel?.addEventListener('change', () => { const sid = secSel.value; filterProfilesForSector(sid); }); } function filterProfilesForSector(sectorId) { const profSel = document.getElementById('pf_profile_select'); if (!profSel) return; const opts = placementProfiles.filter(p => String(p.sectorId || '') === String(sectorId)).map(p => ``).join(''); profSel.innerHTML = opts || ''; } function toggleAssignmentFields(showSectorSelect, showProfileSelect, profileObj) { const sectorInput = document.getElementById('pf_sector'); const profileInput = document.getElementById('pf_placementProfile'); const sectorSel = document.getElementById('pf_sector_select'); const profileSel = document.getElementById('pf_profile_select'); if (showSectorSelect) { sectorInput.style.display = 'none'; sectorInput.disabled = true; sectorSel.style.display = 'block'; // preselect first or leave empty if (profileObj && profileObj.sector && profileObj.sector.id) sectorSel.value = profileObj.sector.id; } else { sectorInput.style.display = 'block'; sectorInput.disabled = Boolean(profileObj && profileObj.sector && profileObj.sector.id); sectorSel.style.display = 'none'; } if (showProfileSelect) { profileInput.style.display = 'none'; profileInput.disabled = true; profileSel.style.display = 'block'; // if sector already selected, filter const sid = (profileObj && profileObj.sector && profileObj.sector.id) || (document.getElementById('pf_sector_select')?.value); if (sid) filterProfilesForSector(sid); } else { profileInput.style.display = 'block'; profileInput.disabled = Boolean(profileObj && profileObj.assignedProfile && profileObj.assignedProfile.id); profileSel.style.display = 'none'; } } document.getElementById('pf_experienceType').addEventListener('change', syncExperienceFields); async function loadProfile() { const token = getAuth(); if (!token) { window.location.href = './login.html'; return; } setBusy(true); clearStatus(); try { const res = await fetch(`${API_BASE}/api/auth/profile`, { headers: { 'Authorization': 'Bearer ' + token } }); if (!res.ok) { if (res.status === 401) { sessionStorage.removeItem(TOKEN_KEY); window.location.href = './login.html'; } setStatus('Unable to load your profile right now. Please try again.', 'error'); return; } const d = await res.json(); const p = d.profile || {}; document.getElementById('pf_name').value = p.name || ''; document.getElementById('pf_email').value = p.email || ''; document.getElementById('pf_phone').value = p.phone || ''; document.getElementById('pf_college').value = p.college || ''; // If searchable dropdown exists on profile, use its setter, otherwise fall back if (typeof setProfileCourseValue === 'function') { setProfileCourseValue(p.course || ''); } else { const pfCourse = document.getElementById('pf_course'); if (pfCourse) pfCourse.value = p.course || ''; } document.getElementById('pf_sector').value = p.sector?.name || 'Pending assignment'; document.getElementById('pf_placementProfile').value = p.assignedProfile?.name || 'Pending assignment'; // load sectors & profiles for possible assignment try { const [sRes, prRes] = await Promise.all([fetch(`${API_BASE}/api/sectors`), fetch(`${API_BASE}/api/profiles`)]); const sJson = await sRes.json(); const pJson = await prRes.json(); placementSectors = sJson.sectors || []; placementProfiles = pJson.profiles || []; populatePlacementSelects(); } catch (err) { // ignore placement catalog load errors silently } hydrateExperience(p); // Handle resume display const resumeDisplay = document.getElementById('pf_resumeDisplay'); if (p.resumePath) { const filename = p.resumePath.split('/').pop(); resumeDisplay.innerHTML = `
${escapeHtml(filename)}
`; } else { resumeDisplay.innerHTML = '
No resume uploaded yet
'; } localStorage.setItem(PROFILE_KEY, JSON.stringify(p)); // if user missing assignment, show selects to allow choosing const needsSector = !p.sector || !p.sector.id; const needsProfile = !p.assignedProfile || !p.assignedProfile.id; toggleAssignmentFields(needsSector, needsProfile, p); if (!needsSector) document.getElementById('pf_sector').disabled = true; if (!needsProfile) document.getElementById('pf_placementProfile').disabled = true; } catch (e) { setStatus('Unable to load your profile right now. Please try again.', 'error'); } finally { setBusy(false); } } document.getElementById('backBtn').addEventListener('click', () => { window.location.href = './candidate-dashboard.html'; }); profileForm.addEventListener('submit', async (e) => { e.preventDefault(); const token = getAuth(); if (!token) return window.location.href = './login.html'; clearStatus(); setBusy(true); const body = { name: document.getElementById('pf_name').value.trim(), phone: document.getElementById('pf_phone').value.trim(), college: document.getElementById('pf_college').value.trim(), course: (typeof getProfileCourseValue === 'function') ? String(getProfileCourseValue() || '').trim() : document.getElementById('pf_course').value.trim(), experienceType: document.getElementById('pf_experienceType').value, experienceYears: document.getElementById('pf_experienceYears').value }; // include sector/profile if selects are visible const sectorSel = document.getElementById('pf_sector_select'); const profileSel = document.getElementById('pf_profile_select'); if (sectorSel && sectorSel.style.display !== 'none') body.sectorId = sectorSel.value || null; if (profileSel && profileSel.style.display !== 'none') body.profileId = profileSel.value || null; try { const res = await fetch(`${API_BASE}/api/auth/profile`, { method: 'PUT', headers: { 'Content-Type': 'application/json', 'Authorization': 'Bearer ' + token }, body: JSON.stringify(body) }); const data = await res.json(); if (!res.ok) throw new Error(data.message || 'Failed to update'); localStorage.setItem(PROFILE_KEY, JSON.stringify(data.profile)); setStatus('Profile updated successfully.', 'success'); setTimeout(() => { window.location.href = './candidate-dashboard.html'; }, 900); } catch (err) { setStatus(err.message || 'Error updating profile.', 'error'); } finally { setBusy(false); } }); (function init(){ // initialize the reusable profile dropdown and then load profile try { initProfilePageDropdown(); } catch (e) { /* ignore if script not present */ } loadProfile(); })();