Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

Add pagination

Khuram Niaz03286cdf42ed
diff --git a/src/actions/job.actions.ts b/src/actions/job.actions.ts
index 05c9e3f..2f6c95b 100644
--- a/src/actions/job.actions.ts
+++ b/src/actions/job.actions.ts
@@ -59,21 +59,42 @@ export const getJobLocationList = async (): Promise<any | undefined> => {
}
};
-export const getJobsList = async (): Promise<any | undefined> => {
+export const getJobsList = async (
+ page = 1,
+ limit = 10
+): Promise<any | undefined> => {
try {
- const list = await prisma.job.findMany({
- include: {
- JobSource: true,
- JobTitle: true,
- Company: true,
- Status: true,
- Location: true,
- },
- orderBy: {
- createdAt: "desc",
- },
- });
- return list;
+ const user = await getCurrentUser();
+
+ if (!user) {
+ throw new Error("Not authenticated");
+ }
+ const skip = (page - 1) * limit;
+ const [data, total] = await Promise.all([
+ prisma.job.findMany({
+ where: {
+ userId: user.id,
+ },
+ skip,
+ take: limit,
+ include: {
+ JobSource: true,
+ JobTitle: true,
+ Company: true,
+ Status: true,
+ Location: true,
+ },
+ orderBy: {
+ createdAt: "desc",
+ },
+ }),
+ prisma.job.count({
+ where: {
+ userId: user.id,
+ },
+ }),
+ ]);
+ return { data, total };
} catch (error) {
const msg = "Failed to fetch jobs list. ";
console.error(msg, error);
diff --git a/src/app/dashboard/myjobs/page.tsx b/src/app/dashboard/myjobs/page.tsx
index 2b37a4a..c1fb972 100644
--- a/src/app/dashboard/myjobs/page.tsx
+++ b/src/app/dashboard/myjobs/page.tsx
@@ -33,8 +33,6 @@ export const metadata: Metadata = {
};
async function MyJobs() {
- const jobsList = await getJobsList();
- console.log("JOB OBJECT: ", jobsList[5]);
return (
<div className="col-span-3">
<Card x-chunk="dashboard-06-chunk-0">
@@ -87,13 +85,9 @@ async function MyJobs() {
</div>
</CardHeader>
<CardContent>
- <MyJobsTable jobs={jobsList} />
+ <MyJobsTable />
</CardContent>
- <CardFooter>
- <div className="text-xs text-muted-foreground">
- Showing <strong>1-10</strong> of <strong>32</strong> products
- </div>
- </CardFooter>
+ <CardFooter></CardFooter>
</Card>
</div>
);
diff --git a/src/components/MyJobsTable.tsx b/src/components/MyJobsTable.tsx
index d3beab2..2159174 100644
--- a/src/components/MyJobsTable.tsx
+++ b/src/components/MyJobsTable.tsx
@@ -19,7 +19,6 @@ import {
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
- DropdownMenuShortcut,
DropdownMenuSub,
DropdownMenuSubContent,
DropdownMenuSubTrigger,
@@ -34,8 +33,10 @@ import {
CommandItem,
CommandList,
} from "./ui/command";
-import { useState } from "react";
-function MyJobsTable({ jobs }: { jobs: any[] }) {
+import { useEffect, useState } from "react";
+import { TablePagination } from "./TablePagination";
+import { getJobsList } from "@/actions/job.actions";
+function MyJobsTable() {
const labels = [
"draft",
"applied",
@@ -47,124 +48,158 @@ function MyJobsTable({ jobs }: { jobs: any[] }) {
];
const [label, setLabel] = useState("feature");
const [open, setOpen] = useState(false);
+
+ const [jobs, setJobs] = useState([]);
+ const [totalJobs, setTotalJobs] = useState(0);
+ const [currentPage, setCurrentPage] = useState(1);
+ const jobsPerPage = 10;
+
+ useEffect(() => {
+ const loadJobs = async () => {
+ const { data, total } = await getJobsList(currentPage, jobsPerPage);
+
+ setJobs(data);
+ setTotalJobs(total);
+ };
+ loadJobs();
+ }, [currentPage]);
+ const totalPages = Math.ceil(totalJobs / jobsPerPage);
+ const startPostIndex = (currentPage - 1) * jobsPerPage + 1;
+ const endPostIndex = Math.min(currentPage * jobsPerPage, totalJobs);
+
return (
- <Table>
- <TableHeader>
- <TableRow>
- <TableHead className="hidden w-[100px] sm:table-cell">
- <span className="sr-only">Company Logo</span>
- </TableHead>
- <TableHead>Title</TableHead>
- <TableHead>Company</TableHead>
- <TableHead>Status</TableHead>
- <TableHead className="hidden md:table-cell">Source</TableHead>
- <TableHead className="hidden md:table-cell">Location</TableHead>
- <TableHead className="hidden md:table-cell">Date Applied</TableHead>
- <TableHead>
- <span className="sr-only">Actions</span>
- </TableHead>
- </TableRow>
- </TableHeader>
- <TableBody>
- {jobs.map((job: any) => {
- return (
- <TableRow key={job.id}>
- <TableCell className="hidden sm:table-cell">
- <Image
- alt="Company logo"
- className="aspect-square rounded-md object-cover"
- height="32"
- src="/icons/amazon-logo.svg"
- width="32"
- />
- </TableCell>
- <TableCell className="font-medium">
- {job.JobTitle?.label}
- </TableCell>
- <TableCell className="font-medium">
- {job.Company?.label}
- </TableCell>
- <TableCell>
- <Badge
- className={cn(
- "w-[70px] justify-center",
- job.Status?.value === "applied" && "bg-cyan-500",
- job.Status?.value === "interview" && "bg-green-500"
- )}
- >
- {job.Status?.label}
- </Badge>
- </TableCell>
- <TableCell className="hidden md:table-cell">
- {job.JobSource?.label}
- </TableCell>
- <TableCell className="hidden md:table-cell">
- {job.Location?.label}
- </TableCell>
- <TableCell className="hidden md:table-cell">
- {format(new Date(job.appliedDate), "PP")}
- </TableCell>
- <TableCell>
- <DropdownMenu>
- <DropdownMenuTrigger asChild>
- <Button aria-haspopup="true" size="icon" variant="ghost">
- <MoreHorizontal className="h-4 w-4" />
- <span className="sr-only">Toggle menu</span>
- </Button>
- </DropdownMenuTrigger>
- <DropdownMenuContent align="end" className="w-[200px]">
- <DropdownMenuLabel>Actions</DropdownMenuLabel>
- <DropdownMenuGroup>
- <DropdownMenuItem>
- <Pencil className="mr-2 h-4 w-4" />
- Edit
- </DropdownMenuItem>
- <DropdownMenuSeparator />
- <DropdownMenuSub>
- <DropdownMenuSubTrigger>
- <Tags className="mr-2 h-4 w-4" />
- Change status
- </DropdownMenuSubTrigger>
- <DropdownMenuSubContent className="p-0">
- <Command>
- <CommandInput
- placeholder="Filter label..."
- autoFocus={true}
- />
- <CommandList>
- <CommandEmpty>No label found.</CommandEmpty>
- <CommandGroup>
- {labels.map((label) => (
- <CommandItem
- key={label}
- value={label}
- onSelect={(value) => {
- setLabel(value);
- setOpen(false);
- }}
- >
- {label}
- </CommandItem>
- ))}
- </CommandGroup>
- </CommandList>
- </Command>
- </DropdownMenuSubContent>
- </DropdownMenuSub>
- <DropdownMenuSeparator />
- <DropdownMenuItem className="text-red-600">
- <Trash className="mr-2 h-4 w-4" />
- Delete
- </DropdownMenuItem>
- </DropdownMenuGroup>
- </DropdownMenuContent>
- </DropdownMenu>
- </TableCell>
- </TableRow>
- );
- })}
- </TableBody>
- </Table>
+ <>
+ <Table>
+ <TableHeader>
+ <TableRow>
+ <TableHead className="hidden w-[100px] sm:table-cell">
+ <span className="sr-only">Company Logo</span>
+ </TableHead>
+ <TableHead>Title</TableHead>
+ <TableHead>Company</TableHead>
+ <TableHead>Status</TableHead>
+ <TableHead className="hidden md:table-cell">Source</TableHead>
+ <TableHead className="hidden md:table-cell">Location</TableHead>
+ <TableHead className="hidden md:table-cell">Date Applied</TableHead>
+ <TableHead>
+ <span className="sr-only">Actions</span>
+ </TableHead>
+ </TableRow>
+ </TableHeader>
+ <TableBody>
+ {jobs.map((job: any) => {
+ return (
+ <TableRow key={job.id}>
+ <TableCell className="hidden sm:table-cell">
+ <Image
+ alt="Company logo"
+ className="aspect-square rounded-md object-cover"
+ height="32"
+ src="/icons/amazon-logo.svg"
+ width="32"
+ />
+ </TableCell>
+ <TableCell className="font-medium">
+ {job.JobTitle?.label}
+ </TableCell>
+ <TableCell className="font-medium">
+ {job.Company?.label}
+ </TableCell>
+ <TableCell>
+ <Badge
+ className={cn(
+ "w-[70px] justify-center",
+ job.Status?.value === "applied" && "bg-cyan-500",
+ job.Status?.value === "interview" && "bg-green-500"
+ )}
+ >
+ {job.Status?.label}
+ </Badge>
+ </TableCell>
+ <TableCell className="hidden md:table-cell">
+ {job.JobSource?.label}
+ </TableCell>
+ <TableCell className="hidden md:table-cell">
+ {job.Location?.label}
+ </TableCell>
+ <TableCell className="hidden md:table-cell">
+ {format(new Date(job.appliedDate), "PP")}
+ </TableCell>
+ <TableCell>
+ <DropdownMenu>
+ <DropdownMenuTrigger asChild>
+ <Button aria-haspopup="true" size="icon" variant="ghost">
+ <MoreHorizontal className="h-4 w-4" />
+ <span className="sr-only">Toggle menu</span>
+ </Button>
+ </DropdownMenuTrigger>
+ <DropdownMenuContent align="end" className="w-[200px]">
+ <DropdownMenuLabel>Actions</DropdownMenuLabel>
+ <DropdownMenuGroup>
+ <DropdownMenuItem>
+ <Pencil className="mr-2 h-4 w-4" />
+ Edit
+ </DropdownMenuItem>
+ <DropdownMenuSeparator />
+ <DropdownMenuSub>
+ <DropdownMenuSubTrigger>
+ <Tags className="mr-2 h-4 w-4" />
+ Change status
+ </DropdownMenuSubTrigger>
+ <DropdownMenuSubContent className="p-0">
+ <Command>
+ <CommandInput
+ placeholder="Filter label..."
+ autoFocus={true}
+ />
+ <CommandList>
+ <CommandEmpty>No label found.</CommandEmpty>
+ <CommandGroup>
+ {labels.map((label) => (
+ <CommandItem
+ key={label}
+ value={label}
+ onSelect={(value) => {
+ setLabel(value);
+ setOpen(false);
+ }}
+ >
+ {label}
+ </CommandItem>
+ ))}
+ </CommandGroup>
+ </CommandList>
+ </Command>
+ </DropdownMenuSubContent>
+ </DropdownMenuSub>
+ <DropdownMenuSeparator />
+ <DropdownMenuItem className="text-red-600">
+ <Trash className="mr-2 h-4 w-4" />
+ Delete
+ </DropdownMenuItem>
+ </DropdownMenuGroup>
+ </DropdownMenuContent>
+ </DropdownMenu>
+ </TableCell>
+ </TableRow>
+ );
+ })}
+ </TableBody>
+ </Table>
+ <div className="text-xs text-muted-foreground">
+ Showing{" "}
+ <strong>
+ {startPostIndex} to {endPostIndex}
+ </strong>{" "}
+ of
+ <strong> {totalJobs}</strong> jobs
+ </div>
+ <TablePagination
+ currentPage={currentPage}
+ totalPages={totalPages}
+ onPageChange={setCurrentPage}
+ />
+ </>
);
}
diff --git a/src/components/TablePagination.tsx b/src/components/TablePagination.tsx
new file mode 100644
index 0000000..b054fce
--- /dev/null
+++ b/src/components/TablePagination.tsx
@@ -0,0 +1,74 @@
+import {
+ Pagination,
+ PaginationContent,
+ PaginationEllipsis,
+ PaginationItem,
+ PaginationLink,
+ PaginationNext,
+ PaginationPrevious,
+} from "@/components/ui/pagination";
+import { cn } from "@/lib/utils";
+
+type PaginationProps = {
+ currentPage: number;
+ totalPages: number;
+ onPageChange: (n: number) => void;
+};
+
+export function TablePagination({
+ currentPage,
+ totalPages,
+ onPageChange,
+}: PaginationProps) {
+ const pages = Array.from({ length: totalPages }, (_, i) => i + 1);
+ const nextDisabled = currentPage === totalPages;
+ const prevDisabled = currentPage === 1;
+
+ return (
+ <Pagination>
+ <PaginationContent>
+ <PaginationItem>
+ <PaginationPrevious
+ onClick={() => {
+ if (!prevDisabled) {
+ onPageChange(currentPage - 1);
+ }
+ }}
+ className={cn(
+ "cursor-pointer",
+ prevDisabled && "cursor-default text-muted-foreground"
+ )}
+ />
+ </PaginationItem>
+ {pages.map((page) => {
+ return (
+ <PaginationItem key={page}>
+ <PaginationLink
+ onClick={() => onPageChange(page)}
+ className={page === currentPage ? "active" : ""}
+ >
+ {page}
+ </PaginationLink>
+ </PaginationItem>
+ );
+ })}
+ {/* <PaginationItem>
+ <PaginationEllipsis />
+ </PaginationItem> */}
+ <PaginationItem>
+ <PaginationNext
+ onClick={() => {
+ if (!nextDisabled) {
+ onPageChange(currentPage + 1);
+ }
+ }}
+ className={cn(
+ "cursor-pointer",
+ nextDisabled && "cursor-default text-muted-foreground"
+ )}
+ />
+ </PaginationItem>
+ </PaginationContent>
+ </Pagination>
+ );
+}
diff --git a/src/components/ui/pagination.tsx b/src/components/ui/pagination.tsx
new file mode 100644
index 0000000..ea40d19
--- /dev/null
+++ b/src/components/ui/pagination.tsx
@@ -0,0 +1,117 @@
+import * as React from "react"
+import { ChevronLeft, ChevronRight, MoreHorizontal } from "lucide-react"
+
+import { cn } from "@/lib/utils"
+import { ButtonProps, buttonVariants } from "@/components/ui/button"
+
+const Pagination = ({ className, ...props }: React.ComponentProps<"nav">) => (
+ <nav
+ role="navigation"
+ aria-label="pagination"
+ className={cn("mx-auto flex w-full justify-center", className)}
+ {...props}
+ />
+)
+Pagination.displayName = "Pagination"
+
+const PaginationContent = React.forwardRef<
+ HTMLUListElement,
+ React.ComponentProps<"ul">
+>(({ className, ...props }, ref) => (
+ <ul
+ ref={ref}
+ className={cn("flex flex-row items-center gap-1", className)}
+ {...props}
+ />
+))
+PaginationContent.displayName = "PaginationContent"
+
+const PaginationItem = React.forwardRef<
+ HTMLLIElement,
+ React.ComponentProps<"li">
+>(({ className, ...props }, ref) => (
+ <li ref={ref} className={cn("", className)} {...props} />
+))
+PaginationItem.displayName = "PaginationItem"
+
+type PaginationLinkProps = {
+ isActive?: boolean
+} & Pick<ButtonProps, "size"> &
+ React.ComponentProps<"a">
+
+const PaginationLink = ({
+ className,
+ isActive,
+ size = "icon",
+ ...props
+}: PaginationLinkProps) => (
+ <a
+ aria-current={isActive ? "page" : undefined}
+ className={cn(
+ buttonVariants({
+ variant: isActive ? "outline" : "ghost",
+ size,
+ }),
+ className
+ )}
+ {...props}
+ />
+)
+PaginationLink.displayName = "PaginationLink"
+
+const PaginationPrevious = ({
+ className,
+ ...props
+}: React.ComponentProps<typeof PaginationLink>) => (
+ <PaginationLink
+ aria-label="Go to previous page"
+ size="default"
+ className={cn("gap-1 pl-2.5", className)}
+ {...props}
+ >
+ <ChevronLeft className="h-4 w-4" />
+ <span>Previous</span>
+ </PaginationLink>
+)
+PaginationPrevious.displayName = "PaginationPrevious"
+
+const PaginationNext = ({
+ className,
+ ...props
+}: React.ComponentProps<typeof PaginationLink>) => (
+ <PaginationLink
+ aria-label="Go to next page"
+ size="default"
+ className={cn("gap-1 pr-2.5", className)}
+ {...props}
+ >
+ <span>Next</span>
+ <ChevronRight className="h-4 w-4" />
+ </PaginationLink>
+)
+PaginationNext.displayName = "PaginationNext"
+
+const PaginationEllipsis = ({
+ className,
+ ...props
+}: React.ComponentProps<"span">) => (
+ <span
+ aria-hidden
+ className={cn("flex h-9 w-9 items-center justify-center", className)}
+ {...props}
+ >
+ <MoreHorizontal className="h-4 w-4" />
+ <span className="sr-only">More pages</span>
+ </span>
+)
+PaginationEllipsis.displayName = "PaginationEllipsis"
+
+export {
+ Pagination,
+ PaginationContent,
+ PaginationEllipsis,
+ PaginationItem,
+ PaginationLink,
+ PaginationNext,
+ PaginationPrevious,
+}