All files / components/RiderApplication RiderApplicationTable.js

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

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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120                          59x 59x   59x 1x     59x 1x     59x 1x         59x                 59x 4x   4x   2x       59x                                             59x           81x 16x       1x     65x           81x 16x       4x     65x         59x           59x   59x              
import React from "react";
import OurTable, { ButtonColumn } from "main/components/OurTable";
import { useBackendMutation } from "main/utils/useBackend";
import { onPutSuccess, cellToAxiosParamsCancelApplicationMember} from "main/utils/riderApplicationUtils";
import { useNavigate } from "react-router-dom";
import { hasRole } from "main/utils/currentUser";
import { Button } from "react-bootstrap";
 
export default function RiderApplicationTable({
        riderApplications,
        currentUser
    }){
 
    const testid = "RiderApplicationTable";
    const navigate = useNavigate();
 
    const showCallback = (cell) => {
        navigate(`/apply/rider/show/${cell.row.values.id}`)
    }
    
    const editCallback = (cell) => {
        navigate(`/apply/rider/edit/${cell.row.values.id}`)
    }
 
    const reviewCallback = (cell) => {
        navigate(`/admin/applications/riders/review/${cell.row.values.id}`)
    }
 
    // Stryker disable all : hard to test for query caching
 
    const cancelMutation = useBackendMutation(
        cellToAxiosParamsCancelApplicationMember,
        { onSuccess: onPutSuccess },
        ["/apply/rider"]
    );
 
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const cancelCallback = async (cell) => { 
        const confirmation = window
                            .confirm("Are you sure you want to cancel this application?\n\nClick 'OK' to confirm or 'Cancel' to keep your application active.");
        if (confirmation)
        {
            cancelMutation.mutate(cell); 
        }
    }
 
    const defaultColumns = [
        {
            Header: 'Application Id',
            accessor: 'id', // accessor is the "key" in the data
        },
        {
            Header: 'Date Applied',
            accessor: 'created_date',
        },
        {
            Header: 'Date Updated',
            accessor: 'updated_date',
        },
        {
            Header: 'Date Cancelled',
            accessor: 'cancelled_date',
        },
        {
            Header: 'Status',
            accessor: 'status',
        }
    ];
 
    const buttonColumnsMember = [
        ...defaultColumns,
        ButtonColumn("Show", "primary", showCallback, "RiderApplicationTable"),
        {
            Header: 'Edit',
            Cell: ({ cell }) => {
                if (cell.row.values.status === 'pending') {
                    return (
                        <Button
                        data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
                        variant = {"primary"} 
                        onClick={() => editCallback(cell)}>Edit</Button>
                    );
                }
                return null;
            }
        },
        {
            Header: 'Cancel',
            Cell: ({ cell }) => {
                if (cell.row.values.status === 'pending') {
                    return (
                        <Button
                        data-testid={`${testid}-cell-row-${cell.row.index}-col-${cell.column.id}-button`}
                        variant = {"danger"} 
                        onClick={() => cancelCallback(cell)}>Cancel</Button>
                    );
                }
                return null;
            }
        },
    ]
 
    const buttonColumnsAdmin = [
        ...defaultColumns,
        ButtonColumn("Review", "primary", reviewCallback, "RiderApplicationTable")
 
    ];
 
    const columnsToDisplay = hasRole(currentUser, "ROLE_ADMIN") ? buttonColumnsAdmin : buttonColumnsMember ;
    
    return <OurTable
        data={riderApplications}
        columns={columnsToDisplay}
        testid={testid}
    />;
};