From 84b1c9c415024734a7950eabbfcf4d67f062cd15 Mon Sep 17 00:00:00 2001 From: Benson Cho <100653148+bcho892@users.noreply.github.com> Date: Sat, 2 Nov 2024 13:37:42 +1300 Subject: [PATCH] allow form to be used for both editing and creating events (#815) * allow form to be used for both editing and creating * add tests * fix paddingg on cards --- .../AdminEventForm/AdminEventForm.story.tsx | 19 +++++ .../AdminEventForm/AdminEventForm.tsx | 77 +++++++++++++++++-- .../Event/EventPreview/EventPreview.tsx | 2 +- .../generic/Event/EventUtils.test.ts | 33 ++++++++ .../components/generic/Event/EventUtils.ts | 25 ++++++ 5 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 client/src/components/generic/Event/EventUtils.test.ts diff --git a/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.story.tsx b/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.story.tsx index 19fecbb3..1bca8540 100644 --- a/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.story.tsx +++ b/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.story.tsx @@ -1,5 +1,6 @@ import type { Meta, StoryObj } from "@storybook/react" import AdminEventForm from "./AdminEventForm" +import { Timestamp } from "firebase/firestore" const meta: Meta = { component: AdminEventForm @@ -9,3 +10,21 @@ export default meta type Story = StoryObj export const DefaultAdminEventForm: Story = {} + +export const DefaultAdminEventFormEditView: Story = { + args: { + isEditMode: true, + defaultData: { + title: "Goon Cave", + description: + "Lorem, ipsum dolor sit amet consectetur adipisicing elit. Eaque officiis similique repellat accusantium eos dignissimos suscipit voluptatibus? Quia nobis repellendus quam, aliquid, veritatis eos fuga eligendi harum dolorum vero facere!", + location: "straight zhao's basement", + sign_up_start_date: Timestamp.fromDate(new Date(2024, 10, 4)), + sign_up_end_date: Timestamp.fromDate(new Date(2024, 11, 5)), + physical_start_date: Timestamp.fromDate(new Date(2024, 10, 3)), + physical_end_date: Timestamp.fromDate(new Date(2024, 11, 3)), + google_forms_link: + "https://www.reddit.com/r/VrGoonCave/comments/18gnrk8/welcome_to_vrgooncave/" + } + } +} diff --git a/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.tsx b/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.tsx index 51428b03..09338135 100644 --- a/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.tsx +++ b/client/src/components/composite/Admin/AdminEventView/AdminEventForm/AdminEventForm.tsx @@ -1,9 +1,14 @@ +import { + EventMessages, + EventRenderingUtils +} from "@/components/generic/Event/EventUtils" import Button from "@/components/generic/FigmaButtons/FigmaButton" import TextInput from "@/components/generic/TextInputComponent/TextInput" +import { DateUtils } from "@/components/utils/DateUtils" import { CreateEventBody } from "@/models/Events" import { Timestamp } from "firebase/firestore" import Image from "next/image" -import { FormEvent, useState } from "react" +import { FormEvent, useMemo, useState } from "react" interface IAdminEventForm { /** @@ -23,6 +28,18 @@ interface IAdminEventForm { * To be called after user submits the new data for the event */ handlePostEvent: (data: CreateEventBody) => void + /** + * If the panel should suggest that the event is being edited, instead of created + * + * Does the create mode if set to `false` + */ + isEditMode?: boolean + /** + * What to pre-fill the form with - _generally_ only to be used + * if {@link isEditMode} is set to true (which would have the previous + * values of the event.) + */ + defaultData?: CreateEventBody["data"] } export const AdminEventFormKeys = { @@ -42,12 +59,20 @@ const Divider = () => const AdminEventForm = ({ handlePostEvent, - generateImageLink + generateImageLink, + isEditMode = false, + defaultData }: IAdminEventForm) => { const [isSubmitting, setIsSubmitting] = useState(false) const [uploadedImage, setUploadedImage] = useState() + const formTitle = useMemo( + () => + isEditMode ? `Edit Event ${defaultData?.title || ""}` : "Create Event", + [isEditMode, defaultData] + ) + const handleSubmit = async (e: FormEvent) => { e.preventDefault() const data = new FormData(e.currentTarget) @@ -86,7 +111,7 @@ const AdminEventForm = ({ if ( confirm( - `Are you sure you want to create the new event with title ${body.title}?` + EventMessages.adminEventPostConfirmation(!!isEditMode, body.title) ) ) { handlePostEvent({ @@ -102,7 +127,7 @@ const AdminEventForm = ({ } return (
-

Create Event

+

{formTitle}