Skip to content

Fixed Pagination-Changed filteredTracks to visibleTracks #722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions apps/web/components/Tracks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
const [filteredTracks, setFilteredTracks] = useState<TrackPros[]>(tracks);
const [visibleTracks, setVisibleTracks] = useState<TrackPros[]>([]);
const [sortBy, setSortBy] = useState<string>("new");
const [isSelectOpen, setIsSelectOpen] = useState<boolean>(false);
//const [isSelectOpen, setIsSelectOpen] = useState<boolean>(false);
const [currentPage, setCurrentPage] = useState<number>(1);
const [loading, setLoading] = useState<boolean>(true);
const [selectedCohort, setSelectedCohort] = useState<number | null>(null);
Expand Down Expand Up @@ -87,6 +87,8 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
} else if (sortBy === "old") {
sortedTracks.sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime());
}
//console.log(sortedTracks);

setFilteredTracks(sortedTracks);
setLoading(false);
};
Expand All @@ -106,7 +108,10 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
useEffect(() => {
setLoading(true);
const start = (currentPage - 1) * tracksPerPage;
//console.log("start", start);

const end = start + tracksPerPage;
//console.log("end", end);
setVisibleTracks(filteredTracks.slice(start, end));
setLoading(false);
}, [currentPage, filteredTracks]);
Expand Down Expand Up @@ -206,7 +211,7 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
☹️ Sorry - currently there are no tracks available.
</p>
) : (
filteredTracks.map((t) => (
visibleTracks.map((t) => (
<motion.li key={t.id} className="w-full" variants={{ hidden: { opacity: 0 }, show: { opacity: 1 } }}>
<TrackCard2 track={t} />
</motion.li>
Expand All @@ -230,14 +235,14 @@ export const Tracks = ({ tracks, categories }: TracksWithCategoriesProps) => {
<PaginationContent>
<PaginationItem className="cursor-pointer">
<PaginationPrevious
onClick={() => {
onClick={() => {
setCurrentPage((prev) => Math.max(prev - 1, 1));
}}
/>
</PaginationItem>
{Array.from({ length: totalPages }).map((_, index) => (
<PaginationItem key={index} className="cursor-pointer">
<PaginationLink onClick={() => setCurrentPage(index + 1)}>{index + 1}</PaginationLink>
<PaginationLink onClick={() => setCurrentPage(index + 1)} isActive={index + 1 === currentPage}>{index + 1}</PaginationLink>
</PaginationItem>
))}
{totalPages > 5 && (
Expand Down