Skip to content

Commit eddfbf9

Browse files
refactor: make changes in existing files in place instead separate and move to the components in src/ui directory
1 parent 72ac8e6 commit eddfbf9

File tree

5 files changed

+53
-44
lines changed

5 files changed

+53
-44
lines changed

src/app/[channel]/(main)/cart/DeleteLineButton.tsx

Lines changed: 0 additions & 28 deletions
This file was deleted.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { SubmitButton } from "./SubmitButton";
2+
import { deleteLineFromCheckout } from "./actions";
3+
4+
type Props = {
5+
lineId: string;
6+
checkoutId: string;
7+
};
8+
9+
export const DeleteLineForm = ({ lineId, checkoutId }: Props) => {
10+
return (
11+
<form action={deleteLineFromCheckout}>
12+
<input type="hidden" name="checkoutId" value={checkoutId} />
13+
<input type="hidden" name="lineId" value={lineId} />
14+
<SubmitButton />
15+
</form>
16+
);
17+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"use client";
2+
3+
import { useFormStatus } from "react-dom";
4+
5+
export const SubmitButton = () => {
6+
const { pending } = useFormStatus();
7+
8+
return (
9+
<button type="submit" className="text-sm text-neutral-500 hover:text-neutral-900" aria-disabled={pending}>
10+
{pending ? "Removing" : "Remove"}
11+
<span className="sr-only">line from cart</span>
12+
</button>
13+
);
14+
};

src/app/[channel]/(main)/cart/actions.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ import { revalidatePath } from "next/cache";
44
import { executeGraphQL } from "@/lib/graphql";
55
import { CheckoutDeleteLinesDocument } from "@/gql/graphql";
66

7-
type deleteLineFromCheckoutArgs = {
8-
lineId: string;
9-
checkoutId: string;
10-
};
7+
export const deleteLineFromCheckout = async (formData: FormData) => {
8+
const lineId = formData.get("lineId")?.toString();
9+
const checkoutId = formData.get("checkoutId")?.toString();
10+
11+
try {
12+
if (!checkoutId || !lineId) {
13+
throw Error("Missing `lineId and/or `checkoutId`.");
14+
}
1115

12-
export const deleteLineFromCheckout = async ({ lineId, checkoutId }: deleteLineFromCheckoutArgs) => {
13-
await executeGraphQL(CheckoutDeleteLinesDocument, {
14-
variables: {
15-
checkoutId,
16-
lineIds: [lineId],
17-
},
18-
cache: "no-cache",
19-
});
16+
await executeGraphQL(CheckoutDeleteLinesDocument, {
17+
variables: {
18+
checkoutId,
19+
lineIds: [lineId],
20+
},
21+
cache: "no-cache",
22+
});
23+
} catch (e) {
24+
// TODO: handle erorrs
25+
}
2026

2127
revalidatePath("/cart");
2228
};

src/app/[channel]/(main)/cart/page.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Image from "next/image";
22
import { CheckoutLink } from "./CheckoutLink";
3-
import { DeleteLineButton } from "./DeleteLineButton";
3+
import { DeleteLineForm } from "./DeleteLineForm";
44
import * as Checkout from "@/lib/checkout";
55
import { formatMoney, getHrefForVariant } from "@/lib/utils";
66
import { LinkWithChannel } from "@/ui/atoms/LinkWithChannel";
@@ -34,7 +34,7 @@ export default async function Page({ params }: { params: { channel: string } })
3434
return (
3535
<section className="mx-auto max-w-7xl p-8">
3636
<h1 className="mt-8 text-3xl font-bold text-neutral-900">Your Shopping Cart</h1>
37-
<form className="mt-12">
37+
<div className="mt-12">
3838
<ul
3939
data-testid="CartProductList"
4040
role="list"
@@ -75,7 +75,7 @@ export default async function Page({ params }: { params: { channel: string } })
7575
</div>
7676
<div className="flex justify-between">
7777
<div className="text-sm font-bold">Qty: {item.quantity}</div>
78-
<DeleteLineButton checkoutId={checkoutId} lineId={item.id} />
78+
<DeleteLineForm checkoutId={checkoutId} lineId={item.id} />
7979
</div>
8080
</div>
8181
</li>
@@ -102,7 +102,7 @@ export default async function Page({ params }: { params: { channel: string } })
102102
/>
103103
</div>
104104
</div>
105-
</form>
105+
</div>
106106
</section>
107107
);
108108
}

0 commit comments

Comments
 (0)