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
68 changes: 66 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,6 +8,8 @@ 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[];
Expand All @@ -23,6 +25,62 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
const [reassign, setReassign] = useState(false);
const [deleteOpen, setDeleteOpen] = useState(-1);

const dayAsString = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

const isAvailableOnDay = (driver: DriverType, day: number) => {
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 reassignDriverFilters: ((driver: DriverType) => boolean)[] = [
// (driver: DriverType) => {
// return driver.firstName != 'Bin laden';
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please dont add this lol
Specifically line 42. Change the first name example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, forgot to removed it when I was messing around. Changed it now tho.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget to push your changes 🙏 The test driver name is still there lol

// },
];

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

const isAvailable = (driver: DriverType, startTime: Date, endTime: Date) => {
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,
}) &&
checkAdditionalFilters(driver)
);
}
return false;
};

const unscheduledColSizes = [0.5, 0.5, 0.8, 1, 1, 0.8, 1];
const unscheduledHeaders = [
'',
Expand Down Expand Up @@ -194,7 +252,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