All files / pages/Scheduler SchedulerPage.js

100% Statements 52/52
100% Branches 15/15
100% Functions 25/25
100% Lines 46/46

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 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197                              39x   39x   39x     39x                   39x 1x     39x 1x       39x               39x           39x                   39x 1x     39x 1x       39x               39x           39x                   39x 1x       39x               39x   39x 73x 17x   51x                 1x 1x 1x           56x 17x   51x                 1x 1x 1x           39x 17x   51x               1x 1x           22x     39x       1x 1x 1x           3x                  
import React from "react";
import { useNavigate, useParams } from "react-router-dom";
 
import SchedulerPanel from "main/components/Scheduler/SchedulerPanel";
 
import BasicLayout from "main/layouts/BasicLayout/BasicLayout";
import { Button, ButtonGroup, Stack } from "react-bootstrap";
 
import { useBackend, useBackendMutation } from "main/utils/useBackend";
 
import { cellToAxiosParamsDelete as shiftCellToAxiosParamsDelete, onDeleteSuccess as shiftOnDeleteSuccess } from "main/utils/shiftUtils"
import { cellToAxiosParamsDelete as RideCellToAxiosParamsDelete, onDeleteSuccess as RideOnDeleteSuccess } from "main/utils/rideUtils"
import { cellToAxiosParamsDelete as driverAvailabilityCellToAxiosParamsDelete, onDeleteSuccess as driverAvailabilityOnDeleteSuccess } from "main/utils/driverAvailabilityUtils"
 
export default function SchedulerPage() {
    const { page } = useParams();
 
    const testId = "SchedulerPage";
 
    const navigate = useNavigate();
 
    const { data: shifts } =
        useBackend(
            // Stryker disable next-line all : don't test internal caching of React Query
            [`/api/shift/all`],
            {  // Stryker disable next-line all : GET is the default, so changing this to "" doesn't introduce a bug
                method: "GET",
                url: `/api/shift/all`
                // Stryker restore all
            },
        );
 
    const editShiftCallback = (shift) => {
        navigate(`/shift/edit/${shift.id}`)
    }
 
    const infoShiftCallback = (shift) => {
        navigate(`/shiftInfo/${shift.id}`)
    }
 
    // Stryker disable all : hard to test for query caching
    const shiftDeleteMutation = useBackendMutation(
        shiftCellToAxiosParamsDelete,
        { onSuccess: shiftOnDeleteSuccess },
        ["/api/shift/all"]
    );
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const deleteShiftCallback = async (shift) => { shiftDeleteMutation.mutate({row: {values: {id: shift.id}}}); }
 
 
 
 
    const { data: ride_request } =
        useBackend(
            // Stryker disable next-line all : don't test internal caching of React Query
            [`/api/ride_request/all`],
            {  // Stryker disable next-line all : GET is the default, so changing this to "" doesn't introduce a bug
                method: "GET",
                url: `/api/ride_request/all`
                // Stryker restore all
            },
        );
 
    const editRideCallback = (ride) => {
        navigate(`/ride/edit/${ride.id}`)
    }
 
    const assignRideCallback = (ride) => {
        navigate(`/ride/assigndriver/${ride.id}`)
    }
 
    // Stryker disable all : hard to test for query caching
    const RideDeleteMutation = useBackendMutation(
        RideCellToAxiosParamsDelete,
        { onSuccess: RideOnDeleteSuccess },
        ["/api/ride_request/all"]
    );
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const deleteRideCallback = async (ride) => { RideDeleteMutation.mutate({row: {values: {id: ride.id}}}); }
 
 
 
 
    const { data: driverAvailability } =
        useBackend(
            // Stryker disable next-line all : don't test internal caching of React Query
            [`/api/driverAvailability/admin/all`],
            {  // Stryker disable next-line all : GET is the default, so changing this to "" doesn't introduce a bug
                method: "GET",
                url: `/api/driverAvailability/admin/all`
                // Stryker restore all
            },
        );
 
    const reviewDriverCallback = (driver) => {
        navigate(`/admin/availability/review/${driver.id}`)
    }
 
    // Stryker disable all : hard to test for query caching
    const DriverDeleteMutation = useBackendMutation(
        driverAvailabilityCellToAxiosParamsDelete,
        { onSuccess: driverAvailabilityOnDeleteSuccess },
        ["/availability"]
    );
    // Stryker restore all 
 
    // Stryker disable next-line all : TODO try to make a good test for this
    const deleteDriverCallback = async (driver) => { DriverDeleteMutation.mutate({row: {values: {id: driver.id}}}); }
 
    const eventLink = () => {
        if(page === "shifts" && shifts !== undefined) {
            return {
                event: 
                    shifts.map(shift => ({
                        id: shift.id,
                        title: `Shift for Driver ${shift.driverID}`,
                        // Stryker disable next-line all : hard to test for specific description
                        description: `Backup Driver: ${shift.driverBackupID}`,
                        day: shift.day,
                        startTime: shift.shiftStart,
                        endTime: shift.shiftEnd,
                        actions: [
                            { text: "Edit", variant: "primary", callback: () => editShiftCallback(shift) },
                            { text: "Delete", variant: "danger", callback: () => {deleteShiftCallback(shift); navigate(0)} },
                            { text: "Info", variant: "success", callback: () => infoShiftCallback(shift) }
                        ]
                    })),
                createLink: "/shift/create"
            }
        }
        else if(page === "rides" && ride_request !== undefined) {
            return {
                event:
                    ride_request.map(request => ({
                        id: request.id,
                        title: `Ride Request for ${request.student}`,
                        // Stryker disable next-line all : hard to test for specific description
                        description: `From ${request.pickupBuilding} to ${request.dropoffBuilding}`,
                        day: request.day,
                        startTime: request.startTime,
                        endTime: request.endTime,
                        actions: [
                            { text: "Edit", variant: "primary", callback: () => editRideCallback(request) },
                            { text: "Delete", variant: "danger", callback: () => {deleteRideCallback(request); navigate(0)} },
                            { text: "Assign Driver", variant: "success", callback: () => assignRideCallback(request) }
                        ]
                    })),
                createLink: "/ride/create"
            }
        }
        else if(page === "driver" && driverAvailability !== undefined) {
            return {
                event:
                    driverAvailability.map(availability => ({
                        id: availability.id,
                        title: `Availability for Driver ${availability.driverId}`,
                        description: availability.notes,
                        day: availability.day,
                        startTime: availability.startTime,
                        endTime: availability.endTime,
                        actions: [  
                            { text: "Delete", variant: "danger", callback: () => {deleteDriverCallback(availability); navigate(0)} },
                            { text: "Review", variant: "success", callback: () => reviewDriverCallback(availability) }
                        ]
                    })),
                createLink: "/availability/create"
            }
        }
        return {}
    }
 
    return (
        <BasicLayout>
            <Stack className="flex-row justify-content-between">
                <ButtonGroup>
                    <Button onClick={() => {navigate('/admin/schedule/shifts')}}>Shifts</Button>
                    <Button onClick={() => {navigate('/admin/schedule/rides')}}>Ride Requests</Button>
                    <Button onClick={() => {navigate('/admin/schedule/driver')}}>Driver Availability</Button>
                </ButtonGroup>
                {page && eventLink().createLink && 
                    <Button
                        variant="success"
                        data-testid={`${testId}-create-${page}`}
                        onClick={() => navigate(eventLink().createLink)}
                    >
                        Create {page}
                    </Button>
                }
            </Stack>
            <SchedulerPanel Events={eventLink().event} />
        </BasicLayout>
    );
}