diff --git a/frontend/src/pages/NewListing.tsx b/frontend/src/pages/NewListing.tsx index 706c97c3..af8f550c 100644 --- a/frontend/src/pages/NewListing.tsx +++ b/frontend/src/pages/NewListing.tsx @@ -1,73 +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"; -interface ListingFormProps { - title: string; - name: string; - setName: Dispatch>; - description: string; - setDescription: Dispatch>; - handleSubmit: (event: FormEvent) => void; -} - -const ListingForm = (props: ListingFormProps) => { - const { title, name, setName, description, setDescription, handleSubmit } = - props; - - return ( - <> -

{title}

-
- {/* Name */} - - - { - setName(e.target.value); - }} - value={name} - required - /> - - - {/* Description */} - - - { - setDescription(e.target.value); - }} - value={description} - /> - - - {/* Submit */} - - Submit - - - - ); -}; +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) => { @@ -90,14 +79,85 @@ const NewListing = () => { }; return ( - + <> +

Create Listing

+
+ {/* Name */} + + { + setName(e.target.value); + }} + value={name} + required + /> + + {/* 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 */} + + Submit + + + ); };