Skip to content

Commit

Permalink
fix: improve delete line from cart functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
grzegorzpokorski committed Nov 27, 2023
1 parent dbc8277 commit f77768f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 54 deletions.
28 changes: 0 additions & 28 deletions src/app/(main)/cart/DeleteLineButton.tsx

This file was deleted.

22 changes: 0 additions & 22 deletions src/app/(main)/cart/actions.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/app/(main)/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { cookies } from "next/headers";
import Image from "next/image";
import Link from "next/link";
import { CheckoutLink } from "./CheckoutLink";
import { DeleteLineButton } from "./DeleteLineButton";
import * as Checkout from "@/lib/checkout";
import { formatMoney, getHrefForVariant } from "@/lib/utils";
import { DeleteLineForm } from "@/ui/components/DeleteLineForm/DeleteLineForm";

export const metadata = {
title: "Shopping Cart · Saleor Storefront example",
Expand Down Expand Up @@ -35,7 +35,7 @@ export default async function Page() {
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 @@ -76,7 +76,7 @@ export default async function Page() {
</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 @@ -103,7 +103,7 @@ export default async function Page() {
/>
</div>
</div>
</form>
</div>
</section>
);
}
33 changes: 33 additions & 0 deletions src/ui/components/DeleteLineForm/DeleteLineForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { revalidatePath } from "next/cache";
import { SubmitButon } from "./components/SubmitButton";
import { executeGraphQL } from "@/lib/graphql";
import { CheckoutDeleteLinesDocument } from "@/gql/graphql";

type Props = {
lineId: string;
checkoutId: string;
};

export const DeleteLineForm = ({ lineId, checkoutId }: Props) => {
const deleteLineAction = async (lineId: string, checkoutId: string) => {
"use server";

await executeGraphQL(CheckoutDeleteLinesDocument, {
variables: {
checkoutId,
lineIds: [lineId],
},
cache: "no-cache",
});

revalidatePath("/cart");
};

const deleteLineActionWithArgs = deleteLineAction.bind(null, lineId, checkoutId);

return (
<form action={deleteLineActionWithArgs}>
<SubmitButon />
</form>
);
};
13 changes: 13 additions & 0 deletions src/ui/components/DeleteLineForm/components/SubmitButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use client";

import { useFormStatus } from "react-dom";

export const SubmitButon = () => {
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>
);
};

0 comments on commit f77768f

Please sign in to comment.