Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter out drivers in Reassign drivers for scheduled rides based on availability. #514

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
78 changes: 76 additions & 2 deletions frontend/src/components/UserTables/RidesTable.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Ride } from '../../types/index';
import { Driver, Ride } from '../../types/index';
import { Row, Table } from '../TableComponents/TableComponents';
import { Button } from '../FormElements/FormElements';
import AssignDriverModal from '../Modal/AssignDriverModal';
Expand All @@ -8,12 +8,80 @@ import styles from './table.module.css';
import { useEmployees } from '../../context/EmployeesContext';
import DeleteOrEditTypeModal from '../Modal/DeleteOrEditTypeModal';
import { trashbig } from '../../icons/other/index';
import { DriverType } from '../../../../server/src/models/driver';
import { start } from 'repl';

type RidesTableProps = {
rides: Ride[];
hasButtons: boolean;
};

/**
* Summary: check if a driver is available on a certain day based on their availability property.
* @param driver
* @param day : is a number from 0 to 6 representing 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
* @returns whether driver is available on that day.
*/
const isAvailableOnDay = (driver: DriverType, day: number) => {
const dayAsString = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
for (const [key, value] of Object.entries(driver.availability)) {
if (key === dayAsString.at(day) && value != undefined) {
return true;
}
}
return false;
};

// You can add additional filters to filter our drivers to reassign here.
const driverFilters: ((driver: DriverType) => boolean)[] = [
// (driver: DriverType) => {
// return driver.firstName != 'Naoya';
// },
];

const checkAdditionalFilters = (driver: DriverType) => {
return driverFilters.every((fn) => fn(driver));
};

/**
* summary: checks if a driver is availble between startTime and endTime based on their availability property.
* @param driver
* @param startTime
* @param endTime
* @returns whether driver is available between these 2 times.
*/
const isAvailable = (driver: DriverType, startTime: Date, endTime: Date) => {
const dayAsString = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const startTimeDay = startTime.getDay();
const endTimeDay = endTime.getDay();
if (
isAvailableOnDay(driver, startTimeDay) &&
isAvailableOnDay(driver, endTimeDay)
) {
const startDay = dayAsString[
startTimeDay
] as keyof typeof driver.availability;
const endDay = dayAsString[endTimeDay] as keyof typeof driver.availability;
const driverStartDayAvailibility = driver.availability[startDay]; // hh:mm even for h <10
const driverEndDayAvailibility = driver.availability[endDay];
return (
driverStartDayAvailibility!.startTime <=
startTime.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
hour12: false,
}) &&
driverEndDayAvailibility!.endTime >=
endTime.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
hour12: false,
})
);
}
return false;
};

const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
const { drivers } = useEmployees();
const [openAssignModal, setOpenAssignModal] = useState(-1);
Expand Down Expand Up @@ -202,7 +270,13 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
isOpen={openAssignModal === index}
close={() => setOpenAssignModal(-1)}
ride={rides[index]}
allDrivers={drivers}
allDrivers={drivers.filter((driver) => {
return isAvailable(
driver,
new Date(ride.startTime),
new Date(ride.endTime)
);
})}
reassign={reassign}
/>
<RideModal
Expand Down
Loading