Skip to content

Commit

Permalink
✨ Add button to order from Item page
Browse files Browse the repository at this point in the history
  • Loading branch information
poissoj committed Jul 7, 2024
1 parent 7073193 commit f6cb9d1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 16 deletions.
10 changes: 4 additions & 6 deletions src/components/OrderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,19 @@ export const OrderForm = ({
}: {
title: string;
onSubmit(data?: Order): Promise<{ type: "error" | "success"; msg: string }>;
data?: Order;
data: Partial<Order> & { date: Order["date"] };
children: React.ReactNode;
}): JSX.Element => {
const defaultValues = data
? { ...data, date: toInputDate(data.date) }
: { date: toInputDate(new Date()), nb: 1 };
const defaultValues = { ...data, date: toInputDate(data.date) };
const { register, handleSubmit, reset, setValue } = useForm<InputOrder>({
defaultValues,
shouldUseNativeValidation: true,
});
const [customer, setCustomer] = React.useState<Customer | null>(
data?.customer ?? null,
data.customer ?? null,
);
const [item, setItem] = React.useState<Item | NewItem | null>(
data?.item ?? { _id: null, title: data?.itemTitle ?? "" },
data.item ?? { _id: null, title: data.itemTitle ?? "" },
);
const utils = trpc.useUtils();

Expand Down
10 changes: 9 additions & 1 deletion src/pages/item/[id].tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faStar as emptyStar } from "@fortawesome/free-regular-svg-icons";
import {
faBook,
faCartPlus,
faEdit,
faSpinner,
Expand Down Expand Up @@ -132,11 +133,18 @@ const TitleWithButtons = ({ item }: { item: ItemWithCount }) => {
return (
<div className="flex items-center">
<CardTitle className="mr-auto">{item.title}</CardTitle>
<LinkButton
href={`/order/new?item=${item._id}`}
title="Commander"
className="rounded-r-none px-md"
>
<FontAwesomeIcon icon={faBook} />
</LinkButton>
<Button
type="button"
title={item.starred ? "Enlever des favoris" : "Ajouter aux favoris"}
onClick={handleClick}
className="rounded-r-none px-md border-r border-primary-darkest"
className="rounded-none px-md border-primary-darkest"
>
<FontAwesomeIcon
icon={
Expand Down
2 changes: 1 addition & 1 deletion src/pages/order/[orderId].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ const UpdateOrder = () => {
return null;
}
return (
<div className="2xl:([margin-left:10%] [margin-right:10%]) flex-1">
<div className="flex-1 max-w-6xl mx-auto">
<Title>Modifier une commande</Title>
<OrderLoader id={orderId} />
</div>
Expand Down
38 changes: 30 additions & 8 deletions src/pages/order/new.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@ import { useRouter } from "next/router";
import { toast } from "react-toastify";

import { Button } from "@/components/Button";
import { ErrorMessage } from "@/components/ErrorMessage";
import { OrderForm } from "@/components/OrderForm";
import { Title } from "@/components/Title";
import type { Order } from "@/utils/order";
import { trpc } from "@/utils/trpc";

const NewOrder = (): JSX.Element => {
const OrderBody = () => {
const router = useRouter();
const rawId = router.query.item;
const id = typeof rawId === "string" ? rawId : "";
const result = trpc.searchItem.useQuery(id, { enabled: id !== "" });
const mutation = trpc.newOrder.useMutation({
onSuccess(data) {
if (data.type === "success") {
Expand All @@ -25,19 +29,37 @@ const NewOrder = (): JSX.Element => {
toast.error("Impossible d'ajouter la commande");
},
});

const submit = async (data: Order) => {
const order = { ...data, date: data.date.toISOString() };
return await mutation.mutateAsync(order);
};
if (result.status === "error") {
return <ErrorMessage />;
}
if (result.status === "loading" && id) {
return <div>Chargement…</div>;
}
const data: React.ComponentProps<typeof OrderForm>["data"] = {
item: result.data || null,
nb: 1,
date: new Date(),
};
return (
<OrderForm title="Nouvelle commande" onSubmit={submit} data={data}>
<Button type="submit" className="px-md">
<FontAwesomeIcon icon={faPlus} className="mr-sm" />
Ajouter
</Button>
</OrderForm>
);
};

const NewOrder = (): JSX.Element => {
return (
<div className="[margin-left:10%] [margin-right:10%] flex-1">
<div className="flex-1 max-w-6xl mx-auto">
<Title>Nouvelle commande</Title>
<OrderForm title="Nouvelle commande" onSubmit={submit}>
<Button type="submit" className="px-md">
<FontAwesomeIcon icon={faPlus} className="mr-sm" />
Ajouter
</Button>
</OrderForm>
<OrderBody />
</div>
);
};
Expand Down

0 comments on commit f6cb9d1

Please sign in to comment.