-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add loading state on item quantity change
- Loading branch information
Showing
12 changed files
with
274 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@bigcommerce/catalyst-core": patch | ||
--- | ||
|
||
add loading state on item quantity update and remove when quantity equals 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ test-results/ | |
playwright-report/ | ||
playwright/.cache/ | ||
graphql-env.d.ts | ||
.DS_Store |
47 changes: 47 additions & 0 deletions
47
apps/core/app/[locale]/(default)/cart/_actions/remove-item.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
'use server'; | ||
|
||
import { revalidateTag } from 'next/cache'; | ||
import { cookies } from 'next/headers'; | ||
|
||
import { graphql } from '~/client/graphql'; | ||
import { deleteCartLineItem } from '~/client/mutations/delete-cart-line-item'; | ||
|
||
type DeleteCartLineItemInput = ReturnType<typeof graphql.scalar<'DeleteCartLineItemInput'>>; | ||
|
||
export async function removeItem({ | ||
lineItemEntityId, | ||
}: Omit<DeleteCartLineItemInput, 'cartEntityId'>) { | ||
// export async function removeProduct(formData: FormData) { | ||
try { | ||
const cartId = cookies().get('cartId')?.value; | ||
// const lineItemEntityId = formData.get('lineItemEntityId'); | ||
|
||
if (!cartId) { | ||
return { status: 'error', error: 'No cartId cookie found' }; | ||
} | ||
|
||
if (!lineItemEntityId) { | ||
return { status: 'error', error: 'No lineItemEntityId found' }; | ||
} | ||
|
||
const updatedCart = await deleteCartLineItem(cartId, lineItemEntityId); | ||
|
||
// If we remove the last item in a cart the cart is deleted | ||
// so we need to remove the cartId cookie and clear shipping data | ||
if (!updatedCart) { | ||
cookies().delete('cartId'); | ||
cookies().delete('shippingCosts'); | ||
revalidateTag('cart'); | ||
} | ||
|
||
revalidateTag('cart'); | ||
|
||
return { status: 'success', data: updatedCart }; | ||
} catch (e: unknown) { | ||
if (e instanceof Error) { | ||
return { status: 'error', error: e.message }; | ||
} | ||
|
||
return { status: 'error' }; | ||
} | ||
} |
32 changes: 0 additions & 32 deletions
32
apps/core/app/[locale]/(default)/cart/_actions/remove-products.ts
This file was deleted.
Oops, something went wrong.
66 changes: 66 additions & 0 deletions
66
apps/core/app/[locale]/(default)/cart/_actions/update-item-quantity.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
'use server'; | ||
|
||
import { revalidatePath } from 'next/cache'; | ||
import { cookies } from 'next/headers'; | ||
|
||
import { graphql } from '~/client/graphql'; | ||
import { updateCartLineItem } from '~/client/mutations/update-cart-line-item'; | ||
|
||
import { removeItem } from './remove-item'; | ||
|
||
type CartLineItemInput = ReturnType<typeof graphql.scalar<'CartLineItemInput'>>; | ||
type UpdateCartLineItemInput = ReturnType<typeof graphql.scalar<'UpdateCartLineItemInput'>>; | ||
|
||
interface UpdateProductQuantityParams extends CartLineItemInput { | ||
lineItemEntityId: UpdateCartLineItemInput['lineItemEntityId']; | ||
} | ||
|
||
export async function updateItemQuantity({ | ||
lineItemEntityId, | ||
productEntityId, | ||
quantity, | ||
variantEntityId, | ||
selectedOptions, | ||
}: UpdateProductQuantityParams) { | ||
try { | ||
const cartId = cookies().get('cartId')?.value; | ||
|
||
if (!cartId) { | ||
return { status: 'error', error: 'No cartId cookie found' }; | ||
} | ||
|
||
if (!lineItemEntityId) { | ||
return { status: 'error', error: 'No lineItemEntityId found' }; | ||
} | ||
|
||
if (quantity === 0) { | ||
const result = await removeItem({ lineItemEntityId }); | ||
|
||
return result; | ||
} | ||
|
||
const cartLineItemData = Object.assign( | ||
{ quantity, productEntityId }, | ||
variantEntityId && { variantEntityId }, | ||
selectedOptions && { selectedOptions }, | ||
); | ||
|
||
const updatedCart = await updateCartLineItem(cartId, lineItemEntityId, { | ||
lineItem: cartLineItemData, | ||
}); | ||
|
||
if (!updatedCart) { | ||
return { status: 'error', error: 'Failed to change product quantity in Cart' }; | ||
} | ||
|
||
revalidatePath('/cart'); | ||
|
||
return { status: 'success', data: updatedCart }; | ||
} catch (e: unknown) { | ||
if (e instanceof Error) { | ||
return { status: 'error', error: e.message }; | ||
} | ||
|
||
return { status: 'error' }; | ||
} | ||
} |
48 changes: 0 additions & 48 deletions
48
apps/core/app/[locale]/(default)/cart/_actions/update-product-quantity.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.