import React, { useState, useEffect } from 'react'; import { supabase } from '../supabaseClient'; import JobListings from '../components/JobListings'; import ApplicantLayout from '../components/ApplicantLayout'; // Simple "Ghost" Card Component for loading state const SkeletonLoader = () => (
); export default function ApplicantJobPage({ onNavigate }) { const [allJobs, setAllJobs] = useState([]); const [filteredJobs, setFilteredJobs] = useState([]); const [userProfile, setUserProfile] = useState(null); const [searchQuery, setSearchQuery] = useState(''); const [activeTab, setActiveTab] = useState('all'); const [loading, setLoading] = useState(true); const [showAllJobs, setShowAllJobs] = useState(false); // ✅ Track if showing all jobs useEffect(() => { const fetchData = async () => { setLoading(true); try { // 1️⃣ Get User const { data: { user } } = await supabase.auth.getUser(); if (user) { const { data: profile } = await supabase .from('profiles') .select('skills, job_title') .eq('id', user.id) .maybeSingle(); if (profile) { console.log("LOG: User Profile Found:", profile); setUserProfile(profile); } } // 🔹 Today (for DATE column comparison) const today = new Date().toISOString().split('T')[0]; // 2️⃣ Fetch ACTIVE jobs first const { data: jobsData, error } = await supabase .from('jobs') .select(` *, companies ( name, logo_url ) `) .eq('status', 'Active'); if (error) throw error; // 3️⃣ Find expired jobs const expiredJobIds = (jobsData || []) .filter(job => job.deadline && job.deadline < today) .map(job => job.id); // 4️⃣ Mark expired jobs as INACTIVE if (expiredJobIds.length > 0) { await supabase .from('jobs') .update({ status: 'Inactive' }) .in('id', expiredJobIds); } // 5️⃣ Fetch ONLY ACTIVE jobs (final list) const { data: activeJobs, error: activeError } = await supabase .from('jobs') .select(` *, companies ( name, logo_url ) `) .eq('status', 'Active') .order('created_at', { ascending: false }); if (activeError) throw activeError; // 6️⃣ Format jobs for UI const formattedJobs = (activeJobs || []).map(job => ({ id: job.id, title: job.title, type: job.job_type, company: job.companies?.name || 'Unknown Company', logo: job.companies?.logo_url || '', location: job.location || 'Remote', salary: job.salary_range || job.salary || 'Not disclosed', deadline: job.deadline ? new Date(job.deadline).toLocaleDateString() : 'Open', postedAt: new Date(job.created_at).toLocaleDateString(), skills: job.skills_required || [], description: job.description })); setAllJobs(formattedJobs); setFilteredJobs(formattedJobs); } catch (error) { console.error("Error fetching data:", error.message); } finally { setLoading(false); } }; fetchData(); }, []); // --- SMART RECOMMENDATION ENGINE --- const getRecommendedJobs = () => { if (!userProfile) return []; return allJobs.filter(job => { const userSkills = userProfile.skills || []; const jobSkills = job.skills || []; const skillMatch = jobSkills.some(skill => userSkills.some(userSkill => userSkill.toLowerCase() === skill.toLowerCase()) ); let titleMatch = false; if (userProfile.job_title) { const userTitle = userProfile.job_title.toLowerCase(); const jobTitle = job.title.toLowerCase(); titleMatch = jobTitle.includes(userTitle) || userTitle.includes(jobTitle); } return skillMatch || titleMatch; }); }; useEffect(() => { let result = activeTab === 'recommended' ? getRecommendedJobs() : allJobs; if (searchQuery) { const lowerQuery = searchQuery.toLowerCase(); result = result.filter(job => job.title.toLowerCase().includes(lowerQuery) || job.company.toLowerCase().includes(lowerQuery) ); } // ✅ Limit to 4 jobs unless "See All" is clicked if (!showAllJobs && !searchQuery) { result = result.slice(0, 4); } setFilteredJobs(result); }, [searchQuery, activeTab, allJobs, userProfile, showAllJobs]); return (

{activeTab === 'recommended' ? 'Jobs For You' : 'All Opportunities'}

{loading ? (
) : ( <> {activeTab === 'recommended' && filteredJobs.length === 0 ? (

No direct matches found yet.

We look for matches based on your Job Title or Skills.

{userProfile && !userProfile.job_title && !userProfile.skills && (

⚠️ Your profile has no Job Title or Skills saved.

)}
) : ( <> 0} filteredJobListings={filteredJobs} /> {/* ✅ See All Button */} {!showAllJobs && !searchQuery && (
)} )} )}
); }