Skip to content

Commit

Permalink
refactor: make changes in existing files in place instead separate an…
Browse files Browse the repository at this point in the history
…d move to the components in src/ui directory
  • Loading branch information
grzegorzpokorski committed Nov 28, 2023
1 parent 57dfc2c commit 92625e3
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 44 deletions.
28 changes: 0 additions & 28 deletions src/app/[channel]/(main)/cart/DeleteLineButton.tsx

This file was deleted.

17 changes: 17 additions & 0 deletions src/app/[channel]/(main)/cart/DeleteLineForm.tsx
Original file line number Diff line number Diff line change
@@ -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 (
<form action={deleteLineFromCheckout}>
<input type="hidden" name="checkoutId" value={checkoutId} />
<input type="hidden" name="lineId" value={lineId} />
<SubmitButton />
</form>
);
};
14 changes: 14 additions & 0 deletions src/app/[channel]/(main)/cart/SubmitButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
"use client";

import { useFormStatus } from "react-dom";

export const SubmitButton = () => {
const { pending } = useFormStatus();

return (
<button type="submit" className="text-sm text-neutral-500 hover:text-neutral-900" aria-disabled={pending}>
{pending ? "Removing" : "Remove"}
<span className="sr-only">line from cart</span>
</button>
);
};
30 changes: 18 additions & 12 deletions src/app/[channel]/(main)/cart/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
};
8 changes: 4 additions & 4 deletions src/app/[channel]/(main)/cart/page.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -34,7 +34,7 @@ export default async function Page({ params }: { params: { channel: string } })
return (
<section className="mx-auto max-w-7xl p-8">
<h1 className="mt-8 text-3xl font-bold text-neutral-900">Your Shopping Cart</h1>
<form className="mt-12">
<div className="mt-12">
<ul
data-testid="CartProductList"
role="list"
Expand Down Expand Up @@ -75,7 +75,7 @@ export default async function Page({ params }: { params: { channel: string } })
</div>
<div className="flex justify-between">
<div className="text-sm font-bold">Qty: {item.quantity}</div>
<DeleteLineButton checkoutId={checkoutId} lineId={item.id} />
<DeleteLineForm checkoutId={checkoutId} lineId={item.id} />
</div>
</div>
</li>
Expand All @@ -102,7 +102,7 @@ export default async function Page({ params }: { params: { channel: string } })
/>
</div>
</div>
</form>
</div>
</section>
);
}

0 comments on commit 92625e3

Please sign in to comment.