'use client'; import { useState, useTransition } from 'react'; import { Card, CardContent, CardHeader, CardTitle } from "@workspace/ui/components/card" import { Button } from "@workspace/ui/components/button" import { Badge } from "@workspace/ui/components/badge" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@workspace/ui/components/dialog" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@workspace/ui/components/select" import { Building2, MapPin, DollarSign, Calendar, Star, CheckCircle, FileText, Upload, AlertCircle } from "lucide-react" import { applyForJob } from "../app/(main)/actions" import { type InferSelectModel } from '@workspace/db/drizzle'; import { jobs, companies, resumes } from '@workspace/db/schema'; export type Job = InferSelectModel & { company: typeof companies.$inferSelect; }; export type Resume = typeof resumes.$inferSelect; interface JobApplicationModalProps { job: Job; studentId: number; resumes: Resume[]; isApplied?: boolean; } export default function JobApplicationModal({ job, studentId, resumes, isApplied = false }: JobApplicationModalProps) { const [isOpen, setIsOpen] = useState(false); const [selectedResume, setSelectedResume] = useState(''); const [isPending, startTransition] = useTransition(); const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null); const deadline = new Date(job.applicationDeadline); const isDeadlinePassed = new Date() > deadline; const handleApply = async () => { if (!selectedResume) { setMessage({ type: 'error', text: 'Please select a resume' }); return; } setMessage(null); startTransition(async () => { try { const result = await applyForJob(job.id, studentId, parseInt(selectedResume)); if (result.success) { setMessage({ type: 'success', text: 'Application submitted successfully!' }); setTimeout(() => { setIsOpen(false); setMessage(null); setSelectedResume(''); }, 2000); } else { setMessage({ type: 'error', text: result.error || 'Failed to submit application' }); } } catch (error) { setMessage({ type: 'error', text: error instanceof Error ? error.message : 'An error occurred while submitting your application' }); } }); }; const cannotApplyReason = isApplied ? 'You have already applied to this job' : resumes.length === 0 ? 'No resumes found. Please upload a resume first.' : isDeadlinePassed ? 'Application deadline has passed' : null; return ( !isPending && setIsOpen(open)}> {!cannotApplyReason && (
)} {cannotApplyReason && (
{cannotApplyReason}
)} Apply for {job.title}
{/* Job Details */}

{job.title}

{job.company.name}

Active
{job.location}
{job.salary}
Deadline: {deadline.toLocaleDateString()}
Min CGPA: {job.minCGPA}

{job.description}

{/* Application Form */}

Application Details

{selectedResume && ( r.id.toString() === selectedResume)?.fileUrl} target="_blank" rel="noopener noreferrer" className="text-sm text-blue-600 underline mt-2 inline-block" > Preview selected resume )} {resumes.length === 0 && (

No resumes found. Please upload a resume first.

)}
{/* Message Display */} {message && (
{message.type === 'success' ? ( ) : ( )} {message.text}
)} {/* Action Buttons */}
{/* Additional Info */}
Important Notes:
  • • Make sure your resume is up-to-date and relevant to this position
  • • You can only apply once per job posting
  • • Applications will be reviewed by the company within 1-2 weeks
  • • You'll receive email notifications about your application status
); }