Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions frontend/src/components/UserTables/RidesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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';

type RidesTableProps = {
rides: Ride[];
Expand Down Expand Up @@ -44,6 +45,20 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
'',
];

// hasConflict(rides, newRide) is true if and only if the driver of the new ride has a conflict (Date object time-overlap)
// with an existing ride in the table (which is represented as array of rides).
function hasConflict(rides: Ride[], newRide: Ride): boolean {
return rides.some((ride) => {
return (
ride.driver === newRide.driver &&
((new Date(newRide.startTime) >= new Date(ride.startTime) &&
new Date(newRide.startTime) < new Date(ride.endTime)) ||
(new Date(newRide.endTime) > new Date(ride.startTime) &&
new Date(newRide.endTime) <= new Date(ride.endTime)))
);
});
}

return (
<>
<Table>
Expand Down Expand Up @@ -85,18 +100,26 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
),
};

const assignButton = (shouldReassign: boolean) => (
<Button
className={styles.assignButton}
onClick={() => {
setOpenAssignModal(index);
setReassign(shouldReassign);
}}
small
>
{shouldReassign ? 'Reassign' : 'Assign'}
</Button>
);
const assignButton = (shouldReassign: boolean, newRide: Ride) => {
return (
<Button
className={styles.assignButton}
onClick={() => {
// If shouldReassign is false, || short-circuits and evaluates to the expression to True,
// so we don't perform the linear scan for checking conflicts.
if (!shouldReassign || !hasConflict(rides, newRide)) {
setOpenAssignModal(index);
setReassign(shouldReassign);
} else {
// To be changed with a UI update later, most likely.
alert('Driver has a scheduling conflict.');
}
}}
>
Assign Ride
</Button>
);
};

const editButton = (
<Button
Expand Down Expand Up @@ -129,7 +152,7 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
data: (
<div className={styles.dataValues}>
{editButton}
{assignButton(false)}
{assignButton(false, ride)}
{deleteButton}
</div>
),
Expand All @@ -139,7 +162,7 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
data: (
<div className={styles.dataValues}>
{editButton}
{assignButton(true)}
{assignButton(true, ride)}
{deleteButton}
</div>
),
Expand Down