From 2df26cabf1c01fa1943fe7609639e85172209cbe Mon Sep 17 00:00:00 2001 From: Dennis Chen Date: Wed, 31 Jul 2024 11:28:59 -0700 Subject: [PATCH] Re-introduce ability to add children to listings --- frontend/src/pages/NewListing.tsx | 139 ++++++++++++++++++++++++------ 1 file changed, 112 insertions(+), 27 deletions(-) diff --git a/frontend/src/pages/NewListing.tsx b/frontend/src/pages/NewListing.tsx index 3c66b959..af8f550c 100644 --- a/frontend/src/pages/NewListing.tsx +++ b/frontend/src/pages/NewListing.tsx @@ -1,18 +1,62 @@ import TCButton from "components/files/TCButton"; +import { paths } from "gen/api"; import { useAlertQueue } from "hooks/alerts"; import { useAuthentication } from "hooks/auth"; -import { Dispatch, FormEvent, SetStateAction, useState } from "react"; -import { Col, Form } from "react-bootstrap"; +import { useTheme } from "hooks/theme"; +import { ChangeEvent, FormEvent, useEffect, useState } from "react"; +import { Col, Form, Row } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; +type ListingDumpResponse = + paths["/listings/dump"]["get"]["responses"][200]["content"]["application/json"]; + const NewListing = () => { const auth = useAuthentication(); const [name, setName] = useState(""); const [description, setDescription] = useState(""); + const [listings, setListings] = useState(null); + const [children, setChildren] = useState([]); // Store the ids of each child const { addAlert, addErrorAlert } = useAlertQueue(); + const { theme } = useTheme(); const navigate = useNavigate(); + const handleChildrenChange = ( + index: number, + e: ChangeEvent, + ) => { + const { value } = e.target; + const newChildren = [...children]; + newChildren[index] = value; + setChildren(newChildren); + }; + + const handleAddChild = () => { + setChildren([...children, ""]); + }; + + const handleRemoveChild = (index: number) => { + const newChildren = children.filter((_, i) => i !== index); + setChildren(newChildren); + }; + + // Fetch all listings to use for children. + useEffect(() => { + const fetchListings = async () => { + try { + const { data, error } = await auth.client.GET("/listings/dump"); + if (error) { + addErrorAlert(error); + } else { + setListings(data); + } + } catch (err) { + addErrorAlert(err); + } + }; + fetchListings(); + }, []); + // On submit, add the listing to the database and navigate to the // newly-created listing. const handleSubmit = async (event: FormEvent) => { @@ -39,33 +83,74 @@ const NewListing = () => {

Create Listing

{/* Name */} - - - { - setName(e.target.value); - }} - value={name} - required - /> - + + { + setName(e.target.value); + }} + value={name} + required + /> {/* Description */} - - - { - setDescription(e.target.value); - }} - value={description} - /> - + + { + setDescription(e.target.value); + }} + value={description} + /> + +

Children

+ {children.map((id, index) => ( + + + + handleChildrenChange(index, e)} + required + > + + {listings && + listings.listings.map((listing, index) => ( + + ))} + + + + handleRemoveChild(index)} + > + Remove + + + + ))} + + + Add Child + {/* Submit */}