All files / components/Users UsersTable.js

100% Statements 34/34
100% Branches 6/6
100% Functions 14/14
100% Lines 32/32

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 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146                      28x 28x 28x 28x     28x               28x 4x 4x     28x 1x 1x       28x               28x 4x 4x     28x 1x 1x     28x                                       78x         78x         78x       28x   28x           28x     28x               1x     1x             28x               1x     1x           28x                      
import React,  {useState} from "react";
import OurTable, {ButtonColumn} from "main/components/OurTable";
import { useBackendMutation } from "main/utils/useBackend";
import { cellToAxiosParamsSuspend, onSuspendSuccess, cellToAxiosParamsRestore, onRestoreSuccess } from "main/utils/usersUtils"
import { hasRole } from "main/utils/currentUser";
import { formatTime } from "main/utils/dateUtils";
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal';
 
export default function UsersTable({ users, currentUser }) {
 
    const [showModalSuspend, setShowModalSuspend] = useState(false);
    const [cellToSuspend, setCellToSuspend] = useState(null);
    const [showModalRestore, setShowModalRestore] = useState(false);
    const [cellToRestore, setCellToRestore] = useState(null);
 
    // Stryker disable all : hard to test for query caching
    const suspendMutation = useBackendMutation(
        cellToAxiosParamsSuspend,
        { onSuccess: onSuspendSuccess },
        ["/api/admin/users"]
    );
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const suspendCallback = async (cell) => {
        setCellToSuspend(cell);
        setShowModalSuspend(true);
    }
 
    const confirmSuspend = async (cell) => {
        suspendMutation.mutate(cell);
        setShowModalSuspend(false);
    };
 
    // Stryker disable all : hard to test for query caching
    const restoreMutation = useBackendMutation(
        cellToAxiosParamsRestore,
        { onSuccess: onRestoreSuccess },
        ["/api/admin/users"]
    );
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const restoreCallback = async (cell) => {
        setCellToRestore(cell);
        setShowModalRestore(true);
    }
 
    const confirmRestore = async (cell) => {
        restoreMutation.mutate(cell);
        setShowModalRestore(false);
    };
 
    const columns = [
        {
            Header: 'id',
            accessor: 'id', // accessor is the "key" in the data
        },
        {
            Header: 'First Name',
            accessor: 'givenName',
        },
        {
            Header: 'Last Name',
            accessor: 'familyName',
        },
        {
            Header: 'Email',
            accessor: 'email',
        },
        {
            Header: 'Last Online',
            id: 'lastOnline',
            accessor: (row) => formatTime(row.lastOnline),
        },
        {
            Header: 'Admin',
            id: 'admin',
            accessor: (row, _rowIndex) => String(row.admin) // hack needed for boolean values to show up
        },
        {
            Header: 'Suspended',
            id: 'suspended',
            accessor: (row, _rowIndex) => String(row.suspended) // hack needed for boolean values to show up
        },
    ];
 
    const testid = "UsersTable";
 
    const columnsIfAdmin = [
        ...columns,
        ButtonColumn("Suspend", "danger", suspendCallback, testid),
        ButtonColumn("Restore", "success", restoreCallback, testid),
    ];
 
    const columnsToDisplay = hasRole(currentUser,"ROLE_ADMIN") ? columnsIfAdmin : columns;
 
    const usersModalSuspend = (
        <Modal data-testid="UsersTable-Modal" show={showModalSuspend} onHide={() => setShowModalSuspend(false)}>
            <Modal.Header closeButton>
                <Modal.Title>Confirm Suspension</Modal.Title>
            </Modal.Header>
            <Modal.Body>
                Are you sure you want to suspend this user?            
            </Modal.Body>
            <Modal.Footer>
                <Button variant="secondary" data-testid="UsersTable-SuspendModal-Cancel" onClick={() => setShowModalSuspend(false)}>
                    Leave this User as is
                </Button>
                <Button variant="danger" data-testid="UsersTable-Modal-Suspend" onClick={() => confirmSuspend(cellToSuspend)}>
                    Suspend this User
                </Button>
            </Modal.Footer>
        </Modal> );
 
    const usersModalRestore = (
        <Modal data-testid="UsersTable-Modal" show={showModalRestore} onHide={() => setShowModalRestore(false)}>
            <Modal.Header closeButton>
                <Modal.Title>Confirm Restoration</Modal.Title>
            </Modal.Header>
            <Modal.Body>
                Are you sure you want to restore this user?            
            </Modal.Body>
            <Modal.Footer>
                <Button variant="secondary" data-testid="UsersTable-RestoreModal-Cancel" onClick={() => setShowModalRestore(false)}>
                    Leave this User as is
                </Button>
                <Button variant="success" data-testid="UsersTable-Modal-Restore" onClick={() => confirmRestore(cellToRestore)}>
                    Restore this User
                </Button>
            </Modal.Footer>
        </Modal> );
 
    return (
    <>
        <OurTable
            data={users}
            columns={columnsToDisplay}
            testid={testid}
        />
        {hasRole(currentUser,"ROLE_ADMIN") && usersModalSuspend}
        {hasRole(currentUser,"ROLE_ADMIN") && usersModalRestore}
    </>);
 
};