All files / components/Commons ProfitsTable.js

100% Statements 20/20
100% Branches 8/8
100% Functions 9/9
100% Lines 20/20

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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98          136x 136x 136x   136x 136x 136x 136x 10x   136x     77x           116x 116x                 77x             136x                   136x           136x                   2x             3x             3x             2x                  
import React from "react";
import OurTable from "main/components/OurTable";
 
export default function ProfitsTable({ profits }) {
 
    const PROFIT_PAGE_SIZE = 5;
    const [currentPage, setCurrentPage] = React.useState(0);
    const totalPageCount = Math.ceil(profits.length / PROFIT_PAGE_SIZE);
    
    const indexOfLastRow = (currentPage + 1) * PROFIT_PAGE_SIZE;
    const indexOfFirstRow = indexOfLastRow - PROFIT_PAGE_SIZE;
    const currentRows = profits.slice(indexOfFirstRow, indexOfLastRow);
    const handlePageChange = (newPage) => {
        setCurrentPage(newPage);
    };
    const columns = [
        {
            Header: "Profit",
            accessor: (row) => `$${row.amount.toFixed(2)}`,
        },
        {
            Header: "Date",
            accessor: "timestamp",
            Cell: ({ value }) => {
                const date = new Date(value);
                return date.toLocaleString('en-US', {
                    year: 'numeric', month: 'numeric', day: 'numeric',
                    hour: '2-digit', minute: '2-digit', second: '2-digit',
                    hour12: false
                });
            }
        },
        {
            Header: "Health",
            accessor: (row) => `${row.avgCowHealth.toFixed(3)}%`
        },
        {
            Header: "Cows",
            accessor: "numCows",
        },
    ];
    const buttonStyle = {
        backgroundColor: '#007bff',
        color: 'white',
        borderRadius: '20px',
        padding: '5px 15px',
        margin: '0 5px',
        cursor: 'pointer',
        marginTop: '20px'
    };
 
    const disabledButtonStyle = {
        ...buttonStyle,
        backgroundColor: '#cccccc',
        cursor: 'not-allowed'
    };
 
    return (
        <div>
            <OurTable
                data={currentRows}
                columns={columns}
                testid={"ProfitsTable"}
            />
            <div>
                <button 
                    style={currentPage === 0 ? disabledButtonStyle : buttonStyle}
                    onClick={() => handlePageChange(0)}
                    disabled={currentPage === 0}
                >
                    First
                </button>
                <button 
                    style={currentPage === 0 ? disabledButtonStyle : buttonStyle}
                    onClick={() => handlePageChange(currentPage - 1)}
                    disabled={currentPage === 0}
                >
                    Previous
                </button>
                <button 
                    style={currentPage >= totalPageCount - 1 ? disabledButtonStyle : buttonStyle}
                    onClick={() => handlePageChange(currentPage + 1)}
                    disabled={currentPage >= totalPageCount - 1}
                >
                    Next
                </button>
                <button 
                    style={currentPage >= totalPageCount - 1 ? disabledButtonStyle : buttonStyle}
                    onClick={() => handlePageChange(totalPageCount - 1)}
                    disabled={currentPage >= totalPageCount - 1}
                >
                    Last
                </button>
            </div>
        </div>
    );
}