Language Exchange Partner Finder

Language Exchange Partner Finder

Find people from around the world to practice languages with.

Loading your profile...

Could not initialize the application. ${e.message}

`; return; } // --- AUTHENTICATION & PROFILE HANDLING --- // Use custom token if available, otherwise sign in anonymously try { if (typeof __initial_auth_token !== 'undefined' && __initial_auth_token) { await signInWithCustomToken(auth, __initial_auth_token); } else { await signInAnonymously(auth); } } catch (error) { console.error("Authentication failed:", error); loadingView.innerHTML = `

Authentication failed. Please try again later.

`; return; } onAuthStateChanged(auth, async (user) => { if (user) { currentUser = user; const userProfileDocRef = doc(db, "artifacts", appId, "public", "data", "profiles", user.uid); const userProfile = await getDoc(userProfileDocRef); if (userProfile.exists()) { renderMyProfile(userProfile.data()); showView('finder'); fetchAllUsers(); } else { showView('profile-creation'); } } }); saveProfileBtn.addEventListener('click', async () => { const name = document.getElementById('profile-name').value.trim(); const location = document.getElementById('profile-location').value.trim(); const speaks = document.getElementById('profile-speaks').value.split(',').map(s => s.trim().toLowerCase()).filter(Boolean); const learning = document.getElementById('profile-learning').value.split(',').map(s => s.trim().toLowerCase()).filter(Boolean); if (!name || speaks.length === 0 || learning.length === 0) { alert("Please fill in your name and at least one language for speaks/learning."); return; } const profileData = { name, location, speaks, learning }; const userProfileDocRef = doc(db, "artifacts", appId, "public", "data", "profiles", currentUser.uid); await setDoc(userProfileDocRef, profileData); renderMyProfile(profileData); showView('finder'); fetchAllUsers(); }); // --- RENDER FUNCTIONS --- const showView = (view) => { loadingView.classList.add('hidden'); profileCreationView.classList.toggle('hidden', view !== 'profile-creation'); finderView.classList.toggle('hidden', view !== 'finder'); }; const renderMyProfile = (profile) => { myProfileSummary.innerHTML = `

${profile.name}

${profile.location}

SPEAKS:

${profile.speaks.map(lang => `${lang}`).join('')}

LEARNING:

${profile.learning.map(lang => `${lang}`).join('')}
`; document.getElementById('download-profile-btn').addEventListener('click', () => downloadProfileAsPDF(profile)); }; const renderPartnerList = (users) => { partnerList.innerHTML = ''; if (users.length === 0) { partnerList.innerHTML = '

No partners found. Try adjusting your filters.

'; return; } users.forEach(user => { const card = document.createElement('div'); card.className = 'profile-card bg-white p-4 rounded-lg shadow border'; card.innerHTML = `

${user.name}

${user.location}

SPEAKS:

${user.speaks.map(lang => `${lang}`).join('')}

LEARNING:

${user.learning.map(lang => `${lang}`).join('')}
`; partnerList.appendChild(card); }); }; // --- DATA FETCHING & FILTERING --- const fetchAllUsers = async () => { const usersCol = collection(db, "artifacts", appId, "public", "data", "profiles"); const speaksFilter = filterSpeaksInput.value.trim().toLowerCase(); const learningFilter = filterLearningInput.value.trim().toLowerCase(); const userSnapshot = await getDocs(usersCol); let allUsers = []; userSnapshot.forEach(doc => { if (doc.id !== currentUser.uid) { // Don't show the current user allUsers.push(doc.data()); } }); const filteredUsers = allUsers.filter(user => { const speaksMatch = !speaksFilter || user.speaks.includes(speaksFilter); const learningMatch = !learningFilter || user.learning.includes(learningFilter); return speaksMatch && learningMatch; }); renderPartnerList(filteredUsers); }; filterSpeaksInput.addEventListener('input', fetchAllUsers); filterLearningInput.addEventListener('input', fetchAllUsers); // --- PDF DOWNLOAD --- const downloadProfileAsPDF = (profile) => { const { jsPDF } = window.jspdf; const doc = new jsPDF(); doc.setFontSize(22); doc.text('Language Exchange Profile', 105, 20, { align: 'center' }); doc.autoTable({ startY: 30, head: [['Field', 'Information']], body: [ ['Name', profile.name], ['Location', profile.location], ['Languages Spoken', profile.speaks.join(', ')], ['Languages Learning', profile.learning.join(', ')] ], theme: 'grid' }); doc.save(`${profile.name}-profile.pdf`); }; });
Scroll to Top