All files / src/ahuora-design-system/ui paginator.tsx

80% Statements 12/15
92.85% Branches 13/14
75% Functions 3/4
78.57% Lines 11/14

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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69            1731x 1731x 1731x               3x 3x       7x 7x 6x   1x           1731x   978x                                                                      
import { ChevronLeft, ChevronRight } from "lucide-react";
import { Button } from "./button"
import DebouncedInput from "./debounced-input";
import { toast } from "sonner";
 
export default function Paginator({ page, setPage, data, minimal }) {
    const next = data?.next;
    const previous = data?.previous;
    const totalPages = data?.pages
 
    function handlePrevious() {
        const pageNumber = new URLSearchParams(previous).get("page");
        handleSetPage(pageNumber);
    }
 
    function handleNext() {
        const pageNumber = new URLSearchParams(next).get("page");
        handleSetPage(pageNumber);
    }
 
    function handleSetPage(value: number | string | null) {
        if (value) {
            if (+value <= totalPages && +value >= 1) {
                setPage(+value);
            } else {
                toast.error('Invalid page number');
            }
        } else E{
            setPage(1);
        }
    }
    if (minimal && !next && !previous) return null;
 
    return (
        <div className="flex justify-between items-center" aria-label="paginator-main-container">
            {!minimal && <p className="flex-1">Showing {data?.results?.length} of {data?.count} results</p>}
            <div className="flex gap-2 items-center" aria-label="paginator-btn-container">
                <Button
                    variant="outline"
                    size="icon"
                    onClick={handlePrevious}
                    disabled={!previous}
                    className="h-6 w-6"
                >
                    <ChevronLeft />
                </Button>
                <small>Page {page} / {totalPages}</small>
                <Button
                    variant="outline"
                    size="icon"
                    onClick={handleNext}
                    disabled={!next}
                    className="h-6 w-6"
                >
                    <ChevronRight />
                </Button>
 
            </div>
 
            {!minimal && <div className="flex flex-1 justify-end gap-2 items-center" aria-label="paginator-goto-input">
                <p>Go to page:</p>
                <div>
                    <DebouncedInput value={page} onUpdate={handleSetPage} type="number" className="border rounded w-16 h-7" />
                </div>
            </div>}
        </div>
    )
}