All files / src/pages JobDetails.js

2.7% Statements 1/37
0% Branches 0/51
0% Functions 0/9
2.94% Lines 1/34

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294                1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          
import React, { useState } from 'react';
import { useParams, Link } from 'react-router-dom';
import { useQuery } from 'react-query';
import axios from 'axios';
import { useAuth } from '../contexts/AuthContext';
import { MapPin, Clock, DollarSign, Briefcase, Users, Calendar } from 'lucide-react';
import toast from 'react-hot-toast';
 
const JobDetails = () => {
  const { id } = useParams();
  const { user } = useAuth();
  const [applying, setApplying] = useState(false);
  const [coverLetter, setCoverLetter] = useState('');
 
  const { data: job, isLoading } = useQuery(['job', id], async () => {
    const response = await axios.get(`/api/jobs/${id}`);
    return response.data;
  });
 
  const handleApply = async () => {
    if (!coverLetter.trim()) {
      toast.error('Please provide a cover letter');
      return;
    }
 
    setApplying(true);
    try {
      await axios.post('/api/applications', {
        jobId: id,
        coverLetter: coverLetter.trim()
      });
      toast.success('Application submitted successfully!');
      setCoverLetter('');
    } catch (error) {
      toast.error(error.response?.data?.error || 'Failed to submit application');
    } finally {
      setApplying(false);
    }
  };
 
  if (isLoading) {
    return (
      <div className="flex items-center justify-center h-64">
        <div className="animate-spin rounded-full h-32 w-32 border-b-2 border-primary-600"></div>
      </div>
    );
  }
 
  if (!job) {
    return (
      <div className="text-center py-12">
        <h3 className="text-lg font-medium text-gray-900">Job not found</h3>
        <p className="mt-1 text-sm text-gray-500">
          The job you're looking for doesn't exist or has been removed.
        </p>
        <div className="mt-6">
          <Link to="/jobs" className="btn btn-primary">
            Browse Jobs
          </Link>
        </div>
      </div>
    );
  }
 
  const formatSalary = (min, max, currency = 'USD') => {
    if (!min && !max) return 'Salary not specified';
    if (!min) return `Up to ${currency} ${max?.toLocaleString()}`;
    if (!max) return `From ${currency} ${min?.toLocaleString()}`;
    return `${currency} ${min?.toLocaleString()} - ${max?.toLocaleString()}`;
  };
 
  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <Link to="/jobs" className="text-sm text-primary-600 hover:text-primary-500">
            ← Back to Jobs
          </Link>
          <h1 className="mt-2 text-3xl font-bold text-gray-900">{job.title}</h1>
          <div className="mt-2 flex items-center text-lg text-gray-600">
            <Briefcase className="h-5 w-5 mr-2" />
            {job.company_name}
          </div>
        </div>
        {user?.role === 'candidate' && job.status === 'active' && (
          <div className="flex space-x-3">
            <button
              onClick={handleApply}
              disabled={applying}
              className="btn btn-primary disabled:opacity-50"
            >
              {applying ? 'Applying...' : 'Apply Now'}
            </button>
          </div>
        )}
      </div>
 
      <div className="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div className="lg:col-span-2 space-y-6">
          {/* Job Description */}
          <div className="bg-white shadow rounded-lg">
            <div className="px-4 py-5 sm:p-6">
              <h2 className="text-lg font-medium text-gray-900 mb-4">Job Description</h2>
              <div className="prose max-w-none">
                <p className="text-gray-600 whitespace-pre-wrap">{job.description}</p>
              </div>
            </div>
          </div>
 
          {/* Requirements */}
          {job.requirements && job.requirements.length > 0 && (
            <div className="bg-white shadow rounded-lg">
              <div className="px-4 py-5 sm:p-6">
                <h2 className="text-lg font-medium text-gray-900 mb-4">Requirements</h2>
                <ul className="list-disc list-inside space-y-2">
                  {job.requirements.map((requirement, index) => (
                    <li key={index} className="text-gray-600">{requirement}</li>
                  ))}
                </ul>
              </div>
            </div>
          )}
 
          {/* Responsibilities */}
          {job.responsibilities && job.responsibilities.length > 0 && (
            <div className="bg-white shadow rounded-lg">
              <div className="px-4 py-5 sm:p-6">
                <h2 className="text-lg font-medium text-gray-900 mb-4">Responsibilities</h2>
                <ul className="list-disc list-inside space-y-2">
                  {job.responsibilities.map((responsibility, index) => (
                    <li key={index} className="text-gray-600">{responsibility}</li>
                  ))}
                </ul>
              </div>
            </div>
          )}
 
          {/* Skills Required */}
          {job.skills_required && job.skills_required.length > 0 && (
            <div className="bg-white shadow rounded-lg">
              <div className="px-4 py-5 sm:p-6">
                <h2 className="text-lg font-medium text-gray-900 mb-4">Required Skills</h2>
                <div className="flex flex-wrap gap-2">
                  {job.skills_required.map((skill, index) => (
                    <span
                      key={index}
                      className="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-primary-100 text-primary-800"
                    >
                      {skill}
                    </span>
                  ))}
                </div>
              </div>
            </div>
          )}
 
          {/* Benefits */}
          {job.benefits && job.benefits.length > 0 && (
            <div className="bg-white shadow rounded-lg">
              <div className="px-4 py-5 sm:p-6">
                <h2 className="text-lg font-medium text-gray-900 mb-4">Benefits</h2>
                <ul className="list-disc list-inside space-y-2">
                  {job.benefits.map((benefit, index) => (
                    <li key={index} className="text-gray-600">{benefit}</li>
                  ))}
                </ul>
              </div>
            </div>
          )}
        </div>
 
        {/* Sidebar */}
        <div className="space-y-6">
          {/* Job Details */}
          <div className="bg-white shadow rounded-lg">
            <div className="px-4 py-5 sm:p-6">
              <h3 className="text-lg font-medium text-gray-900 mb-4">Job Details</h3>
              <div className="space-y-4">
                <div className="flex items-center">
                  <MapPin className="h-5 w-5 text-gray-400 mr-3" />
                  <div>
                    <p className="text-sm font-medium text-gray-900">{job.location}</p>
                    {job.remote_allowed && (
                      <p className="text-sm text-gray-500">Remote work allowed</p>
                    )}
                  </div>
                </div>
 
                <div className="flex items-center">
                  <Briefcase className="h-5 w-5 text-gray-400 mr-3" />
                  <div>
                    <p className="text-sm font-medium text-gray-900 capitalize">
                      {job.employment_type?.replace('-', ' ')}
                    </p>
                  </div>
                </div>
 
                <div className="flex items-center">
                  <DollarSign className="h-5 w-5 text-gray-400 mr-3" />
                  <div>
                    <p className="text-sm font-medium text-gray-900">
                      {formatSalary(job.salary_min, job.salary_max, job.currency)}
                    </p>
                  </div>
                </div>
 
                {job.experience_level && (
                  <div className="flex items-center">
                    <Users className="h-5 w-5 text-gray-400 mr-3" />
                    <div>
                      <p className="text-sm font-medium text-gray-900 capitalize">
                        {job.experience_level} level
                      </p>
                    </div>
                  </div>
                )}
 
                <div className="flex items-center">
                  <Clock className="h-5 w-5 text-gray-400 mr-3" />
                  <div>
                    <p className="text-sm font-medium text-gray-900">
                      Posted {new Date(job.created_at).toLocaleDateString()}
                    </p>
                  </div>
                </div>
 
                {job.application_deadline && (
                  <div className="flex items-center">
                    <Calendar className="h-5 w-5 text-gray-400 mr-3" />
                    <div>
                      <p className="text-sm font-medium text-gray-900">
                        Apply by {new Date(job.application_deadline).toLocaleDateString()}
                      </p>
                    </div>
                  </div>
                )}
              </div>
            </div>
          </div>
 
          {/* Company Info */}
          <div className="bg-white shadow rounded-lg">
            <div className="px-4 py-5 sm:p-6">
              <h3 className="text-lg font-medium text-gray-900 mb-4">Company</h3>
              <div className="space-y-2">
                <p className="text-sm font-medium text-gray-900">{job.company_name}</p>
                {job.industry && (
                  <p className="text-sm text-gray-500">{job.industry}</p>
                )}
                {job.company_size && (
                  <p className="text-sm text-gray-500">{job.company_size} employees</p>
                )}
              </div>
            </div>
          </div>
 
          {/* Apply Section for Candidates */}
          {user?.role === 'candidate' && job.status === 'active' && (
            <div className="bg-white shadow rounded-lg">
              <div className="px-4 py-5 sm:p-6">
                <h3 className="text-lg font-medium text-gray-900 mb-4">Apply for this job</h3>
                <div className="space-y-4">
                  <div>
                    <label htmlFor="coverLetter" className="block text-sm font-medium text-gray-700">
                      Cover Letter
                    </label>
                    <textarea
                      id="coverLetter"
                      rows={4}
                      className="mt-1 block w-full border border-gray-300 rounded-md shadow-sm focus:ring-primary-500 focus:border-primary-500 sm:text-sm"
                      placeholder="Tell us why you're interested in this position..."
                      value={coverLetter}
                      onChange={(e) => setCoverLetter(e.target.value)}
                    />
                  </div>
                  <button
                    onClick={handleApply}
                    disabled={applying || !coverLetter.trim()}
                    className="w-full btn btn-primary disabled:opacity-50"
                  >
                    {applying ? 'Applying...' : 'Submit Application'}
                  </button>
                </div>
              </div>
            </div>
          )}
        </div>
      </div>
    </div>
  );
};
 
export default JobDetails;