diff --git a/src/app/[channel]/(main)/cart/DeleteLineButton.tsx b/src/app/[channel]/(main)/cart/DeleteLineButton.tsx deleted file mode 100644 index 84647e887..000000000 --- a/src/app/[channel]/(main)/cart/DeleteLineButton.tsx +++ /dev/null @@ -1,28 +0,0 @@ -"use client"; - -import { useTransition } from "react"; -import { deleteLineFromCheckout } from "./actions"; - -type Props = { - lineId: string; - checkoutId: string; -}; - -export const DeleteLineButton = ({ lineId, checkoutId }: Props) => { - const [isPending, startTransition] = useTransition(); - - return ( - - ); -}; diff --git a/src/app/[channel]/(main)/cart/DeleteLineForm.tsx b/src/app/[channel]/(main)/cart/DeleteLineForm.tsx new file mode 100644 index 000000000..cb410852f --- /dev/null +++ b/src/app/[channel]/(main)/cart/DeleteLineForm.tsx @@ -0,0 +1,17 @@ +import { SubmitButton } from "./SubmitButton"; +import { deleteLineFromCheckout } from "./actions"; + +type Props = { + lineId: string; + checkoutId: string; +}; + +export const DeleteLineForm = ({ lineId, checkoutId }: Props) => { + return ( +
+ ); +}; diff --git a/src/app/[channel]/(main)/cart/SubmitButton.tsx b/src/app/[channel]/(main)/cart/SubmitButton.tsx new file mode 100644 index 000000000..249319827 --- /dev/null +++ b/src/app/[channel]/(main)/cart/SubmitButton.tsx @@ -0,0 +1,14 @@ +"use client"; + +import { useFormStatus } from "react-dom"; + +export const SubmitButton = () => { + const { pending } = useFormStatus(); + + return ( + + ); +}; diff --git a/src/app/[channel]/(main)/cart/actions.ts b/src/app/[channel]/(main)/cart/actions.ts index f1bc68fe1..beac8e5dc 100644 --- a/src/app/[channel]/(main)/cart/actions.ts +++ b/src/app/[channel]/(main)/cart/actions.ts @@ -4,19 +4,25 @@ import { revalidatePath } from "next/cache"; import { executeGraphQL } from "@/lib/graphql"; import { CheckoutDeleteLinesDocument } from "@/gql/graphql"; -type deleteLineFromCheckoutArgs = { - lineId: string; - checkoutId: string; -}; +export const deleteLineFromCheckout = async (formData: FormData) => { + const lineId = formData.get("lineId")?.toString(); + const checkoutId = formData.get("checkoutId")?.toString(); + + try { + if (!checkoutId || !lineId) { + throw Error("Missing `lineId and/or `checkoutId`."); + } -export const deleteLineFromCheckout = async ({ lineId, checkoutId }: deleteLineFromCheckoutArgs) => { - await executeGraphQL(CheckoutDeleteLinesDocument, { - variables: { - checkoutId, - lineIds: [lineId], - }, - cache: "no-cache", - }); + await executeGraphQL(CheckoutDeleteLinesDocument, { + variables: { + checkoutId, + lineIds: [lineId], + }, + cache: "no-cache", + }); + } catch (e) { + // TODO: handle erorrs + } revalidatePath("/cart"); }; diff --git a/src/app/[channel]/(main)/cart/page.tsx b/src/app/[channel]/(main)/cart/page.tsx index 17fcc086d..a516b91ec 100644 --- a/src/app/[channel]/(main)/cart/page.tsx +++ b/src/app/[channel]/(main)/cart/page.tsx @@ -1,6 +1,6 @@ import Image from "next/image"; import { CheckoutLink } from "./CheckoutLink"; -import { DeleteLineButton } from "./DeleteLineButton"; +import { DeleteLineForm } from "./DeleteLineForm"; import * as Checkout from "@/lib/checkout"; import { formatMoney, getHrefForVariant } from "@/lib/utils"; import { LinkWithChannel } from "@/ui/atoms/LinkWithChannel"; @@ -34,7 +34,7 @@ export default async function Page({ params }: { params: { channel: string } }) return (