Skip to content

Commit

Permalink
feat: add subs to cart items (#262)
Browse files Browse the repository at this point in the history
* feat: add subscription items to cart

* chore: changeset
  • Loading branch information
field123 authored Jan 8, 2025
1 parent 7c62d03 commit 23358ba
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/gold-pianos-stare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@elasticpath/react-shopper-hooks": patch
---

Support subscription items in the cart state
14 changes: 13 additions & 1 deletion packages/react-shopper-hooks/src/cart/types/cart-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export type RegularCartItem = DeepReadonly<CartItem> & {
readonly type: "cart_item"
}

export type SubscriptionCartItem = DeepReadonly<CartItem> & {
readonly type: "subscription_item"
}

/**
* Cart items seperated into their respective groups by type property
* cart_item, promotion_item and custom_item.
Expand All @@ -23,6 +27,11 @@ export interface GroupedCartItems {
*/
readonly regular: RegularCartItem[]

/**
* cart items of type subscription_item
*/
readonly subscription_offerings: SubscriptionCartItem[]

/**
* cart items of type promotion_item
*/
Expand All @@ -34,7 +43,10 @@ export interface GroupedCartItems {
readonly custom: CustomCartItem[]
}

export type RefinedCartItem = RegularCartItem | CustomCartItem
export type RefinedCartItem =
| RegularCartItem
| CustomCartItem
| SubscriptionCartItem

export type ItemDiscountInstance = {
id: string
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
type CartItemsType = "cart_item" | "promotion_item" | "custom_item";
type CartItemsType =
| "cart_item"
| "promotion_item"
| "custom_item"
| "subscription_item"

export function assertCartItemType<
T extends { type?: string },
R extends CartItemsType
R extends CartItemsType,
>(
obj: T,
test: R
test: R,
): obj is T & {
type: R;
type: R
} {
return typeof obj.type === "string" && obj.type === test;
return typeof obj.type === "string" && obj.type === test
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ export function enhanceCartItems(items: CartItem[]) {
const enhanced =
items
?.filter(
(item) => item.type === "cart_item" || item.type === "custom_item",
(item) =>
item.type === "cart_item" ||
item.type === "custom_item" ||
item.type === "subscription_item",
)
.sort(sortItemByCreatedAsc) ?? []

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ export function groupCartItems(items: CartItem[]): GroupedCartItems {
...(assertCartItemType(item, "cart_item")
? { regular: [...acc?.regular, item] }
: acc.regular),
...(assertCartItemType(item, "subscription_item")
? { subscription_offerings: [...acc?.subscription_offerings, item] }
: acc.subscription_offerings),
...(assertCartItemType(item, "promotion_item")
? { promotion: [...acc?.promotion, item] }
: acc.promotion),
Expand All @@ -20,6 +23,7 @@ export function groupCartItems(items: CartItem[]): GroupedCartItems {
},
{
regular: [],
subscription_offerings: [],
promotion: [],
custom: [],
} as GroupedCartItems,
Expand Down

0 comments on commit 23358ba

Please sign in to comment.