Skip to content

Commit

Permalink
Prevent past events from being edited (#248)
Browse files Browse the repository at this point in the history
* Prevent past events from being edited.

* Update date and time fields in EventForm to use Date type instead of string

* Add backend proteection for editing of past events

* Fix logic

---------

Co-authored-by: Akinfolami Akin-Alamu <aoa9@cornell.edu>
  • Loading branch information
jasozh and akinfelami authored Jun 8, 2024
1 parent 9d442dd commit bd3c7bd
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 11 deletions.
8 changes: 8 additions & 0 deletions backend/src/events/controllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,14 @@ const getEvents = async (
* @returns promise with eventID or error.
*/
const updateEvent = async (eventID: string, event: Event) => {
const eventDetails = await getEvent(eventID);
const currentDate = new Date();
if (eventDetails) {
if (eventDetails.startDate < currentDate) {
return Promise.reject("Event has already started");
}
}

return prisma.event.update({
where: {
id: eventID,
Expand Down
40 changes: 30 additions & 10 deletions frontend/src/components/organisms/EventForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import {
import { api } from "@/utils/api";
import { useMutation, useQueryClient } from "@tanstack/react-query";
import Dropzone from "../atoms/Dropzone";
import Alert from "../atoms/Alert";
import EditorComp from "@/components/atoms/Editor";
import Modal from "../molecules/Modal";
import Alert from "../atoms/Alert";

interface EventFormProps {
eventId?: string | string[] | undefined;
Expand All @@ -43,9 +43,9 @@ type FormValues = {
eventDescription: string;
imageURL: string;
rsvpLinkImage: string;
startDate: Date;
startTime: Date;
endTime: Date;
startDate: string;
startTime: string;
endTime: string;
mode: string;
status: string;
};
Expand Down Expand Up @@ -294,8 +294,14 @@ const EventForm = ({
);
};

// Check if this event has been canceled
const thisEventHasBeenCanceled = eventDetails?.status === "CANCELED";
/** Edit event "Save changes" button should be disabled if event is in the past */
const currentDate = new Date();
const eventIsPast = eventDetails
? new Date(eventDetails?.startDate) < currentDate
: false;

/** Check if this event has been canceled */
const eventIsCanceled = eventDetails?.status === "CANCELED";

return (
<>
Expand All @@ -304,6 +310,7 @@ const EventForm = ({
handleClose={handleClose}
children={<ModalBody handleClose={handleClose} />}
/>

{/* Error component */}
<Snackbar
variety="error"
Expand All @@ -312,9 +319,22 @@ const EventForm = ({
>
Error: {errorMessage}
</Snackbar>
{thisEventHasBeenCanceled && (

{/* Alert for when the event cannot be edited because it's in the past */}
{eventType == "edit" && eventIsPast && (
<div className="pb-6">
<Alert variety="warning">
This event is in the past. You are not able to make changes.
</Alert>
</div>
)}

{/* Alert for when the event cannot be edited because it has been canceled */}
{eventIsCanceled && (
<div className="pb-6">
<Alert variety="warning">This event has been canceled.</Alert>
<Alert variety="warning">
This event has been canceled. You are not able to make changes.
</Alert>
</div>
)}

Expand Down Expand Up @@ -508,7 +528,7 @@ const EventForm = ({
<Button
variety="error"
loading={cancelEventPending}
disabled={editEventPending || thisEventHasBeenCanceled}
disabled={editEventPending || eventIsCanceled || eventIsPast}
onClick={handleOpen}
>
Cancel event
Expand All @@ -518,7 +538,7 @@ const EventForm = ({
<Button
type="submit"
loading={editEventPending}
disabled={editEventPending || thisEventHasBeenCanceled}
disabled={editEventPending || eventIsCanceled || eventIsPast}
>
Save changes
</Button>
Expand Down
5 changes: 4 additions & 1 deletion frontend/src/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,10 @@ export const formatDateString = (dateString: string) => {
* @param inputDate is the inputDate
* @returns the ISO string
*/
export const convertToISO = (inputTime: Date, inputDate: Date) => {
export const convertToISO = (
inputTime: Date | string,
inputDate: Date | string
) => {
const date = format(new Date(inputDate), "yyyy-MM-dd");
const time = format(new Date(inputTime), "HH:mm:ss");

Expand Down

0 comments on commit bd3c7bd

Please sign in to comment.