import React, { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; import { supabase } from '../supabaseClient'; import { SearchIcon } from './Icons'; import JobDetail from './JobDetail'; import ApplyModel from './ApplyModel'; import JobCard from './JobCard'; import VerificationModal from './VerificationModal'; // ✅ Import the new modal export default function JobListings({ searchQuery, setSearchQuery, isSearching, filteredJobListings }) { const [selectedJob, setSelectedJob] = useState(null); const [appliedJobIds, setAppliedJobIds] = useState(new Set()); const [applying, setApplying] = useState(null); // State for the Apply Modal const [jobToApply, setJobToApply] = useState(null); // ✅ State for Verification Modal const [showVerificationModal, setShowVerificationModal] = useState(false); // 1. Check Existing Applications on Load useEffect(() => { const fetchApplications = async () => { const { data: { user } } = await supabase.auth.getUser(); if (user) { const { data } = await supabase .from('applications') .select('job_id') .eq('user_id', user.id); if (data) { setAppliedJobIds(new Set(data.map(app => app.job_id))); } } }; fetchApplications(); }, []); // 2. Open Apply Modal const initiateApply = (jobId) => { const job = filteredJobListings.find(j => j.id === jobId); if (job) { setJobToApply(job); } }; // ✅ Helper: Send welcome message to applicant const sendApplicationConfirmationMessage = async (userId, jobTitle) => { try { const message = `Hello, Thank you for applying for the **${jobTitle}** position. We have received your application and our team is currently reviewing your profile. If your qualifications match our requirements, we will contact you shortly regarding the next steps in the selection process. We appreciate your interest in this opportunity.`; // Insert message using applicant's ID for both sender and receiver // (RLS prevents applicants from inserting messages on behalf of an Admin) const { error } = await supabase.from('messages').insert([{ sender_id: userId, receiver_id: userId, content: message, is_read: false }]); if (error) console.error('Error sending confirmation message:', error); } catch (err) { console.error('Failed to send confirmation message:', err); } }; // 3. Submit Application (With Verification Gatekeeper) const handleFinalSubmit = async (formData) => { if (!jobToApply) return; setApplying(jobToApply.id); try { const { data: { user } } = await supabase.auth.getUser(); if (!user) { alert("Please log in to apply."); return; } // --- 🔒 GATEKEEPER CHECK: Verify Phone Status --- const { data: profile, error: profileError } = await supabase .from('profiles') .select('is_phone_verified, experience_years') .eq('id', user.id) .single(); if (profileError) throw profileError; // If NOT verified, stop the application and show modal /** if (!profile.is_phone_verified) { setApplying(null); // Stop loading spinner setJobToApply(null); // Close application form setShowVerificationModal(true); // Open Verification Modal return; // 🛑 Stop execution here } **/ // --- ✅ IF VERIFIED: Proceed with Application --- const { error } = await supabase .from('applications') .insert([{ job_id: jobToApply.id, user_id: user.id, status: 'Pending', resume_url: formData.resume_url, cover_letter: formData.cover_letter, experience: profile.experience // Include experience from profile }]); if (error) throw error; // ✅ Send confirmation message to applicant await sendApplicationConfirmationMessage(user.id, jobToApply.title); setAppliedJobIds(prev => new Set(prev).add(jobToApply.id)); alert("Application submitted successfully!"); setJobToApply(null); } catch (error) { console.error("Error applying:", error.message); alert(`Could not apply: ${error.message}`); } finally { // Only stop spinner if we didn't switch to verification modal if (!showVerificationModal) setApplying(null); } }; // 4. Withdraw Application const handleWithdraw = async (jobId) => { if (!confirm("Are you sure you want to withdraw this application?")) { return; } try { const { data: { user } } = await supabase.auth.getUser(); if (!user) return; const { error } = await supabase .from('applications') .delete() .eq('job_id', jobId) .eq('user_id', user.id); if (error) throw error; setAppliedJobIds(prev => { const newSet = new Set(prev); newSet.delete(jobId); return newSet; }); } catch (error) { console.error("Error withdrawing:", error.message); alert("Failed to withdraw application."); } }; return ( <> {/* Search Bar */}
Browse through our current job openings