Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Paribesh01 committed Oct 14, 2024
2 parents 7015ff5 + 17651c4 commit 95fad42
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
6 changes: 5 additions & 1 deletion src/actions/job.action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const getRecommendedJobs = withServerActionAsyncCatcher<
where: {
category: category,
id: { not: id },
isVerifiedJob: true,
expired: false,
},
orderBy: {
Expand Down Expand Up @@ -295,7 +296,10 @@ export const getCityFilters = async () => {
export const getRecentJobs = async () => {
try {
const recentJobs = await prisma.job.findMany({
where: { expired: false },
where: {
isVerifiedJob: true,
expired: false,
},
orderBy: {
postedAt: 'desc',
},
Expand Down
17 changes: 17 additions & 0 deletions src/hooks/useFilterCheck.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { useMemo } from 'react';
import { JobQuerySchemaType } from '@/lib/validators/jobs.validator';

export const useFilterCheck = (formValues: JobQuerySchemaType) => {
const isAnyFilterSelected = useMemo(() => {
return ['workmode', 'EmpType', 'salaryrange', 'city'].some((key) => {
const value = formValues[key as keyof JobQuerySchemaType];
if (Array.isArray(value)) {
return value.length > 0;
}
// For non-array values, check if they're truthy
return !!value;
});
}, [formValues]);

return isAnyFilterSelected;
};
24 changes: 17 additions & 7 deletions src/layouts/job-filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ import _ from 'lodash';
import { DEFAULT_PAGE } from '@/config/app.config';
import { getCityFilters } from '@/actions/job.action';
import { X } from 'lucide-react';

import { useFilterCheck } from '@/hooks/useFilterCheck';
import { AnimatePresence, motion } from 'framer-motion';
const JobFilters = ({ searchParams }: { searchParams: JobQuerySchemaType }) => {
const [cityFilters, setCityFilters] = useState<string[]>([]);

Expand All @@ -47,6 +48,7 @@ const JobFilters = ({ searchParams }: { searchParams: JobQuerySchemaType }) => {
});

const formValues = form.watch();
const isAnyFilterSelected = useFilterCheck(formValues);

async function fetchCityFilters() {
const cities = await getCityFilters();
Expand Down Expand Up @@ -82,12 +84,20 @@ const JobFilters = ({ searchParams }: { searchParams: JobQuerySchemaType }) => {
>
<div className="flex items-center justify-between mb-6">
<h3 className="font-bold text-xl">All Filters</h3>
<button
className="font-medium text-gray-600 dark:text-gray-300"
onClick={clearFilters}
>
Clear Filters
</button>
<AnimatePresence>
{isAnyFilterSelected && (
<motion.button
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
className="font-medium text-gray-600 dark:text-gray-300"
onClick={clearFilters}
>
Clear Filters
</motion.button>
)}
</AnimatePresence>
</div>
<Form {...form}>
<form className=" flex flex-col gap-3 mt-6">
Expand Down

0 comments on commit 95fad42

Please sign in to comment.