Skip to content

Commit

Permalink
Remove unnecessary layer of indirection
Browse files Browse the repository at this point in the history
  • Loading branch information
chennisden committed Jul 31, 2024
1 parent b2975db commit 3b25180
Showing 1 changed file with 28 additions and 53 deletions.
81 changes: 28 additions & 53 deletions frontend/src/pages/NewListing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,38 @@ import { Dispatch, FormEvent, SetStateAction, useState } from "react";
import { Col, Form } from "react-bootstrap";
import { useNavigate } from "react-router-dom";

interface ListingFormProps {
title: string;
name: string;
setName: Dispatch<SetStateAction<string>>;
description: string;
setDescription: Dispatch<SetStateAction<string>>;
handleSubmit: (event: FormEvent<HTMLFormElement>) => void;
}
const NewListing = () => {
const auth = useAuthentication();
const [name, setName] = useState<string>("");
const [description, setDescription] = useState<string>("");

const { addAlert, addErrorAlert } = useAlertQueue();
const navigate = useNavigate();

// On submit, add the listing to the database and navigate to the
// newly-created listing.
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const { data, error } = await auth.client.POST("/listings/add", {
body: {
name,
description,
child_ids: [],
},
});

const ListingForm = (props: ListingFormProps) => {
const { title, name, setName, description, setDescription, handleSubmit } =
props;
if (error) {
addErrorAlert(error);
} else {
addAlert("Listing added successfully", "success");
navigate(`/listing/${data.listing_id}`);
}
};

return (
<>
<h1>{title}</h1>
<h1 className="display-6">Create Listing</h1>
<Form onSubmit={handleSubmit} className="mb-3">
{/* Name */}
<Col md={12} className="mb-4">
Expand Down Expand Up @@ -60,45 +76,4 @@ const ListingForm = (props: ListingFormProps) => {
);
};

const NewListing = () => {
const auth = useAuthentication();
const [name, setName] = useState<string>("");
const [description, setDescription] = useState<string>("");

const { addAlert, addErrorAlert } = useAlertQueue();
const navigate = useNavigate();

// On submit, add the listing to the database and navigate to the
// newly-created listing.
const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
event.preventDefault();

const { data, error } = await auth.client.POST("/listings/add", {
body: {
name,
description,
child_ids: [],
},
});

if (error) {
addErrorAlert(error);
} else {
addAlert("Listing added successfully", "success");
navigate(`/listing/${data.listing_id}`);
}
};

return (
<ListingForm
title="Create Listing"
name={name}
setName={setName}
description={description}
setDescription={setDescription}
handleSubmit={handleSubmit}
/>
);
};

export default NewListing;

0 comments on commit 3b25180

Please sign in to comment.