File size: 10,758 Bytes
ea9ca44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9574
ea9ca44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e5bfbf
59f9574
 
 
 
6e5bfbf
ea9ca44
59f9574
ea9ca44
 
 
 
 
 
 
 
 
59f9574
 
ea9ca44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59f9574
 
6e5bfbf
59f9574
 
 
6e5bfbf
 
 
 
 
 
 
 
59f9574
 
 
 
 
 
 
 
 
 
 
ea9ca44
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
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 = () => (
    <div style={{
        backgroundColor: 'rgba(255, 255, 255, 0.03)',
        borderRadius: '1rem',
        padding: '1.5rem',
        marginBottom: '1rem',
        border: '1px solid rgba(255, 255, 255, 0.05)',
        display: 'flex',
        flexDirection: 'column',
        gap: '1rem'
    }}>
        <div style={{ display: 'flex', gap: '1rem', alignItems: 'center' }}>
            <div style={{ width: '48px', height: '48px', borderRadius: '10px', backgroundColor: 'rgba(255,255,255,0.1)' }}></div>
            <div style={{ flex: 1 }}>
                <div style={{ width: '40%', height: '20px', borderRadius: '4px', backgroundColor: 'rgba(255,255,255,0.1)', marginBottom: '8px' }}></div>
                <div style={{ width: '25%', height: '16px', borderRadius: '4px', backgroundColor: 'rgba(255,255,255,0.05)' }}></div>
            </div>
        </div>
        <div style={{ width: '100%', height: '1px', backgroundColor: 'rgba(255,255,255,0.05)' }}></div>
    </div>
);

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 (
        <ApplicantLayout activePage="applicant-jobs" onNavigate={onNavigate}>

            <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: '1.5rem' }}>
                <h2 style={{ fontSize: '1.5rem', fontWeight: 'bold' }}>
                    {activeTab === 'recommended' ? 'Jobs For You' : 'All Opportunities'}
                </h2>
                <div style={{ backgroundColor: 'rgba(255,255,255,0.1)', padding: '0.25rem', borderRadius: '0.5rem', display: 'flex', gap: '0.5rem' }}>
                    <button onClick={() => { setActiveTab('all'); setShowAllJobs(false); }} style={{ padding: '0.5rem 1rem', borderRadius: '0.3rem', border: 'none', cursor: 'pointer', fontWeight: 'bold', backgroundColor: activeTab === 'all' ? '#FBBF24' : 'transparent', color: activeTab === 'all' ? '#1a202c' : '#9ca3af' }}>All Jobs</button>
                    <button onClick={() => { setActiveTab('recommended'); setShowAllJobs(false); }} style={{ padding: '0.5rem 1rem', borderRadius: '0.3rem', border: 'none', cursor: 'pointer', fontWeight: 'bold', backgroundColor: activeTab === 'recommended' ? '#FBBF24' : 'transparent', color: activeTab === 'recommended' ? '#1a202c' : '#9ca3af' }}>Recommended ✨</button>
                </div>
            </div>

            {loading ? (
                <div>
                    <SkeletonLoader />
                    <SkeletonLoader />
                    <SkeletonLoader />
                </div>
            ) : (
                <>
                    {activeTab === 'recommended' && filteredJobs.length === 0 ? (
                        <div style={{ textAlign: 'center', padding: '3rem', color: '#9ca3af', border: '1px dashed #374151', borderRadius: '1rem' }}>
                            <p style={{ fontSize: '1.2rem', marginBottom: '1rem' }}>No direct matches found yet.</p>
                            <p>We look for matches based on your <strong>Job Title</strong> or <strong>Skills</strong>.</p>

                            {userProfile && !userProfile.job_title && !userProfile.skills && (
                                <p style={{ color: '#F87171', marginTop: '10px', fontSize: '0.9rem' }}>
                                    ⚠️ Your profile has no Job Title or Skills saved.
                                </p>
                            )}

                            <button onClick={() => onNavigate('applicant-profile')} style={{ marginTop: '1rem', padding: '0.5rem 1rem', backgroundColor: '#374151', color: 'white', border: 'none', borderRadius: '0.5rem', cursor: 'pointer' }}>Update Profile</button>
                        </div>
                    ) : (
                        <>
                            <JobListings searchQuery={searchQuery} setSearchQuery={setSearchQuery} isSearching={searchQuery.length > 0} filteredJobListings={filteredJobs} />

                            {/* ✅ See All Button */}
                            {!showAllJobs && !searchQuery && (
                                <div style={{ display: 'flex', justifyContent: 'center', marginTop: '2rem' }}>
                                    <button
                                        onClick={() => setShowAllJobs(true)}
                                        style={{
                                            padding: '0.75rem 2rem',
                                            backgroundColor: '#FBBF24',
                                            color: '#1a202c',
                                            border: 'none',
                                            borderRadius: '0.5rem',
                                            cursor: 'pointer',
                                            fontWeight: 'bold',
                                            fontSize: '1rem',
                                            transition: 'all 0.2s'
                                        }}
                                    >
                                        See All Jobs
                                    </button>
                                </div>
                            )}
                        </>
                    )}
                </>
            )}

        </ApplicantLayout>
    );
}