Skip to content

Commit

Permalink
Merge pull request #30 from IvanoskiHarmonia/transfer-jobs-to-mongodb…
Browse files Browse the repository at this point in the history
…-instead-of-using-static-ones

transfers job data from a static JSON file to a MongoDB database to facilitate future job additions and improve data management. Additionally, it fixes an issue that prevents users from applying to the same job more than once and ensures the "More details" screen displays a spinner before showing job descriptions and the apply button.
  • Loading branch information
IvanoskiHarmonia authored Jun 7, 2024
2 parents 4c1a686 + 1b664f6 commit b92c454
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 357 deletions.
1 change: 1 addition & 0 deletions client/src/common/api/getJobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const getJobs = async () => {
const errorMessage = `Error ${response.status}: ${response.statusText}`;
throw new Error(errorMessage);
}

return response.data;
};

Expand Down
59 changes: 27 additions & 32 deletions client/src/ui/modules/components/card/JobDetails/JobDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ArrowRight } from "react-feather";
import JobApplicationDetails from "./JobApplicationDetails";
import JobApplicationDetailsPlaceholder from "./JobApplicationDetailsPlaceholder";
import { useAuth } from "../../../../../common/hooks/useAuth";
import SpinnerOverlay from "../../loading/SpinnerOverlay";

const JobDetails = ({ setJob, job, detailsScreen = false }) => {
const [loading, setLoading] = useState(true);
Expand All @@ -15,48 +16,42 @@ const JobDetails = ({ setJob, job, detailsScreen = false }) => {
const navigate = useNavigate();

useEffect(() => {
if (detailsScreen) {
const fetchUserAppliedToJob = async () => {
axios
.get(`/api/user-applications/check-application/${userId}/${jobId}`)
.then((response) => {
setDisableApplyButton(true);
setApplicationDetails(response.data);
})
.catch((error) => {
console.error("Error fetching user application status:", error.response.data.message);
});
};
const fetchData = async () => {
if (detailsScreen) {
try {
const response = await axios.get(`/api/user-applications/check-application/${userId}/${jobId}`);
setDisableApplyButton(true);
setApplicationDetails(response.data);
} catch (error) {
console.error("Error fetching user application status:", error.response.data.message);
}
}

fetchUserAppliedToJob();
}

const fetchDetails = async () => {
axios
.get(`/api/jobs/${jobId}`)
.then((response) => {
setJob(response.data.job);
})
.catch((error) => {
console.error("Error fetching job description:", error);
})
.finally(() => {
setLoading(false);
});
try {
const response = await axios.get(`/api/jobs/${jobId}`);
setJob(response.data.job);
} catch (error) {
console.error("Error fetching job description:", error);
} finally {
setLoading(false);
}
};

fetchDetails();
}, [jobId, setJob, detailsScreen, userId, disableApplyButton]);
fetchData();
}, [jobId, setJob, detailsScreen, userId]);

const handleApply = () => {
navigate(`/careers/apply/${jobId}`);
};

if (loading) {
return (
<div className="col-lg-10 offset-lg-1">
<JobApplicationDetailsPlaceholder />
</div>
<>
<div className="col-lg-10 offset-lg-1">
<JobApplicationDetailsPlaceholder />
</div>
<SpinnerOverlay />
</>
);
}

Expand Down
13 changes: 8 additions & 5 deletions server/controllers/jobsController.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
const jobs = require("../data/jobs.json");
const Job = require("../models/Job");

const getAllJobs = (req, res) => {
const getAllJobs = async (req, res) => {
const jobs = await Job.find({});
res.send(jobs);
};

const getJobById = (req, res) => {
const job = jobs.find((job) => job.id === parseInt(req.params.jobId));
const getJobById = async (req, res) => {
const jobId = parseInt(req.params.jobId);
const job = await Job.findOne({ id: jobId });
if (!job) {
return res.status(404).send("Job not found");
}
res.send({ job });
};

const getJobsByTitleOrDescription = (req, res) => {
const getJobsByTitleOrDescription = async (req, res) => {
const searchTerm = req.params.searchTerm.toLowerCase();
const jobs = await Job.find({});
const matchingJobs = jobs.filter((job) => job.title.toLowerCase().includes(searchTerm) || job.description.toLowerCase().includes(searchTerm));
if (matchingJobs.length === 0) {
return res.status(404).send("No jobs found");
Expand Down
Loading

0 comments on commit b92c454

Please sign in to comment.