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

Dka36/DriversConflict #556

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Loading