From a7e7ce3bfbfc74c7bc7a1b1b49ce56e534f180ef Mon Sep 17 00:00:00 2001 From: VinayJangotra <109384165+VinayJangotra@users.noreply.github.com> Date: Sat, 27 Apr 2024 15:01:54 +0530 Subject: [PATCH] My Jobs --- frontend/src/components/Job/MyJobs.jsx | 359 ++++++++++++++++++++++++- 1 file changed, 352 insertions(+), 7 deletions(-) diff --git a/frontend/src/components/Job/MyJobs.jsx b/frontend/src/components/Job/MyJobs.jsx index 1e69355..47fbb69 100644 --- a/frontend/src/components/Job/MyJobs.jsx +++ b/frontend/src/components/Job/MyJobs.jsx @@ -1,11 +1,356 @@ -import React from 'react' +import axios from "axios"; +import { useContext, useEffect, useState } from "react"; +import toast from "react-hot-toast"; +import { FaCheck } from "react-icons/fa6"; +import { RxCross2 } from "react-icons/rx"; +import { Context } from "../../main"; +import { useNavigate } from "react-router-dom"; const MyJobs = () => { + const [myJobs, setMyJobs] = useState([]); + const [editingMode, setEditingMode] = useState(null); + const { isAuthorized, user } = useContext(Context); + + const navigateTo = useNavigate(); + //Fetching all jobs + useEffect(() => { + const fetchJobs = async () => { + try { + const { data } = await axios.get( + "http://localhost:4000/api/v1/job/getmyjobs", + { withCredentials: true } + ); + setMyJobs(data.myJobs); + } catch (error) { + toast.error(error.response.data.message); + setMyJobs([]); + } + }; + fetchJobs(); + }, []); + if (!isAuthorized || (user && user.role !== "Employer")) { + navigateTo("/"); + } + + //Function For Enabling Editing Mode + const handleEnableEdit = (jobId) => { + //Here We Are Giving Id in setEditingMode because We want to enable only that job whose ID has been send. + setEditingMode(jobId); + }; + + //Function For Disabling Editing Mode + const handleDisableEdit = () => { + setEditingMode(null); + }; + + //Function For Updating The Job + const handleUpdateJob = async (jobId) => { + const updatedJob = myJobs.find((job) => job._id === jobId); + await axios + .put(`http://localhost:4000/api/v1/job/update/${jobId}`, updatedJob, { + withCredentials: true, + }) + .then((res) => { + toast.success(res.data.message); + setEditingMode(null); + }) + .catch((error) => { + toast.error(error.response.data.message); + }); + }; + + //Function For Deleting Job + const handleDeleteJob = async (jobId) => { + await axios + .delete(`http://localhost:4000/api/v1/job/delete/${jobId}`, { + withCredentials: true, + }) + .then((res) => { + toast.success(res.data.message); + setMyJobs((prevJobs) => prevJobs.filter((job) => job._id !== jobId)); + }) + .catch((error) => { + toast.error(error.response.data.message); + }); + }; + + const handleInputChange = (jobId, field, value) => { + // Update the job object in the jobs state with the new value + setMyJobs((prevJobs) => + prevJobs.map((job) => + job._id === jobId ? { ...job, [field]: value } : job + ) + ); + }; + return ( -
- -
- ) -} + <> +
+
+

Your Posted Jobs

+ {myJobs.length > 0 ? ( + <> +
+ {myJobs.map((element) => ( +
+
+
+
+ Title: + + handleInputChange( + element._id, + "title", + e.target.value + ) + } + /> +
+
+ {" "} + Country: + + handleInputChange( + element._id, + "country", + e.target.value + ) + } + /> +
+
+ City: + + handleInputChange( + element._id, + "city", + e.target.value + ) + } + /> +
+
+ Category: + +
+
+ + Salary:{" "} + {element.fixedSalary ? ( + + handleInputChange( + element._id, + "fixedSalary", + e.target.value + ) + } + /> + ) : ( +
+ + handleInputChange( + element._id, + "salaryFrom", + e.target.value + ) + } + /> + + handleInputChange( + element._id, + "salaryTo", + e.target.value + ) + } + /> +
+ )} +
+
+
+ {" "} + Expired: + +
+
+
+
+ Description:{" "} +