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: Need to make recurring rides work as a feature; Fixed Schedule so that rides from other days show up. #539

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion frontend/src/components/Schedule/Schedule.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ Rider: ${ride.rider.firstName} ${ride.rider.lastName}`,
localizer={localizer}
toolbar={false}
step={5}
defaultDate={scheduleDay}
date={scheduleDay}
timeslots={12}
showMultiDayTimes={true}
events={events}
Expand Down
8 changes: 1 addition & 7 deletions frontend/src/components/UserTables/RidesTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
const { drivers } = useEmployees();
const [openAssignModal, setOpenAssignModal] = useState(-1);
const [openEditModal, setOpenEditModal] = useState(-1);
const [openDeleteOrEditModal, setOpenDeleteOrEditModal] = useState(-1);
const [editSingle, setEditSingle] = useState(false);
const [reassign, setReassign] = useState(false);
const [deleteOpen, setDeleteOpen] = useState(-1);
Expand Down Expand Up @@ -103,11 +102,7 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
outline
small
onClick={() => {
if (rides[index].recurring) {
setOpenDeleteOrEditModal(index);
} else {
setOpenEditModal(index);
}
setOpenEditModal(index);
}}
>
Edit
Expand Down Expand Up @@ -191,7 +186,6 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
ride={rides[index]}
deleting={true}
onNext={(single) => {
setOpenDeleteOrEditModal(-1);
setOpenEditModal(index);
setEditSingle(single);
}}
Expand Down
89 changes: 84 additions & 5 deletions frontend/src/context/RidesContext.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React, { useCallback, useState, useRef, useEffect } from 'react';
import React, { useCallback, useState, useRef, useEffect, useContext } from 'react';
import { Ride, Type } from '../types';
import { useDate } from './date';
import { format_date } from '../util/index';
import axios from '../util/axios';


type ridesState = {
unscheduledRides: Ride[];
scheduledRides: Ride[];
Expand All @@ -23,23 +24,101 @@ type RidesProviderProps = {
children: React.ReactNode;
};

function uuidv4() {
return '10000000-1000-4000-8000-100000000000'.replace(/[018]/g, (c) =>
(
+c ^
(crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (+c / 4)))
).toString(16)
);
}

export const RidesProvider = ({ children }: RidesProviderProps) => {
const [unscheduledRides, setUnscheduledRides] = useState<Ride[]>([]);
const [scheduledRides, setScheduledRides] = useState<Ride[]>([]);
const { curDate } = useDate();
const date = format_date(curDate);

const refreshRides = useCallback(async () => {
const ridesData: Ride[] = await axios
const ridesDataToday: Ride[] = await axios
.get(`/api/rides?date=${date}`)
.then((res) => res.data)
.then((data) => data.data);
if (ridesData) {

const newRecurringRides: Ride[] = await axios
.get(`/api/rides?recurring=true`)
Atikpui007 marked this conversation as resolved.
Show resolved Hide resolved
.then((res) => res.data)
.then((data) => data.data)
// here, I'm assuming that all rides with parentRide as undefined is the "source" rides, meaning that all recurring rides have these rides as parents.
.then((data : Ride[]) => data.filter((ride) => ride.parentRide === undefined))
.then((data: Ride[]) =>
data.filter((ride) => {
let endDate = new Date(ride!.endDate!);
return (
curDate <= endDate &&
ride!.recurringDays?.includes(curDate.getDay())
);
})
)
.then((recurringParentRides: Ride[]) =>
recurringParentRides.filter((parentRide) => {
const startTimeRecurringRide = new Date(parentRide.startTime);
startTimeRecurringRide.setFullYear(curDate.getFullYear());
startTimeRecurringRide.setMonth(curDate.getMonth());
startTimeRecurringRide.setDate(curDate.getDate());

const endTimeRecurringRide = new Date(parentRide.endTime);
endTimeRecurringRide.setFullYear(curDate.getFullYear());
endTimeRecurringRide.setMonth(curDate.getMonth());
endTimeRecurringRide.setDate(curDate.getDate());

return !ridesDataToday.some(
(rideToday) =>
(new Date(rideToday.startTime)).getTime() === startTimeRecurringRide.getTime() &&
(new Date(rideToday.endTime)).getTime() === endTimeRecurringRide.getTime() &&
rideToday.startLocation.name === parentRide.startLocation.name &&
rideToday.endLocation.name === parentRide.endLocation.name &&
rideToday.rider.id === parentRide.rider.id
);
})
)
.then((recurringParentRides: Ride[]) =>
recurringParentRides.map((parentRide) => {

const startTimeRecurringRide = new Date(parentRide.startTime);
startTimeRecurringRide.setFullYear(curDate.getFullYear());
startTimeRecurringRide.setMonth(curDate.getMonth());
startTimeRecurringRide.setDate(curDate.getDate());

const endTimeRecurringRide = new Date(parentRide.endTime);
endTimeRecurringRide.setFullYear(curDate.getFullYear());
endTimeRecurringRide.setMonth(curDate.getMonth());
endTimeRecurringRide.setDate(curDate.getDate());

console.log("end time recurring ride is ", endTimeRecurringRide);
console.log("end time iso recurring ride is ", endTimeRecurringRide.toISOString());
console.log("end time parent", parentRide.endTime);

// console.log("parent end time", new Date(parentRide.endTime))
return {
...parentRide,
startTime: startTimeRecurringRide.toISOString(),
endTime: endTimeRecurringRide.toISOString(),
type : Type.UNSCHEDULED,
parentRide: parentRide,
};
})
);

console.log("current date is ", curDate.getDate());

const combinedRidesData = ridesDataToday.concat(newRecurringRides);
if (combinedRidesData) {
setUnscheduledRides(
ridesData.filter(({ type }) => type === Type.UNSCHEDULED)
combinedRidesData.filter(({ type }) => type === Type.UNSCHEDULED)
);
setScheduledRides(
ridesData.filter(({ type }) => type !== Type.UNSCHEDULED)
combinedRidesData.filter(({ type }) => type !== Type.UNSCHEDULED)
);
}
}, [date]);
Expand Down
Loading