File size: 1,562 Bytes
62c3fe0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
  import React, { useEffect, useState } from "react";
  import { useParams, useNavigate } from "react-router-dom";
  import JobPageSection from "../sections/jobs/JobPageSection"; // Import JobPageSection
  import jobData from "../data/jobdescription"; // Import job data

  const JobPage = () => {
    const { slug } = useParams(); // Extract slug from the route
    const navigate = useNavigate(); // For redirecting if there's an error
    const [job, setJob] = useState(null);
    const [error, setError] = useState(null);

    useEffect(() => {
      // Validate the slug to ensure it's an integer
      if (!/^\d+$/.test(slug)) {
        setError("Invalid job slug. Redirecting...");
        setTimeout(() => navigate("/jobs"), 2000); // Redirect to an error page or home
        return;
      }

      // Find the job description based on the slug
      const foundJob = jobData.find((item) => item.id === parseInt(slug, 10));
      if (!foundJob) {
        setError("Job description not found. Redirecting...");
        setTimeout(() => navigate("/jobs"), 2000); // Redirect if not found
      } else {
        setJob(foundJob);
      }
    }, [slug, navigate]);

    // Display error message if any
    if (error) return <div>{error}</div>;

    // If no job data, return a "Loading..." message
    if (!job) {
      return <div>Loading...</div>;
    }

    // Render the job description content
    return (
      <div>
        <JobPageSection job={job} /> {/* Pass the job data to JobPageSection */}
      </div>
    );
  };

  export default JobPage;