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 4 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
23 changes: 16 additions & 7 deletions frontend/src/components/Modal/DeleteOrEditTypeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,25 @@ const DeleteOrEditTypeModal = ({
};

const confirmCancel = () => {
if (ride.recurring && single) {
const startDate = format_date(ride.startTime);
axios
.put(`/api/rides/${ride.id}/edits`, {
deleteOnly: true,
origDate: startDate,
})
if (ride.recurring) {
if (single) {
let startDate = new Date(ride.startTime);
startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), 0, 0, 0);
let parentDeletedDates : string[] = ride?.parentRide?.deleted === undefined ? [] : ride!.parentRide!.deleted;
parentDeletedDates.push(startDate.toISOString());
axios
.put(`/api/rides/${ride!.parentRide!.id}`, {deleted: parentDeletedDates})
.then(() => closeModal())
.then(refreshRides);
} else {
console.log("hellow, del", ride!.parentRide!.id);
axios
.delete(`/api/rides/${ride!.parentRide!.id}`)
.then(() => closeModal())
.then(refreshRides);
}
} else {
// console.log("hellow, del", ride!.id);
axios
.delete(`/api/rides/${ride.id}`)
.then(() => closeModal())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import { format_date } from '../../util/index';
import { LocationsProvider } from '../../context/LocationsContext';
import axios from '../../util/axios';

//this is for rider????


type CreateOrEditRideModalProps = {
isOpen: boolean;
modalType: RideModalType;
Expand All @@ -27,6 +30,7 @@ const CreateOrEditRideModal = ({
onClose = () => {},
ride,
}: CreateOrEditRideModalProps) => {
// console.log("FUCKKKK");
const defaultStartDate = () => {
if (ride) {
if (
Expand Down
142 changes: 113 additions & 29 deletions frontend/src/components/RideModal/RideModal.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import React, { useCallback, useEffect, useState } from 'react';
import moment from 'moment';
import Modal from '../Modal/Modal';
import { Button } from '../FormElements/FormElements';
import { Input, Label, Button } from '../FormElements/FormElements';
import { DriverPage, RiderInfoPage, RideTimesPage } from './Pages';
import { ObjectType, RepeatValues, Ride } from '../../types/index';
import { format_date } from '../../util/index';
import { useRides } from '../../context/RidesContext';
import { ToastStatus, useToast } from '../../context/toastContext';
import axios from '../../util/axios';
import DeleteOrEditTypeModal from 'components/Modal/DeleteOrEditTypeModal';
import { isOutOfBounds } from 'react-datepicker/dist/date_utils';

type RideModalProps = {
open?: boolean;
close?: () => void;
ride?: Ride;
editSingle?: boolean;
};

const getRideData = (ride: Ride | undefined) => {
Expand Down Expand Up @@ -62,14 +63,65 @@ const getRideData = (ride: Ride | undefined) => {
return {};
};

const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
type EditRecurringProps = {
onSubmit: (recurring : boolean) => void;
};

const EditRecurring = ({ onSubmit }: EditRecurringProps) => {
const [single, setSingle] = useState(true);
const changeSelection = (e: any) => {
setSingle(e.target.value === 'single');
};
return (
<>
<div>
<Input
type="radio"
id="single"
name="rideType"
value="single"
onClick={(e) => changeSelection(e)}
defaultChecked={true}
/>
<Label htmlFor="single">
This Ride Only
</Label>
</div>
<div>
<Input
type="radio"
id="recurring"
name="rideType"
value="recurring"
onClick={(e) => changeSelection(e)}
/>
<Label htmlFor="recurring">
All Repeating Rides
</Label>
</div>
<div>
<Button
type="submit"
onClick={() => {onSubmit(single)}}
>
{"OK"}
</Button>
</div>
</>
);
};

//need to add option to this to select whether to editsingle or not, shoulnd't be a prop passed in
const RideModal = ({ open, close, ride }: 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 [editSingle, setEditSingle] = useState(true);
const { showToast } = useToast();
const { refreshRides } = useRides();
const [showRest, setShowRest] = useState(false);

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

Expand All @@ -80,6 +132,7 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
setIsOpen(true);
};

// console.log("HELLO MATEW");
const closeModal = useCallback(() => {
if (close) {
setFormData(originalRideData);
Expand Down Expand Up @@ -167,17 +220,22 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
rideData.type = 'unscheduled';
}
if (editSingle) {
// edit single instance of repeating ride
const daysWithEdits: string[] =
ride!.parentRide!.edits === undefined
? []
: ride!.parentRide!.edits;
daysWithEdits.push(new Date(ride.startTime).toISOString());
axios
.put(`/api/rides/${ride.id}/edits`, {
deleteOnly: false,
origDate: format_date(ride.startTime),
...rideData,
.put(`/api/rides/${ride!.parentRide!.id!}`, {
edits: daysWithEdits,
})
.then(refreshRides);
// create new ride with no relation to parent ride
rideData = { ...rideData, parentRide: undefined };
axios.post('/api/rides', rideData).then(refreshRides);
} else {
// edit ride or all instances of repeating ride
axios.put(`/api/rides/${ride.id}`, rideData).then(refreshRides);
axios.put(`/api/rides/${ride!.parentRide!.id}`, rideData).then(refreshRides);
}
} else {
// unscheduled ride
Expand All @@ -199,26 +257,52 @@ const RideModal = ({ open, close, ride, editSingle }: RideModalProps) => {
]);

return ride ? (
<>
<Modal
paginate
title={['Edit Ride', 'Edit Ride']}
isOpen={open || isOpen}
currentPage={currentPage}
onClose={closeModal}
>
<RideTimesPage
defaultRepeating={ride.recurring}
formData={formData}
onSubmit={saveDataThen(goNextPage)}
/>
<RiderInfoPage
formData={formData}
onBack={goPrevPage}
onSubmit={saveDataThen(submitData)}
/>
</Modal>
</>
ride.recurring ? (
<>
{true && (
<Modal
paginate
title={['Edit Repeating Ride', 'Edit Ride', 'Edit Ride']}
isOpen={open || isOpen}
currentPage={currentPage}
onClose={closeModal}
>
<EditRecurring onSubmit={(recurring) => {setEditSingle(recurring); goNextPage()}}/>
<RideTimesPage
defaultRepeating={ride.recurring}
formData={formData}
onSubmit={saveDataThen(goNextPage)}
/>
<RiderInfoPage
formData={formData}
onBack={goPrevPage}
onSubmit={saveDataThen(submitData)}
/>
</Modal>
)}
</>
) : (
<>
<Modal
paginate
title={['Edit Ride', 'Edit Ride']}
isOpen={open || isOpen}
currentPage={currentPage}
onClose={closeModal}
>
<RideTimesPage
defaultRepeating={ride.recurring}
formData={formData}
onSubmit={saveDataThen(goNextPage)}
/>
<RiderInfoPage
formData={formData}
onBack={goPrevPage}
onSubmit={saveDataThen(submitData)}
/>
</Modal>
</>
)
) : (
<>
{!open && <Button onClick={openModal}>+ Add ride</Button>}
Expand Down
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
9 changes: 1 addition & 8 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 @@ -109,11 +108,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 @@ -197,7 +192,6 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
ride={rides[index]}
deleting={true}
onNext={(single) => {
setOpenDeleteOrEditModal(-1);
setOpenEditModal(index);
setEditSingle(single);
}}
Expand All @@ -216,7 +210,6 @@ const RidesTable = ({ rides, hasButtons }: RidesTableProps) => {
open={openEditModal === index}
close={() => setOpenEditModal(-1)}
ride={rides[index]}
editSingle={editSingle}
/>
</React.Fragment>
);
Expand Down
80 changes: 74 additions & 6 deletions frontend/src/context/RidesContext.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +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';
Expand Down Expand Up @@ -30,16 +36,78 @@ export const RidesProvider = ({ children }: RidesProviderProps) => {
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) {
.then((data) => data.data)
.then((data : Ride[]) => data.filter((ride : Ride) => !(ride.recurring && ride.parentRide === undefined)));

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!);
console.log(ride);
let startDate = new Date(ride.startTime);
startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate(), 0, 0, 0);
let deletedDateList = ride.deleted === undefined ? [] : ride.deleted.map((d) => new Date(d));
let editedDateList = ride.edits === undefined ? [] : ride.edits.map((d) => new Date(d));
console.log(deletedDateList);
return (
curDate >= startDate &&
curDate <= endDate &&
ride!.recurringDays?.includes(curDate.getDay()) &&
!deletedDateList.some((d) => d.getDate() === curDate.getDate() && d.getMonth() === curDate.getMonth() && d.getFullYear() === curDate.getFullYear()) &&
!editedDateList.some((d) => d.getDate() === curDate.getDate() && d.getMonth() === curDate.getMonth() && d.getFullYear() === curDate.getFullYear())
);
})
)
.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,
id : ""
};
})
);

console.log('current date is ', curDate.getDate());
// const ridesDataTodayNoParentRec = ridesDataToday.filter((ride : Ride) => !(ride.recurring && ride.parentRide === undefined));
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