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 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
150 changes: 137 additions & 13 deletions frontend/src/components/Modal/DeleteOrEditTypeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { format_date } from '../../util/index';
import { ToastStatus, useToast } from '../../context/toastContext';
import { useRides } from '../../context/RidesContext';
import axios from '../../util/axios';
import { useDate } from '../../context/date';


type DeleteOrEditTypeModalProps = {
open: boolean;
Expand All @@ -26,25 +28,134 @@ const DeleteOrEditTypeModal = ({
isRider,
}: DeleteOrEditTypeModalProps) => {
const [single, setSingle] = useState(true);
const [allOrFollowing, setAllOrFollowing] = useState(true); // true means for all.
const { showToast } = useToast();
const { refreshRides } = useRides();

const closeModal = () => {
onClose();
setSingle(true);
};
const {curDate} = useDate();

//delete logic for normal rides and recurring rides (delete single, all, or from this ride and all following rides)
const confirmCancel = () => {
if (ride.recurring && single) {
const startDate = format_date(ride.startTime);
axios
.put(`/api/rides/${ride.id}/edits`, {
deleteOnly: true,
origDate: startDate,
})
.then(() => closeModal())
.then(refreshRides);
if (ride.recurring) {
//Need to fix the logic for this
if (single) {
/**
* trim the end date of the ride to before today, axios.put
* create a new recurring ride with the parent’s original end date (if that endate is > today) starting from today + 1
* link that ride back to the parent, fill in parent and children fields.
*/
const originalEndDate = new Date(ride.sourceRide!.endDate!);

let newEndDate = curDate;
newEndDate.setDate(newEndDate.getDate() - 1);
newEndDate.setHours(0, 0, 0);

let sourceRideStartDate = new Date(ride.sourceRide!.startTime);
sourceRideStartDate.setHours(0, 0, 0);
let deletedSourceRide = false;

if (sourceRideStartDate <= newEndDate) {
const {id, parentRide, childRide, sourceRide, ...sourceRidewithoutRideTypes} = ride.sourceRide!;
axios.put(`/api/rides/${ride.id}`, {...sourceRidewithoutRideTypes, endDate : newEndDate.toISOString()});
ride.sourceRide! = {...ride.sourceRide!, endDate : newEndDate.toISOString()};
} else {
// if enddate is a day before the sourceRide start date then delete
deletedSourceRide = true;
axios.delete(`/api/rides/${ride.id}`);
}

if (originalEndDate > curDate) {
//create a new recurring ride with same data as the old one but with start date = curDate + 1.
let newRideStartTime = new Date(ride.sourceRide!.startTime);
newRideStartTime.setDate(curDate.getDate() + 1);
let newRideEndTime = new Date(ride.sourceRide!.endTime);
newRideEndTime.setDate(curDate.getDate() + 1);


let {id, ...rideNoId} = ride.sourceRide!;
const newChildRide = {
...rideNoId,
startTime : newRideStartTime.toISOString(),
endTime : newRideEndTime.toISOString(),
endDate : format_date(originalEndDate),
parentRideId : ride.id,
childRideId: ride.sourceRide!.childRideId,
recurring : true,
type : 'unscheduled'
}
axios
.post('/api/rides', newChildRide)
.then((response) => response.data)
.then((rideData) => {
if (!deletedSourceRide) {
const {id, parentRide, childRide, sourceRide, ...sourceRidewithoutRideTypes} = ride.sourceRide!;
axios.put(`/api/rides/${ride.id}`, {...sourceRidewithoutRideTypes, childRideId: rideData.id});
}
if (ride.sourceRide!.childRideId !== undefined) {
const {id, parentRide, childRide, sourceRide, ...childRidewithoutRideTypes} = ride.sourceRide!.childRide!;
axios.put(`/api/rides/${ride.sourceRide!.childRide!.id}`, {...childRidewithoutRideTypes, parentRideId : rideData.id});
}
}
);
}
closeModal();
refreshRides();
} else {
if (allOrFollowing) {
/**
* 1. Go up to find the ancestor ride and delete all descendants, including itself.
* 2. refreshRides
*/

//traverse to the ancestor and delete all rides along the way.
let currentRide = ride.sourceRide;
while (currentRide!.parentRide !== undefined) {
currentRide = currentRide!.parentRide;
}
// now current ride is at the beginning of linked list
console.log(currentRide);
while (currentRide !== undefined) {
console.log("hello, deleting all, current Ride is", currentRide);
axios.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.childRide;
}

closeModal();
refreshRides();
} else {
/**
* trim the ride’s parent date to before today, axios put
* delete all descendants of the parent’s ride., axios delete
*/
let newEndDate = curDate;
newEndDate.setDate(newEndDate.getDate() - 1);
newEndDate.setHours(0, 0, 0);

let sourceRideStartDate = new Date(ride.sourceRide!.startTime);
sourceRideStartDate.setHours(0, 0, 0);

//if start date of source ride > trimmedEndate then need to delete
if (sourceRideStartDate <= newEndDate) {
axios.put(`/api/rides/${ride.id}`, {...ride.sourceRide, endDate : newEndDate.toISOString()});
} else {
axios.delete(`/api/rides/${ride.id}`)
}
let currentRide = ride.childRide;
while (currentRide !== undefined) {
axios
.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.childRide;
}
closeModal();
refreshRides();
}
}
} else {
// console.log("hellow, del", ride!.id);
axios
.delete(`/api/rides/${ride.id}`)
.then(() => closeModal())
Expand All @@ -60,6 +171,7 @@ const DeleteOrEditTypeModal = ({
setSingle(e.target.value === 'single');
};

//bruh the fuck is onnext?
const onButtonClick = () => {
if (deleting) {
confirmCancel();
Expand Down Expand Up @@ -119,15 +231,27 @@ const DeleteOrEditTypeModal = ({
<div>
<Input
type="radio"
id="recurring"
id="allRecurring"
name="rideType"
value="recurring"
onClick={(e) => changeSelection(e)}
value="allRecurring"
onClick={(e) => {changeSelection(e); setAllOrFollowing(true)}}
/>
<Label htmlFor="recurring" className={styles.modalText}>
<Label htmlFor="allRecurring" className={styles.modalText}>
All Repeating Rides
</Label>
</div>
<div>
<Input
type="radio"
id="followingRecurring"
name="rideType"
value="followingRecurring"
onClick={(e) => {changeSelection(e); setAllOrFollowing(false)}}
/>
<Label htmlFor="followingRecurring" className={styles.modalText}>
This and Following Rides.
</Label>
</div>
<div className={styles.buttonContainer}>
<Button outline type="button" onClick={closeModal}>
Back
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { format_date } from '../../util/index';
import { LocationsProvider } from '../../context/LocationsContext';
import axios from '../../util/axios';


type CreateOrEditRideModalProps = {
isOpen: boolean;
modalType: RideModalType;
Expand All @@ -36,7 +37,7 @@ const CreateOrEditRideModal = ({
return format_date(ride.startTime);
}
if (modalType === 'EDIT_ALL_RECURRING') {
return format_date(ride.parentRide?.startTime);
return format_date(ride.sourceRide?.startTime);
}
}
return format_date();
Expand Down Expand Up @@ -176,8 +177,8 @@ const CreateOrEditRideModal = ({
} else {
// edit regular ride or all recurring rides by editing parent ride
if (
!ride.parentRide ||
(ride.parentRide && ride.parentRide.type === 'active')
!ride.sourceRide ||
(ride.sourceRide && ride.sourceRide.type === 'active')
) {
rideData.type = 'unscheduled';
}
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/components/RequestRideModal/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export type RideModalType =
| 'CREATE'
| 'EDIT_REGULAR'
| 'EDIT_SINGLE_RECURRING'
| 'EDIT_ALL_RECURRING';
| 'EDIT_ALL_RECURRING'
| 'EDIT_THIS_AND_FOLLOWING';
2 changes: 1 addition & 1 deletion frontend/src/components/RideModal/Pages/RideTimes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ const RepeatSection: React.FC<RepeatSectionProps> = ({ repeatValue }) => {
{...register('endDate', {
required: true,
validate: (endDate: string) => {
const fmtEnd = format_date(endDate);
const fmtEnd = (endDate);
const fmtStart = format_date(getValues('date'));
const notWeekend =
moment(fmtEnd).day() !== 0 && moment(fmtEnd).day() !== 6;
Expand Down
Loading
Loading