All files / src/components/Pagination Pagination.tsx

100% Statements 12/12
100% Branches 9/9
100% Functions 2/2
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48                    3x 9x   9x 4x 4x 3x 2x 1x     9x                                                    
import Button from 'common-components/Button';
import styles from './Pagination.module.scss';
 
type Props = {
    pagination: Pagination;
    page: number;
    isPreviousData: boolean;
    onPaginate: (page: number) => void;
}
 
const Pagination = ({ pagination, page, isPreviousData, onPaginate }: Props) => {
    const { pages, urls } = pagination;
 
    const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
        const type = e.currentTarget.textContent;
        if (type === "<") onPaginate(Math.max(page - 1, 0));
        else if (type === ">" && !isPreviousData && urls.next) onPaginate(page + 1);
        else if (type === "First") onPaginate(1);
        else onPaginate(pages);
    }
 
    return (
        <section className={styles.panel} aria-label="Page Navigation">
            <Button onClick={handleClick} disabled={!urls?.first} tabIndex={0} data-testid="first">First</Button>
            <div className={styles.panel__prevnext}>
                <Button
                    data-testid="prev"
                    onClick={handleClick}
                    disabled={!urls?.prev}
                    tabIndex={0}
                    title="Previous Page"
                >{"<"}</Button>
                <span>{page} / {pages}</span>
                <Button
                    data-testid="next"
                    onClick={handleClick}
                    disabled={!urls.next}
                    tabIndex={0}
                    title="Next Page"
                >{">"}</Button>
            </div>
            <Button onClick={handleClick} disabled={!urls?.next} tabIndex={0} data-testid="last">Last</Button>
        </section>
    )
}
 
export default Pagination