Skip to content

Commit

Permalink
implemented add and delete recurring rides on the admin side, now nee…
Browse files Browse the repository at this point in the history
…d to fix bug when axios.put
  • Loading branch information
namanhboi committed Nov 5, 2024
1 parent 936da4f commit 830220f
Show file tree
Hide file tree
Showing 6 changed files with 146 additions and 161 deletions.
55 changes: 41 additions & 14 deletions frontend/src/components/Modal/DeleteOrEditTypeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,25 @@ const DeleteOrEditTypeModal = ({
* 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.
*/
//need to look at sourceride not ride.
const originalEndDate = new Date(ride.sourceRide!.endDate!);

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

axios.put(`/api/rides/${ride.id}`, {...ride.sourceRide!, endDate : newEndDate.toISOString()});
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.
Expand All @@ -79,17 +91,19 @@ const DeleteOrEditTypeModal = ({
.post('/api/rides', newChildRide)
.then((response) => response.data)
.then((rideData) => {
axios.put(`/api/rides/${ride.id}`, {...ride.sourceRide!, childrenId: rideData.id});
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) {
axios.put(`/api/rides/${ride.sourceRide!.childRideId}`, {...ride.sourceRide!.childRide, parentRideId : rideData.id});
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) {
/**
Expand All @@ -99,22 +113,35 @@ const DeleteOrEditTypeModal = ({

//traverse to the ancestor and delete all rides along the way.
let currentRide = ride.sourceRide;
while (currentRide !== undefined) {
axios
.delete(`/api/rides/${currentRide.id}`)
while (currentRide?.parentRide !== undefined) {
currentRide = currentRide.parentRide;
}
// now current ride is at the beginning of linked list
while (currentRide !== undefined) {
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 trimmedEndDateImmPar = curDate;
trimmedEndDateImmPar.setDate(trimmedEndDateImmPar.getDate() - 1);
axios.put(`/api/rides/${ride.sourceRide}`, {...ride.sourceRide, endDate : trimmedEndDateImmPar.toISOString()});

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
Expand Down
177 changes: 100 additions & 77 deletions frontend/src/components/RideModal/RideModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,8 @@ const getRideData = (ride: Ride | undefined) => {
repeats,
days,
endDate: format_date(ride.endDate),
parentRide : ride.parentRide,
parentRideId : ride.parentRideId,
sourceRide : ride.sourceRide,
sourceRideId : ride.sourceRide,
childRide: ride.childRide,
childRideId : ride.childRideId
};
}
Expand Down Expand Up @@ -161,7 +158,6 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
setIsOpen(true);
};

// console.log("HELLO MATEW");
const closeModal = useCallback(() => {
if (close) {
setFormData(originalRideData);
Expand Down Expand Up @@ -218,12 +214,8 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
rider,
startLocation,
endLocation,
immediateParentRide,
immediateParentRideId,
sourceRide,
sourceRideId,
children,
childrenId,
parentRideId,
childRideId,
} = formData;

const startTime = moment(`${date} ${pickupTime}`).toISOString();
Expand All @@ -238,12 +230,8 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
rider,
startLocation,
endLocation,
immediateParentRide,
immediateParentRideId,
sourceRide,
sourceRideId,
children,
childrenId,
parentRideId,
childRideId
};


Expand All @@ -267,114 +255,148 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
if (ride.recurring) {
if (editAddType == EditAddRideType.ALL) {
//need to show them the data of the sourceRide to edit.
//have to ask desmond
/**
* go up the chain to the ancestor ride
* delete all children by going down the linked list
* apply edit to the ancestor.
*/
let currentRide = ride.sourceRide!;
while (currentRide.parentRide !== undefined) {
axios.delete(`/api/rides/${currentRide.id}`);
let currentRide = ride.sourceRide;
while (currentRide?.parentRide !== undefined) {
currentRide = currentRide.parentRide;
}
//now currentRides is at the beginning of linked list
currentRide = currentRide?.childRide;
while (currentRide !== undefined) {
axios.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.childRide;
}
//now all elements except the beginning has been deleted.

axios
.put(`/api/rides/${ride.id}`, rideData)
.then(refreshRides);
} else if (editAddType == EditAddRideType.THIS_AND_FOLLOWING) {
//need to show them the data of the current ride, so don't need to change anything.
/**
* Note: lock the day and recurring (recurring must be false).
* trim its end date: change its end date then axios put with id and with sourceRide end date changed.
* Create a new ride on that day, have the ride above as its parent and parentId, axios.post. Also axios put the parent ride above to have children as this new ride.
* After this, add another recurring ride with the original ride end date and information (if the end date is larger than today).
* Link this ride to the single non recurring ride you just created: axios put appropriate parent and children field.
*/

// lock the start date (date)
// trim the date of this ride, axios.put
// create a new ride, do whatever, can be a single non repeating ride.
// should not link back to parent (gcal does this)


//delete ride if enddate is before startdate. also have to axios put children's parent to undefined.
let trimmedEndDateImmPar = curDate;
trimmedEndDateImmPar.setDate(trimmedEndDateImmPar.getDate() - 1);
trimmedEndDateImmPar.setHours(0, 0, 0);

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

if (trimmedEndDateImmPar >= sourceRideStartDate) {
const {id, parentRide, childRide, sourceRide, ...sourceRidewithoutRideTypes} = ride.sourceRide!;

axios.put(`/api/rides/${ride.id}`, {
...ride.sourceRide,
...sourceRidewithoutRideTypes,
endDate: trimmedEndDateImmPar.toISOString(),
});
else {
axios
.delete(`/api/rides/${ride.id}`)
.then((response) => {
axios.put(`/api/rides/${ride.childRideId}`, {
...ride.childRide,
parentRideId : undefined
});
});


console.log(sourceRideStartDate);
ride.sourceRide! = {...ride.sourceRide!, endDate: trimmedEndDateImmPar.toISOString()}
} else {
axios.delete(`/api/rides/${ride.id}`)
}


//delete all children ride
let currentRide = ride.immediateParentRide!.children;
//delete all descendants ride of sourceRide.
let currentRide = ride.sourceRide?.childRide;
while (currentRide !== undefined) {
axios.delete(`/api/rides/${currentRide.id}`);
currentRide = currentRide.children;
currentRide = currentRide.childRide;
}
//now we create a new ride starting from today to with the data in formData, but have it link back to the parent ride and the source ride.
// we also have to update the parentRide to have this ride as its children.
//now we create a new ride starting from today to with the data in formData, but have it doesn't link back
rideData = {
...rideData,
immediateParentRide : ride.immediateParentRide,
immediateParentRideId : ride.immediateParentRideId,
sourceRide : ride.sourceRide,
sourceRideId : ride.sourceRideId,
children : undefined,
childrenId : undefined
parentRideId : undefined,
childRideId : undefined
}

//adds the new ride to the database and updates the parent ride to have it as the child.
axios
.post(`/api/rides`, rideData)
.then((response) => response.data)

.then(data => axios.put(`/api/rides/${ride.immediateParentRideId}`, {
...ride.immediateParentRide,
children: data,
childrenId : data.id
}))
.then(refreshRides);
} else {
//edit single ride, meaning that we should also lock the recurring to false, can't repeat.
//otherwise, you can do a thing where you add the new recurring ride and then another ride to continoue
//where that ride ended and resume with the parent ride's data. IDK yet, gotta ask desmond
/**
* Note: should also lock the date for simplicity.
* trim end date of immediate parent to before this day.
* add a new ride with the edited data on that date with immediateParent and immediateParentId being from the immediate parent.
* The children of the immediate parent is this ride.
* create a new recurring ride on the date + 1 with data similar to parent if the parent’s endTime allow for it. Add the id of the parent ride as the id of the newly created ride.
* refreshRides
*/
//dont need to lock the date.

// Note: lock the day and recurring (recurring must be false).
// trim its end date: change its end date then axios put with id and with sourceRide end date changed.
// Create a new ride on that day, have the ride above as its parent and parentId, axios.post. Also axios put the parent ride above to have children as this new ride.
// After this, add another recurring ride with the original ride end date and information (if the end date is larger than today).
// Link this ride to the single non recurring ride you just created: axios put appropriate parent and children field.

let trimmedEndDateImmPar = curDate;
trimmedEndDateImmPar.setDate(trimmedEndDateImmPar.getDate() - 1);
axios.put(`/api/rides/${ride.immediateParentRideId}`, {
...ride.immediateParentRide,
endDate: trimmedEndDateImmPar.toISOString(),
});
trimmedEndDateImmPar.setDate(trimmedEndDateImmPar.getDate() - 1);
trimmedEndDateImmPar.setHours(0, 0, 0);

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


if (trimmedEndDateImmPar >= sourceRideStartDate) {
const {id, parentRide, childRide, sourceRide, ...sourceRidewithoutRideTypes} = ride.sourceRide!;
console.log(sourceRidewithoutRideTypes);

axios.put(`/api/rides/${ride.id}`, {
...sourceRidewithoutRideTypes,
endDate: trimmedEndDateImmPar.toISOString(),
});
ride.sourceRide! = {...ride.sourceRide!, endDate: trimmedEndDateImmPar.toISOString()}
deletedSourceRide = true;
} else {
axios.delete(`/api/rides/${ride.id}`)
}

// here i create a single ride linking back to the trimmed parent ride. it is a one off ride and should not be recurring. Should prob change/remove recurring days.
rideData = {
...rideData,
immediateParentRideId : ride.immediateParentRideId,
sourceRideId : ride.sourceRideId,
parentRideId : deletedSourceRide ? undefined: ride.id,
sourceRideId : undefined,
recurring : false,
recurringDays: undefined,
endDate : undefined,
id : undefined
};
axios.post(`/api/rides/`, rideData);
//here, we add the new ride with the changed data and then add
//another recurring ride after with the same data as sourceRide

const originalEndDate = new Date(ride.sourceRide!.endDate!);

axios
.post(`/api/rides/`, rideData)
.then((response) => response.data)
.then((data) => {
console.log(originalEndDate);
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);


const {id, parentRide, childRide, sourceRide, ...sourceRidewithoutRideTypes} = ride.sourceRide!;

axios.post(`/api/rides/`, {
...sourceRidewithoutRideTypes,
startTime : newRideStartTime.toISOString(),
endTime : newRideEndTime.toISOString(),
endDate : format_date(originalEndDate),
parentRideId : data.id,
childRideId: ride.sourceRide!.childRideId,
recurring : true,
type : 'unscheduled'
});
}
});

}
} else {
Expand Down Expand Up @@ -413,6 +435,7 @@ const RideModal = ({ open, close, ride }: RideModalProps) => {
<EditRecurring
onSubmit={(e) => {
setEditAddType(e);
goNextPage();
}}
/>
<RideTimesPage
Expand Down
Loading

0 comments on commit 830220f

Please sign in to comment.