File tree Expand file tree Collapse file tree 5 files changed +53
-44
lines changed
src/app/[channel]/(main)/cart Expand file tree Collapse file tree 5 files changed +53
-44
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change @@ -4,19 +4,25 @@ import { revalidatePath } from "next/cache";
44import { executeGraphQL } from "@/lib/graphql" ;
55import { 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} ;
Original file line number Diff line number Diff line change 11import Image from "next/image" ;
22import { CheckoutLink } from "./CheckoutLink" ;
3- import { DeleteLineButton } from "./DeleteLineButton " ;
3+ import { DeleteLineForm } from "./DeleteLineForm " ;
44import * as Checkout from "@/lib/checkout" ;
55import { formatMoney , getHrefForVariant } from "@/lib/utils" ;
66import { 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}
You can’t perform that action at this time.
0 commit comments