import { useCallback, useEffect, useMemo, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import usersService from "../../services/UsersService"; import { PriceFormatter } from "../Helpers/PriceFormatter"; import SelectBox from "../Helpers/SelectBox"; import { handlePagingFunc } from "../Pagination/HandlePagination"; import PaginatedList from "../Pagination/PaginatedList"; import LoadingSpinner from "../Spinners/LoadingSpinner"; import DeleteJobPopout from "../jobPopout/DeleteJobPopout"; import EditJobPopOut from "../jobPopout/EditJobPopout"; import DeleteIcon from "../../assets/images/icon-delete.svg"; import EditIcon from "../../assets/images/icon-edit.svg"; import { tableReload } from "../AddJob/settings"; import CreditPopup from "../MyWallet/Popup/CreditPopup"; import JobListPopout from "../jobPopout/JobListPopout"; export default function MyJobTable({ MyJobList, reloadJobList, className }) { const dispatch = useDispatch(); // Getting the categories const currentJobCart = MyJobList?.data?.categories; // DropDown Box const filterCategories = { All: "All Categories", ...currentJobCart }; const [selectedCategory, setCategory] = useState( Object.keys(filterCategories)[0] ); const [jobPopout, setJobPopout] = useState({ show: false, data: {} }); const [deleteJobPopout, setDeleteJobPopout] = useState({ show: false, data: {}, }); const [editJob, setEditJob] = useState({ show: false, data: {} }); const [myCountry, setCountries] = useState(""); const { userDetails: { country }, } = useSelector((state) => state?.userDetails); const userApi = useMemo(() => new usersService(), []); const [creditPopup, setCreditPopup] = useState({ show: false, data: {} }); const [walletItem, setWalletItem] = useState(null); /** * Opens the credit popup. * @param {Object} value - The value object. */ const openPopUp = (value) => { setCreditPopup({ show: true, data: { ...value }, }); }; /** * Closes the credit popup and dispatches a table reload action. */ const closePopUp = () => { setCreditPopup({ show: false, data: {} }); dispatch(tableReload({ type: "WALLETTABLE" })); }; // Get Country Api const getCountryList = useCallback(async () => { try { const res = await userApi.getSignupCountryData(); if (res.status === 200 && res.data.internal_return >= 0) { const { data: { result_list }, } = await res; let checkCountry = result_list ?.filter((item) => item.code == country) ?.map((item) => item.country) .join(""); setCountries(checkCountry); } } catch (error) { throw new Error(error); } }, [userApi, country]); useEffect(() => { getCountryList(); }, [getCountryList]); // Handle Pagination const [currentPage, setCurrentPage] = useState(0); const indexOfFirstItem = Number(currentPage); const indexOfLastItem = Number(indexOfFirstItem) + Number(process.env.REACT_APP_ITEM_PER_PAGE); // Handle Filter Job List const filterJobList = () => { if (selectedCategory === "All") return MyJobList?.data?.result_list; else return MyJobList?.data?.result_list?.filter((item) => item.category.includes(selectedCategory) ); }; const currentJobList = filterJobList(); // Handling Filter Pagination const filteredCurrentJobList = currentJobList?.slice( indexOfFirstItem, indexOfLastItem ); const handlePagination = (e) => { handlePagingFunc(e, setCurrentPage); }; // Handles the category selection const handleSetCategory = (value) => { setCurrentPage(0); for (let i in filterCategories) { if (filterCategories[i] == value) { setCategory(i); } } }; const { MyJobListHeader, MyJobListTable } = myJobTableFeatures( filterCategories, selectedCategory, handleSetCategory, setDeleteJobPopout, setEditJob, setJobPopout, MyJobList, filteredCurrentJobList, handlePagination, currentPage, currentJobList, indexOfFirstItem, indexOfLastItem ); return (
{MyJobList?.loading ? ( ) : ( )} {/* Job List Popout */} {jobPopout.show && ( { setJobPopout({ show: false, data: {} }); }} setWalletItem={setWalletItem} openWallet={openPopUp} situation={jobPopout.show} /> )} {/* End of Job List Popout */} {/* Delete Job Popout */} {deleteJobPopout.show && ( { setDeleteJobPopout({ show: false, data: {} }); }} reloadJobList={reloadJobList} situation={deleteJobPopout.show} /> )} {/* END of Delete Job Popout */} {editJob.show && ( { setEditJob({ show: false, data: {}, }); }} situation={editJob.show} country={myCountry} categories={currentJobCart} /> )} {creditPopup.show && ( )}
); } function myJobTableFeatures( filterCategories, selectedCategory, handleSetCategory, setDeleteJobPopout, setEditJob, setJobPopout, MyJobList, filteredCurrentJobList, handlePagination, currentPage, currentJobList, indexOfFirstItem, indexOfLastItem ) { // List of job table features const MyJobListHeader = () => (

{filterCategories[selectedCategory]} Jobs

); const JobListItem = ({ value, index, image_server }) => { let thePrice = PriceFormatter( value?.price * 0.01, value?.currency_code, value?.currency ); const image = localStorage.getItem("session_token") ? `${image_server}${localStorage.getItem("session_token")}/job/${ value.job_uid }` : ""; return (
data

{value.title}

{value.description}
Price: {thePrice} Duration:{" "} {" "} {value.timeline_days} day(s)
); }; const NoJobsRow = ({ text }) => ( {text} ); const MyJobListTable = () => (
<> {MyJobList?.data?.result_list?.length > 0 ? ( filteredCurrentJobList.length > 0 ? ( filteredCurrentJobList.map((value, index) => ( )) ) : ( ) ) : ( )}
{/* PAGINATION BUTTON */} = currentJobList?.length ? true : false } data={currentJobList} start={indexOfFirstItem} stop={indexOfLastItem} /> {/* END OF PAGINATION BUTTON */}
); return { MyJobListHeader, MyJobListTable }; }