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.

Refactor resume table and job table pagination

Khuram Niaz52896f17aded
diff --git a/src/components/activities/ActivitiesContainer.tsx b/src/components/activities/ActivitiesContainer.tsx
index 470da45..d3ffacb 100644
--- a/src/components/activities/ActivitiesContainer.tsx
+++ b/src/components/activities/ActivitiesContainer.tsx
@@ -232,7 +232,7 @@ function ActivitiesContainer() {
elapsedTime={timeElapsed}
/>
)}
- {activitiesList.length && (
+ {activitiesList.length > 0 && (
<>
<ActivitiesTable
activities={activitiesList}
diff --git a/src/components/admin/CompaniesContainer.tsx b/src/components/admin/CompaniesContainer.tsx
index 9474f41..eff23f2 100644
--- a/src/components/admin/CompaniesContainer.tsx
+++ b/src/components/admin/CompaniesContainer.tsx
@@ -75,7 +75,7 @@ function CompaniesContainer() {
</CardHeader>
<CardContent>
{loading && <Loading />}
- {companies.length && (
+ {companies.length > 0 && (
<>
<CompaniesTable
companies={companies}
diff --git a/src/components/admin/JobLocationsContainer.tsx b/src/components/admin/JobLocationsContainer.tsx
index c7ebe3c..d5fc0ad 100644
--- a/src/components/admin/JobLocationsContainer.tsx
+++ b/src/components/admin/JobLocationsContainer.tsx
@@ -56,7 +56,7 @@ function JobLocationsContainer() {
</CardHeader>
<CardContent>
{loading && <Loading />}
- {locations.length && (
+ {locations.length > 0 && (
<>
<JobLocationsTable
jobLocations={locations}
diff --git a/src/components/admin/JobTitlesContainer.tsx b/src/components/admin/JobTitlesContainer.tsx
index f741e38..6d04c37 100644
--- a/src/components/admin/JobTitlesContainer.tsx
+++ b/src/components/admin/JobTitlesContainer.tsx
@@ -56,7 +56,7 @@ function JobTitlesContainer() {
</CardHeader>
<CardContent>
{loading && <Loading />}
- {titles.length && (
+ {titles.length > 0 && (
<>
<JobTitlesTable
jobTitles={titles}
diff --git a/src/components/myjobs/JobsContainer.tsx b/src/components/myjobs/JobsContainer.tsx
index 9030ed5..95b8bab 100644
--- a/src/components/myjobs/JobsContainer.tsx
+++ b/src/components/myjobs/JobsContainer.tsx
@@ -39,7 +39,6 @@ import Loading from "../Loading";
import { usePathname, useRouter, useSearchParams } from "next/navigation";
import { AddJob } from "./AddJob";
import MyJobsTable from "./MyJobsTable";
-import { Resume } from "@/models/profile.model";
type MyJobsProps = {
statuses: JobStatus[];
@@ -69,17 +68,14 @@ function JobsContainer({
[queryParams]
);
const [jobs, setJobs] = useState<JobResponse[]>([]);
- const [currentPage, setCurrentPage] = useState(
- Number(queryParams.get("page")) || 1
- );
+ const [page, setPage] = useState(1);
const [totalJobs, setTotalJobs] = useState(0);
+ const [filterKey, setFilterKey] = useState<string>();
const [editJob, setEditJob] = useState(null);
const [loading, setLoading] = useState(false);
const jobsPerPage = APP_CONSTANTS.RECORDS_PER_PAGE;
- const totalPages = Math.ceil(totalJobs / jobsPerPage);
-
const loadJobs = useCallback(
async (page: number, filter?: string) => {
setLoading(true);
@@ -88,26 +84,30 @@ function JobsContainer({
jobsPerPage,
filter
);
- if (!success) {
+ if (success && data) {
+ setJobs((prev) => (page === 1 ? data : [...prev, ...data]));
+ setTotalJobs(total);
+ setPage(page);
+ setLoading(false);
+ } else {
toast({
variant: "destructive",
title: "Error!",
description: message,
});
- return;
- }
- setJobs(data);
- setTotalJobs(total);
- if (data) {
setLoading(false);
+ return;
}
},
[jobsPerPage]
);
- const reloadJobs = () => {
- loadJobs(1);
- };
+ const reloadJobs = useCallback(async () => {
+ await loadJobs(1);
+ if (filterKey !== "none") {
+ setFilterKey("none");
+ }
+ }, [loadJobs, filterKey]);
const onDeleteJob = async (jobId: string) => {
const { res, success, message } = await deleteJobById(jobId);
@@ -162,16 +162,16 @@ function JobsContainer({
};
useEffect(() => {
- loadJobs(currentPage);
- }, [currentPage, loadJobs]);
+ (async () => await loadJobs(1))();
+ }, [loadJobs]);
const onFilterChange = (filterBy: string) => {
- filterBy === "none" ? reloadJobs() : loadJobs(1, filterBy);
- };
-
- const onPageChange = (page: number) => {
- setCurrentPage(page);
- router.push(pathname + "?" + createQueryString("page", page.toString()));
+ if (filterBy === "none") {
+ reloadJobs();
+ } else {
+ setFilterKey(filterBy);
+ loadJobs(1, filterBy);
+ }
};
return (
@@ -181,7 +181,7 @@ function JobsContainer({
<CardTitle>My Jobs</CardTitle>
<div className="flex items-center">
<div className="ml-auto flex items-center gap-2">
- <Select onValueChange={onFilterChange}>
+ <Select value={filterKey} onValueChange={onFilterChange}>
<SelectTrigger className="w-[120px] h-8">
<ListFilter className="h-3.5 w-3.5" />
<SelectValue placeholder="Filter" />
@@ -219,18 +219,38 @@ function JobsContainer({
</CardHeader>
<CardContent>
{loading && <Loading />}
- <MyJobsTable
- jobs={jobs}
- jobStatuses={statuses}
- currentPage={currentPage}
- totalPages={totalPages}
- jobsPerPage={jobsPerPage}
- totalJobs={totalJobs}
- onPageChange={onPageChange}
- deleteJob={onDeleteJob}
- editJob={onEditJob}
- onChangeJobStatus={onChangeJobStatus}
- />
+ {jobs.length > 0 && (
+ <>
+ <MyJobsTable
+ jobs={jobs}
+ jobStatuses={statuses}
+ deleteJob={onDeleteJob}
+ editJob={onEditJob}
+ onChangeJobStatus={onChangeJobStatus}
+ />
+ <div className="text-xs text-muted-foreground">
+ Showing{" "}
+ <strong>
+ {1} to {jobs.length}
+ </strong>{" "}
+ of
+ <strong> {totalJobs}</strong> jobs
+ </div>
+ </>
+ )}
+ {jobs.length < totalJobs && (
+ <div className="flex justify-center p-4">
+ <Button
+ size="sm"
+ variant="outline"
+ onClick={() => loadJobs(page + 1, filterKey)}
+ disabled={loading}
+ className="btn btn-primary"
+ >
+ {loading ? "Loading..." : "Load More"}
+ </Button>
+ </div>
+ )}
</CardContent>
<CardFooter></CardFooter>
</Card>
diff --git a/src/components/myjobs/MyJobsTable.tsx b/src/components/myjobs/MyJobsTable.tsx
index a1627f3..cd89905 100644
--- a/src/components/myjobs/MyJobsTable.tsx
+++ b/src/components/myjobs/MyJobsTable.tsx
@@ -31,9 +31,8 @@ import {
DropdownMenuSubTrigger,
DropdownMenuTrigger,
} from "../ui/dropdown-menu";
-import { Button, buttonVariants } from "../ui/button";
+import { Button } from "../ui/button";
import { useState } from "react";
-import { TablePagination } from "../TablePagination";
import { JobResponse, JobStatus } from "@/models/job.model";
import Link from "next/link";
import { useRouter } from "next/navigation";
@@ -42,11 +41,6 @@ import { DeleteAlertDialog } from "../DeleteAlertDialog";
type MyJobsTableProps = {
jobs: JobResponse[];
jobStatuses: JobStatus[];
- currentPage: number;
- totalPages: number;
- totalJobs: number;
- jobsPerPage: number;
- onPageChange: (n: number) => void;
deleteJob: (id: string) => void;
editJob: (id: string) => void;
onChangeJobStatus: (id: string, status: JobStatus) => void;
@@ -55,11 +49,6 @@ type MyJobsTableProps = {
function MyJobsTable({
jobs,
jobStatuses,
- currentPage,
- totalPages,
- totalJobs,
- jobsPerPage,
- onPageChange,
deleteJob,
editJob,
onChangeJobStatus,
@@ -67,8 +56,6 @@ function MyJobsTable({
const [alertOpen, setAlertOpen] = useState(false);
const [jobIdToDelete, setJobIdToDelete] = useState("");
- const startPostIndex = (currentPage - 1) * jobsPerPage + 1;
- const endPostIndex = Math.min(currentPage * jobsPerPage, totalJobs);
const router = useRouter();
const viewJobDetails = (jobId: string) => {
router.push(`/dashboard/myjobs/${jobId}`);
@@ -234,21 +221,6 @@ function MyJobsTable({
})}
</TableBody>
</Table>
- <div className="text-xs text-muted-foreground">
- Showing{" "}
- <strong>
- {startPostIndex} to {endPostIndex}
- </strong>{" "}
- of
- <strong> {totalJobs}</strong> jobs
- </div>
- {totalJobs > jobsPerPage && (
- <TablePagination
- currentPage={currentPage}
- totalPages={totalPages}
- onPageChange={onPageChange}
- />
- )}
<DeleteAlertDialog
pageTitle="job"
open={alertOpen}
diff --git a/src/components/profile/ProfileContainer.tsx b/src/components/profile/ProfileContainer.tsx
index 3b20c42..0fa86b3 100644
--- a/src/components/profile/ProfileContainer.tsx
+++ b/src/components/profile/ProfileContainer.tsx
@@ -4,7 +4,6 @@ import CreateResume from "./CreateResume";
import { Card, CardContent, CardHeader, CardTitle } from "../ui/card";
import { getResumeList } from "@/actions/profile.actions";
import { Resume } from "@/models/profile.model";
-import { useSearchParams } from "next/navigation";
import { APP_CONSTANTS } from "@/lib/constants";
import Loading from "../Loading";
import ResumeTable from "./ResumeTable";
@@ -13,21 +12,16 @@ import { PlusCircle } from "lucide-react";
import { Button } from "../ui/button";
function ActivitiesContainer() {
- const queryParams = useSearchParams();
const recordsPerPage = APP_CONSTANTS.RECORDS_PER_PAGE;
const [resumes, setResumes] = useState<Resume[]>([]);
const [resumeDialogOpen, setResumeDialogOpen] = useState(false);
const [resumeToEdit, setResumeToEdit] = useState<Resume | null>(null);
- const [totalResumes, setTotalResumes] = useState(0);
- const [currentPage, setCurrentPage] = useState(
- Number(queryParams.get("page")) || 1
- );
+ const [totalResumes, setTotalResumes] = useState<number>(0);
+ const [page, setPage] = useState<number>(1);
const [loading, setLoading] = useState(false);
- const totalPages = Math.ceil(totalResumes / recordsPerPage);
-
const loadResumes = useCallback(
async (page: number) => {
setLoading(true);
@@ -35,9 +29,10 @@ function ActivitiesContainer() {
page,
recordsPerPage
);
- if (success) {
- setResumes(data);
+ if (success && data) {
+ setResumes((prev) => (page === 1 ? data : [...prev, ...data]));
setTotalResumes(total);
+ setPage(page);
setLoading(false);
} else {
setLoading(false);
@@ -51,13 +46,13 @@ function ActivitiesContainer() {
[recordsPerPage]
);
- const reloadResumes = async () => {
+ const reloadResumes = useCallback(async () => {
await loadResumes(1);
- };
+ }, [loadResumes]);
useEffect(() => {
- loadResumes(currentPage);
- }, [currentPage, loadResumes]);
+ (async () => await loadResumes(1))();
+ }, [loadResumes]);
const createResume = () => {
setResumeToEdit(null);
@@ -103,16 +98,36 @@ function ActivitiesContainer() {
</CardHeader>
<CardContent>
{loading && <Loading />}
- <ResumeTable
- resumes={resumes}
- currentPage={currentPage}
- totalPages={totalPages}
- recordsPerPage={recordsPerPage}
- totalResumes={totalResumes}
- // onPageChange={onPageChange}
- editResume={onEditResume}
- reloadResumes={reloadResumes}
- />
+ {resumes.length > 0 && (
+ <>
+ <ResumeTable
+ resumes={resumes}
+ editResume={onEditResume}
+ reloadResumes={reloadResumes}
+ />
+ <div className="text-xs text-muted-foreground">
+ Showing{" "}
+ <strong>
+ {1} to {resumes.length}
+ </strong>{" "}
+ of
+ <strong> {totalResumes}</strong> resumes
+ </div>
+ </>
+ )}
+ {resumes.length < totalResumes && (
+ <div className="flex justify-center p-4">
+ <Button
+ size="sm"
+ variant="outline"
+ onClick={() => loadResumes(page + 1)}
+ disabled={loading}
+ className="btn btn-primary"
+ >
+ {loading ? "Loading..." : "Load More"}
+ </Button>
+ </div>
+ )}
</CardContent>
</Card>
);
diff --git a/src/components/profile/ResumeTable.tsx b/src/components/profile/ResumeTable.tsx
index 821c90f..3c39e6b 100644
--- a/src/components/profile/ResumeTable.tsx
+++ b/src/components/profile/ResumeTable.tsx
@@ -32,25 +32,11 @@ import { DeleteAlertDialog } from "../DeleteAlertDialog";
type ResumeTableProps = {
resumes: Resume[];
- currentPage: number;
- totalPages: number;
- totalResumes: number;
- recordsPerPage: number;
- // onPageChange: (n: number) => void;
editResume: (resume: Resume) => void;
reloadResumes: () => void;
};
-function ResumeTable({
- resumes,
- currentPage,
- totalPages,
- totalResumes,
- recordsPerPage,
- editResume,
- reloadResumes,
-}: // onPageChange,
-ResumeTableProps) {
+function ResumeTable({ resumes, editResume, reloadResumes }: ResumeTableProps) {
const [alertOpen, setAlertOpen] = useState(false);
const [resumeToDelete, setResumeToDelete] = useState<Resume>();
const onDeleteResume = useMemo(