|
| 1 | +import { useAuth } from '../context/AuthContext'; |
| 2 | +import { useNavigate } from 'react-router-dom'; |
| 3 | +import useConversation from '../zustand/useConversation'; |
| 4 | +import { useState } from 'react'; |
| 5 | +import { |
| 6 | + Download, |
| 7 | + CheckCircle, |
| 8 | + XCircle, |
| 9 | + Clock, |
| 10 | + Building, |
| 11 | + User, |
| 12 | + FileText |
| 13 | +} from 'lucide-react'; |
| 14 | + |
| 15 | +const OfferCard = ({ offer, newStatus }) => { |
| 16 | + const navigate = useNavigate(); |
| 17 | + const { user } = useAuth(); |
| 18 | + const [status, setStatus] = useState(offer.status); |
| 19 | + const cvPath = offer.offerLetterPath.split("/").pop(); |
| 20 | + console.log(offer) |
| 21 | + const handleStatusChange = async (newStatus) => { |
| 22 | + try { |
| 23 | + // First, update the status of the offer |
| 24 | + const res = await fetch(`http://localhost:3500/offer/updateStatus/${offer.offerId}`, { |
| 25 | + method: "PUT", |
| 26 | + headers: { |
| 27 | + "Content-Type": "application/json" |
| 28 | + }, |
| 29 | + body: JSON.stringify({ status: newStatus }) |
| 30 | + }); |
| 31 | + |
| 32 | + if (res.ok) { |
| 33 | + setStatus(newStatus); |
| 34 | + |
| 35 | + // If status is "Accepted", call the hire API to create a hiring record |
| 36 | + if (newStatus === 'Accepted') { |
| 37 | + const hireRes = await fetch('http://localhost:3500/hiring/hire', { |
| 38 | + method: 'POST', |
| 39 | + headers: { |
| 40 | + 'Content-Type': 'application/json', |
| 41 | + }, |
| 42 | + body: JSON.stringify({ |
| 43 | + jobSeekerId: offer.jobSeekerId, // assuming `user.id` is the job seeker ID |
| 44 | + companyId: offer.companyId, // assuming `offer.company.id` is the company ID |
| 45 | + }), |
| 46 | + }); |
| 47 | + |
| 48 | + if (!hireRes.ok) { |
| 49 | + console.error("Failed to create hiring record"); |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + } catch (error) { |
| 54 | + console.error("Failed to update status", error); |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + const getStatusDetails = (currentStatus) => { |
| 59 | + switch (currentStatus) { |
| 60 | + case 'Pending': |
| 61 | + return { |
| 62 | + icon: <Clock className="w-5 h-5 text-amber-500" />, |
| 63 | + bgColor: 'bg-amber-50', |
| 64 | + textColor: 'text-amber-600' |
| 65 | + }; |
| 66 | + case 'Accepted': |
| 67 | + return { |
| 68 | + icon: <CheckCircle className="w-5 h-5 text-emerald-500" />, |
| 69 | + bgColor: 'bg-emerald-50', |
| 70 | + textColor: 'text-emerald-600' |
| 71 | + }; |
| 72 | + case 'Rejected': |
| 73 | + return { |
| 74 | + icon: <XCircle className="w-5 h-5 text-rose-500" />, |
| 75 | + bgColor: 'bg-rose-50', |
| 76 | + textColor: 'text-rose-600' |
| 77 | + }; |
| 78 | + default: |
| 79 | + return { |
| 80 | + icon: null, |
| 81 | + bgColor: 'bg-gray-50', |
| 82 | + textColor: 'text-gray-600' |
| 83 | + }; |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + const statusDetails = getStatusDetails(status); |
| 88 | + |
| 89 | + return ( |
| 90 | + <div className="bg-white rounded-3xl shadow-lg hover:shadow-xl transition-all duration-300 overflow-hidden "> |
| 91 | + {/* Header */} |
| 92 | + <div className="bg-gradient-to-r from-violet-100 to-indigo-100 text-gray-800 p-6"> |
| 93 | + <div className="flex justify-between items-start"> |
| 94 | + <div className="flex items-center space-x-4"> |
| 95 | + <Building className="w-8 h-8 text-violet-500" /> |
| 96 | + <div> |
| 97 | + <h4 className="text-2xl font-bold mb-1 tracking-wide">{offer.company.name}</h4> |
| 98 | + <div className="flex items-center space-x-2 text-sm text-gray-600"> |
| 99 | + <User className="w-4 h-4" /> |
| 100 | + <span>{offer.application.jobPost.position}</span> |
| 101 | + </div> |
| 102 | + </div> |
| 103 | + </div> |
| 104 | + <div className="flex items-center space-x-2"> |
| 105 | + {statusDetails.icon} |
| 106 | + <span className={`px-3 py-1 rounded-full text-xs font-medium ${statusDetails.bgColor} ${statusDetails.textColor}`}> |
| 107 | + {status} |
| 108 | + </span> |
| 109 | + </div> |
| 110 | + </div> |
| 111 | + </div> |
| 112 | + |
| 113 | + {/* Card Body */} |
| 114 | + <div className="p-6"> |
| 115 | + <div className="flex flex-col sm:flex-row justify-between items-center space-y-4 sm:space-y-0 sm:space-x-4"> |
| 116 | + {/* Download Offer Letter Button */} |
| 117 | + <a |
| 118 | + href={`http://localhost:3500/apply/download/${cvPath}`} |
| 119 | + download |
| 120 | + className=" |
| 121 | + w-full sm:w-auto flex items-center justify-center |
| 122 | + px-6 py-3 |
| 123 | + bg-violet-100 |
| 124 | + text-violet-700 |
| 125 | + rounded-full |
| 126 | + hover:bg-violet-200 |
| 127 | + transition duration-300 |
| 128 | + space-x-2 |
| 129 | + group |
| 130 | + " |
| 131 | + > |
| 132 | + <FileText className="w-5 h-5" /> |
| 133 | + <span>Download Offer Letter</span> |
| 134 | + </a> |
| 135 | + |
| 136 | + {/* Status Change Dropdown for Job Seeker */} |
| 137 | + {user.userType === 'JobSeeker' && ( |
| 138 | + <select |
| 139 | + className=" |
| 140 | + w-full sm:w-auto |
| 141 | + px-6 py-3 |
| 142 | + bg-violet-100 |
| 143 | + text-violet-700 |
| 144 | + rounded-full |
| 145 | + focus:outline-none |
| 146 | + focus:ring-2 |
| 147 | + focus:ring-offset-2 |
| 148 | + focus:ring-violet-300 |
| 149 | + " |
| 150 | + value={status} |
| 151 | + onChange={(e) => handleStatusChange(e.target.value)} |
| 152 | + > |
| 153 | + <option value="Pending">Pending</option> |
| 154 | + <option value="Accepted">Accepted</option> |
| 155 | + <option value="Rejected">Rejected</option> |
| 156 | + </select> |
| 157 | + )} |
| 158 | + </div> |
| 159 | + </div> |
| 160 | + </div> |
| 161 | + ); |
| 162 | +}; |
| 163 | + |
| 164 | +export default OfferCard; |
0 commit comments