Skip to content

Commit

Permalink
Merge pull request #393 from marleypm16/#392
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcelo8173 authored Oct 9, 2024
2 parents f88674f + d974ab4 commit 844a201
Showing 1 changed file with 28 additions and 32 deletions.
60 changes: 28 additions & 32 deletions src/hooks/useJobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ const useJobs = () => {
setLoading(false);
}
};

useEffect(() => {
getJobs();
}, []);

const getJobsQuantity = async () =>{
setLoading(true)
try{
Expand All @@ -67,48 +62,49 @@ const useJobs = () => {
}

}

useEffect(()=>{
useEffect(() => {
getJobs();
getJobsQuantity()
},[])

const filterJobs = (
jobs: JobsProps[],
}, []);

const filterJobs = async (

searchTerm: string,
location: string,
) => {
return jobs.filter((job) => {
const matchesSearchTerm = job.title
.toLowerCase()
.includes(searchTerm.toLowerCase());
const matchesLocation = job.location
.toLowerCase()
.includes(location.toLowerCase());

return matchesSearchTerm && matchesLocation;
});
};
let order = 'ASC'
if(sortOrder === "Mais Antigos"){
order = 'DESC'
}else{
order = 'ASC'
}
try{
const { data : data } = await apiJobs.get(`/job?filter=${searchTerm}&sort=${order}`)
setJobs(data.data)
} catch (error) {
console.error('Erro ao filtrar vagas:', error);
setError('Erro ao filtrar vagas');
}
};

const sortedJobs = jobs.sort((a, b) => {
const dateA = new Date(a.created_date).getTime();
const dateB = new Date(b.created_date).getTime();

return sortOrder === 'Mais Recentes' ? dateB - dateA : dateA - dateB;
});

useEffect(()=>{
filterJobs(searchTerm)
setFilteredJobsCount(jobs.length)
},[searchTerm,jobs])

const filteredJobs = filterJobs(sortedJobs, searchTerm, location);
const allJobs = jobsCount

useEffect(() => {
setFilteredJobsCount(filteredJobs.length);
}, [filteredJobs]);



const {
currentPage,
paginatedItems: currentJobs,
totalPages,
setCurrentPage,
} = usePagination<JobsProps>(filteredJobs, ITEMS_PER_PAGE);
} = usePagination<JobsProps>(jobs, ITEMS_PER_PAGE);

const containerRef = useRef<HTMLDivElement>(null);

Expand Down

0 comments on commit 844a201

Please sign in to comment.