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

WIP: No Overlapping Rides per Driver #517

Open
wants to merge 2 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
69 changes: 66 additions & 3 deletions frontend/src/components/RideModal/RideModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,20 @@ const getRideData = (ride: Ride | undefined) => {
return {};
};

interface RideTime {
startTime: string;
endTime: string;
}

const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
const originalRideData = getRideData(ride);
const [formData, setFormData] = useState<ObjectType>(originalRideData);
const [isOpen, setIsOpen] = useState(open !== undefined ? open : false);
const [currentPage, setCurrentPage] = useState(0);
const [isSubmitted, setIsSubmitted] = useState(false);
const { showToast } = useToast();
const { refreshRides } = useRides();
const { scheduledRides, refreshRides } = useRides();
const [rideTimesList, setRideTimesList] = useState<RideTime[]>([]);

const goNextPage = () => setCurrentPage((p) => p + 1);

Expand All @@ -91,6 +97,22 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
setIsOpen(false);
}, [close, originalRideData]);

const saveRideTimeDataThen = (next: () => void) => (data: ObjectType) => {
setFormData((prev) => ({ ...prev, ...data }));

const rideTimeData = {
startTime: moment(`${data.date} ${data.pickupTime}`).toISOString(),
endTime: moment(`${data.date} ${data.dropoffTime}`).toISOString(),
};
setRideTimesList([rideTimeData]);

// After the ride has been created, we set RideTimesList to be empty, which
// means there are no more impending rides to add.
setRideTimesList([]);

next();
};

const saveDataThen = (next: () => void) => (data: ObjectType) => {
setFormData((prev) => ({ ...prev, ...data }));
next();
Expand Down Expand Up @@ -124,6 +146,20 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
};

useEffect(() => {
if (rideTimesList.length == 1) {
console.log('ride we wanna add', rideTimesList[0]);

const inProgressRidePickup =
new Date(rideTimesList[0].startTime).getHours() * 60 +
new Date(rideTimesList[0].startTime).getMinutes();

const inProgressRideDropoff =
new Date(rideTimesList[0].endTime).getHours() * 60 +
new Date(rideTimesList[0].endTime).getMinutes();

checkOverlap(inProgressRidePickup, inProgressRideDropoff, scheduledRides);
}

if (isSubmitted) {
const {
date,
Expand Down Expand Up @@ -210,7 +246,7 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
<RideTimesPage
defaultRepeating={ride.recurring}
formData={formData}
onSubmit={saveDataThen(goNextPage)}
onSubmit={saveRideTimeDataThen(goNextPage)}
/>
<RiderInfoPage
formData={formData}
Expand All @@ -232,7 +268,7 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
>
<RideTimesPage
formData={formData}
onSubmit={saveDataThen(goNextPage)}
onSubmit={saveRideTimeDataThen(goNextPage)}
/>
<DriverPage
formData={formData}
Expand All @@ -250,4 +286,31 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
);
};

// Checks if the impending ride overlaps with any of the scheduled rides. This is
// a WIP: after identifying which driver has an overlap, we want to remove their
// ID from the list of drivers that can be scheduled to take on this impending ride.
const checkOverlap = (
inProgressRidePickup: number,
inProgressRideDropoff: number,
rides: Ride[]
) => {
for (let i = 0; i < rides.length; i++) {
const ridePickup =
new Date(rides[i].startTime).getHours() * 60 +
new Date(rides[i].startTime).getMinutes();
const rideDropoff =
new Date(rides[i].endTime).getHours() * 60 +
new Date(rides[i].endTime).getMinutes();

if (
inProgressRidePickup - 5 < rideDropoff &&
ridePickup - 5 < inProgressRideDropoff
) {
console.log(rides[i].driver?.firstName, 'has an overlap!');
} else {
console.log(rides[i].driver?.firstName, "doesn't have an overlap!");
}
}
};

export default RideModal;
Loading