Skip to content

Commit

Permalink
add fix pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
u4aew committed Aug 15, 2024
1 parent 95a2e6f commit 56c8177
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 11 deletions.
1 change: 0 additions & 1 deletion app/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ const PageStock = async ({ params }: Props) => {
) : (
<span>Not data</span>
)}

</div>
</div>
);
Expand Down
5 changes: 2 additions & 3 deletions components/Header/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react';
import Link from 'next/link';
import { Logo } from '@/components/Logo';
import { Profile } from '@/components/Profile';

Expand All @@ -10,9 +9,9 @@ export const Header = () => {
<div className={styles.header}>
<div className={styles.main}>
<div className={styles.logo}>
<Link href="/" aria-label="Homepage">
<a href="/" aria-label="Homepage">
<Logo />
</Link>
</a>
</div>
</div>
<div className={styles.side}>
Expand Down
42 changes: 42 additions & 0 deletions components/StocksList/Pagination/LoadMore/LoadMore.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
'use client';

import { useEffect, useRef } from 'react';
import styles from '../pagination.module.scss';
import Link from 'next/link';

interface LoadMoreProps {
currentStart: number;
currentEnd: number;
itemsPerPage: number;
}

export const LoadMore = ({
currentStart,
currentEnd,
itemsPerPage,
}: LoadMoreProps) => {
const scrollPosition = useRef<number>(0);

useEffect(() => {
if (scrollPosition.current > 0) {
window.scrollTo({
top: scrollPosition.current,
});
scrollPosition.current = 0;
}
});

const handleLoadMoreClick = () => {
scrollPosition.current = window.scrollY;
};

return (
<Link
onClick={handleLoadMoreClick}
className={styles.btn}
href={`?start=${currentStart}&end=${currentEnd + itemsPerPage}`}
>
Show more
</Link>
);
};
1 change: 1 addition & 0 deletions components/StocksList/Pagination/LoadMore/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { LoadMore as default } from './LoadMore';
14 changes: 7 additions & 7 deletions components/StocksList/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Link from 'next/link';
import classNames from 'classnames';
import { createPageArray } from '@/utils';

import dynamic from 'next/dynamic';
const LoadMore = dynamic(() => import('./LoadMore'), { ssr: false });
import styles from './pagination.module.scss';

export const Pagination = ({
Expand Down Expand Up @@ -35,12 +36,11 @@ export const Pagination = ({
return (
<div className={styles.box}>
<div className={styles.loadMore}>
<Link
className={styles.btn}
href={`?start=${currentStart}&end=${currentEnd + itemsPerPage}`}
>
Show more
</Link>
<LoadMore
currentStart={currentStart}
currentEnd={currentEnd}
itemsPerPage={itemsPerPage}
/>
</div>
<div className={styles.pagination}>
<div className={styles.prev}>
Expand Down

0 comments on commit 56c8177

Please sign in to comment.