import {useDispatch, useSelector} from "react-redux"; import {useEffect, useState} from "react"; import {useParams} from "react-router"; import {ApplicationActions} from "../../redux/actions/applicationActions"; import {ApplicationDetailsModal} from "./ApplicationDetailsModal"; import "./Applications.css" import Select from "react-select"; import {sortElementsBy} from "../../utils/utils"; import {JobSeekerActions} from "../../redux/actions/JobSeekerActions"; import {notifyAppStatusUpdate} from "../../utils/toastUtils"; export const ApplicationsByJobAd = () => { const dispatch = useDispatch(); let {advertisement_id} = useParams(); const [applicationsByJobAd, setApplicationsByJobAd] = useState([]); let applicationsByJobAdState = useSelector(state => state.appl.applicationsByJobAdId) const [dispatched, setDispatched] = useState(false); const [profilePics, setProfilePics] = useState({}); let profilePicsState = useSelector(state => state.images.profilePictures) const [profilePicsDispatched, setProfilePicsDispatched] = useState(false); const [jobAdTitle, setJobAdTitle] = useState(""); const [changedApplications, setChangedApplications] = useState({}); useEffect(() => { if(!dispatched) { dispatch(ApplicationActions.fetchApplicationsByJobAdId(advertisement_id, (success, reponse) => { if (success && reponse.data.length > 0) { setApplicationsByJobAd(sortElementsBy(reponse.data, "submittedOn")) setJobAdTitle(reponse.data[0].jobAdTitle) } setDispatched(true) console.log("Fetch applications by job ad GET") })) } else { setApplicationsByJobAd(sortElementsBy(applicationsByJobAdState, "submittedOn")); if(applicationsByJobAdState.length > 0) { setJobAdTitle(applicationsByJobAdState[0].jobAdTitle) } } }, [applicationsByJobAdState]) useEffect(() => { if(dispatched && !profilePicsDispatched) { applicationsByJobAd.forEach(app => { if(app.jobSeekerId && !profilePics[app.jobSeekerId]) { fetchProfilePic(app.jobSeekerId) } }) setProfilePicsDispatched(true); console.log("Fetch all profile pics GET") } else if(profilePicsDispatched) { setProfilePics(profilePicsState) console.log("Fetch all profile pics STATE") } }, [dispatched]) const fetchProfilePic = (jobSeekerId) => { dispatch(JobSeekerActions.downloadProfilePic(jobSeekerId, (success, response) => { if(success) { setProfilePics(prevState => ({...prevState, [jobSeekerId]: response})) } })) } const options = [ {value: 'PROPOSED', label: Proposed}, {value: 'UNDER_REVIEW', label: Under Review}, {value: 'ACCEPTED', label: Accepted}, {value: 'DENIED', label: Denied} ]; const [selectedFilter, setSelectedFilter] = useState('All'); const filters = [ { value: 'ALL', label: 'All', icon: 'fa-folder-open' }, { value: 'PROPOSED', label: 'Proposed', icon: 'fa-paper-plane' }, { value: 'UNDER_REVIEW', label: 'Under Review', icon: 'fa-file-pen' }, { value: 'ACCEPTED', label: 'Accepted', icon: 'fa-user-check' }, { value: 'DENIED', label: 'Denied', icon: 'fa-user-slash' }, ]; const filterApplicationsByJobAdvertisement = (filter) => { dispatch(ApplicationActions.filterApplicationsByJobAdId(advertisement_id, filter, (success, response) => { if(success) { //notify } })) } let handleDefaultStatus = (status) => { return options.find(option => option.value === status); } let handleStatusChange = (selectedOption, id) => { const currentApplication = applicationsByJobAd.find(app => app.id === id); setChangedApplications(prevState => ({ ...prevState, [id]: { ...prevState[id], response: (selectedOption.value === "ACCEPTED" || selectedOption.value === "DENIED") ? (prevState[id]?.response || currentApplication.response || "") : "", status: selectedOption.value } })) setApplicationsByJobAd(prevState => ( prevState.map(application => application.id === id ? {...application, status: selectedOption.value} : application) )) const responseTextarea = document.getElementById(`response-${id}`); if (responseTextarea) { responseTextarea.value = ""; } /* dispatch(ApplicationActions.updateApplicationStatus(id, selectedOption.value, (success, response) => { if(success) { notifyAppStatusUpdate() } }))*/ } let handleResponseChange = (e, id) => { const currentApplication = applicationsByJobAd.find(app => app.id === id); setChangedApplications(prevState => ({ ...prevState, [id]: { ...prevState[id], response: e.target.value, status: prevState[id]?.status || currentApplication.status, } } )) } const handleSaveChanges = () => { console.log(changedApplications) const changes = Object.entries(changedApplications).map( ([applicationId, change]) => ({ id: applicationId, status: change.status, response: change.response, }) ); console.log(changes) if(changes.length === 0) { return; } dispatch(ApplicationActions.updateApplications(changes, (success, response) => { if(success) { setChangedApplications({}); notifyAppStatusUpdate() //notify change success } })) } const isChangedApplication = (id) => { return changedApplications && Object.keys(changedApplications).includes(id.toString()); }; return (
{jobAdTitle ?

Applications for {jobAdTitle}

:

}
{ filters.map(filter => ( { setSelectedFilter(filter.label) filterApplicationsByJobAdvertisement(filter.value) setChangedApplications({}); }} > {filter.label} )) }
{applicationsByJobAd && applicationsByJobAd.map((application, index) => (
Submitted on {new Date(application.submittedOn).toLocaleString('default', { day: 'numeric', month: 'long', year: 'numeric' })}
{application.jobSeekerName}
{application.jobSeekerEmail}
{application.jobSeekerPhoneNumber}