Skip to content

Commit

Permalink
Added pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
nazimboudeffa committed Dec 12, 2024
1 parent 444fe12 commit 2406512
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 32 deletions.
38 changes: 24 additions & 14 deletions src/app/api/jobs/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,31 @@ import { NextResponse, NextRequest } from "next/server";
connect();

export async function POST(request: NextRequest) {
try {

try {
const body = await request.json();
console.log(body);

if (body) {
const jobs = await Job.find({ ...body }).sort({ createdAt: -1 });
return NextResponse.json({ jobs }, { status: 200 });
} else {
const jobs = await Job.find({}).sort({ createdAt: -1 });
return NextResponse.json({ jobs }, { status: 200 });
}
const { page = 1, jobsPerPage = 10, ...filters } = body;

// Calculer les valeurs pour la pagination
const skip = (page - 1) * jobsPerPage;

// Rechercher les jobs avec pagination
const jobs = await Job.find({ ...filters })
.sort({ createdAt: -1 })
.skip(skip)
.limit(jobsPerPage);

// Obtenir le nombre total de jobs correspondant aux filtres
const totalJobs = await Job.countDocuments({ ...filters });

} catch (error) {
console.log(error);
return NextResponse.json({ message: "Error", error }, { status: 500 });
}
}
// Retourner les résultats avec les métadonnées de pagination
return NextResponse.json(
{ jobs, total: totalJobs },
{ status: 200 }
);
} catch (error) {
console.log(error);
return NextResponse.json({ message: "Error", error }, { status: 500 });
}
}
5 changes: 3 additions & 2 deletions src/app/job-submitted/page.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
export default function Page() {
return (
<main className="m-auto my-10 max-w-5xl space-y-5 px-3 text-center">
<h1>Job submitted</h1>
<p>Your job posting has been submitted and is pending approval.</p>
<h1 className="font-bold">Job submitted</h1>
<p>Your job posting has been submitted.</p>
<p>Please, contact me if you want to remove it.</p>
</main>
);
}
5 changes: 2 additions & 3 deletions src/app/jobs/new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import { CreateJobValues, createJobSchema } from "@/lib/validations";
import { zodResolver } from "@hookform/resolvers/zod";
import { useForm } from "react-hook-form";
import { createJobPosting } from "./actions";
import { Toaster, toast } from 'sonner'
import { redirect } from "next/navigation";

export default function NewJobForm() {
const form = useForm<CreateJobValues>({
Expand Down Expand Up @@ -52,7 +52,7 @@ export default function NewJobForm() {
} catch (error) {
console.error(error);
} finally {
toast.success("Job posting created successfully");
redirect("/job-submitted")
}
}

Expand All @@ -64,7 +64,6 @@ export default function NewJobForm() {
Get your job posting seen by thousands of job seekers.
</p>
</div>
<Toaster />
<div className="space-y-6 rounded-lg border p-4">
<Form {...form}>
<form
Expand Down
6 changes: 3 additions & 3 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ function getTitle({ title, type }: JobFilterValues) {
if (title) {
titlePrefix = `${title}`;
} else if (type) {
titlePrefix = `${type} developer`;
titlePrefix = `${type} freelance`;
} else {
titlePrefix = "All developer";
titlePrefix = "All freelance";
}

const titleSuffix = " jobs";
Expand All @@ -36,7 +36,7 @@ export default function Home() {
<main className="m-auto my-10 max-w-5xl space-y-10 px-3">
<div className="space-y-5 text-center">
<h1 className="text-4xl font-extrabold tracking-tight lg:text-5xl">{getTitle(filterValues)}</h1>
<p className="text-muted-foreground">Find your dream job.</p>
<p className="text-muted-foreground">Find your dream mission.</p>
</div>
<section className="flex flex-col gap-4 md:flex-row">
<JobFilterSidebar defaultValues={filterValues} />
Expand Down
8 changes: 5 additions & 3 deletions src/app/privacy-policy/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export default function PP() {
return (
<div>
<h1>Page</h1>
</div>
<main className="m-auto my-10 max-w-5xl space-y-5 px-3 text-center">
<h1 className="font-bold">Privacy Policy</h1>
<p>Your privacy is important for us.</p>
<p>Consider that we do not collect informations from you.</p>
</main>
)
}
8 changes: 5 additions & 3 deletions src/app/tos/page.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
export default function TOS() {
return (
<div>
<h1>Page</h1>
</div>
<main className="m-auto my-10 max-w-5xl space-y-5 px-3 text-center">
<h1 className="font-bold">Term of Service</h1>
<p>This software is not garanteed.</p>
<p>The data may disapear from a day to another.</p>
</main>
)
}
2 changes: 1 addition & 1 deletion src/components/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function Footer() {
</p>
</div>
<div className="flex flex-wrap gap-5 text-sm text-muted-foreground">
<Link href="https://www.linkedin.com/in/nazimboudeffa" className="hover:underline">
<Link href="https://www.linkedin.com/in/nazimboudeffa" target="_blank" className="hover:underline">
Contact
</Link>
<Link href="/tos" className="hover:underline">
Expand Down
8 changes: 5 additions & 3 deletions src/components/JobResults.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,22 @@ export default function JobResults({
}: Readonly<JobResultsProps>) {

const [jobs, setJobs] = useState<Job[]>([]);
const [totalJobs, setTotalJobs] = useState(0);

useEffect(() => {
fetch("/api/jobs", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...filterValues }),
body: JSON.stringify({ ...filterValues, page, jobsPerPage }),
})
.then((res) => res.json())
.then((data) => {
console.log(data);
setJobs(data.jobs);
setTotalJobs(data.total);
})
.catch((err) => console.log(err));
}, [filterValues]);
}, [filterValues, page]);

return (
<div className="grow space-y-4">
Expand All @@ -72,7 +74,7 @@ export default function JobResults({
{jobs?.length > 0 && (
<Pagination
currentPage={page}
totalPages={Math.ceil(jobs?.length / jobsPerPage)}
totalPages={Math.ceil(totalJobs / jobsPerPage)}
filterValues={filterValues}
/>
)}
Expand Down

0 comments on commit 2406512

Please sign in to comment.