-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add stand alone scoped hook for add product * feat: add use products hook with filter, offset and limit * feat: expose new cart scoped hooks * feat: clean up cart provider * refactor: use new scoped hooks * chore: added changeset
- Loading branch information
Showing
13 changed files
with
225 additions
and
112 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,7 @@ | ||
--- | ||
"@elasticpath/react-shopper-hooks": minor | ||
--- | ||
|
||
- Moved scoped hooks outside cart provider | ||
- Added default cart id to cart provider using the sdks cart id | ||
- Added useProducts hook |
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
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
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-add-bundle-item.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useAddBundleProductToCart } from "./use-add-bundle-product-to-cart" | ||
|
||
export function useCartAddBundleItem() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useAddBundleProductToCart(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-add-product.tsx
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,21 @@ | ||
import { useAddProductToCart } from "./use-add-product" | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
|
||
export function useCartAddProduct() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useAddProductToCart(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-add-promotion.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useAddPromotionToCart } from "./use-add-promotion" | ||
|
||
export function useCartAddPromotion() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useAddPromotionToCart(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-clear.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useDeleteCartItems } from "./use-delete-cart-items" | ||
|
||
export function useCartClear() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useDeleteCartItems(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-remove-item.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useRemoveCartItem } from "./use-remove-cart-item" | ||
|
||
export function useCartRemoveItem() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useRemoveCartItem(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-remove-promotion.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useRemovePromotionCode } from "./use-remove-promotion" | ||
|
||
export function useCartRemovePromotion() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useRemovePromotionCode(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
21 changes: 21 additions & 0 deletions
21
packages/react-shopper-hooks/src/cart/hooks/use-cart-update-item.tsx
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,21 @@ | ||
import { createCartItemsUpdater, useCart } from "../cart-provider" | ||
import { useQueryClient } from "@tanstack/react-query" | ||
import { cartQueryKeys } from "./use-get-cart" | ||
import { useUpdateCartItem } from "./use-update-cart-items" | ||
|
||
export function useCartUpdateItem() { | ||
const { cartId } = useCart() | ||
const queryClient = useQueryClient() | ||
return useUpdateCartItem(cartId, { | ||
onSuccess: (updatedData) => { | ||
// Updates the cart items in the query cache | ||
queryClient.setQueryData( | ||
cartQueryKeys.detail(cartId), | ||
createCartItemsUpdater(updatedData.data), | ||
) | ||
return queryClient.invalidateQueries({ | ||
queryKey: cartQueryKeys.detail(cartId), | ||
}) | ||
}, | ||
}) | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./use-product" | ||
export * from "./use-products" |
48 changes: 48 additions & 0 deletions
48
packages/react-shopper-hooks/src/product/hooks/use-products.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,48 @@ | ||
import { useElasticPath } from "../../elasticpath/elasticpath" | ||
import { UseQueryOptionsWrapper } from "../../types" | ||
import type { | ||
Moltin, | ||
ShopperCatalogResourcePage, | ||
ProductResponse, | ||
} from "@moltin/sdk" | ||
import { useQuery, UseQueryResult } from "@tanstack/react-query" | ||
import { queryKeysFactory } from "../../shared/util/query-keys-factory" | ||
|
||
const PRODUCTS_QUERY_KEY = "products" as const | ||
|
||
export const productsQueryKeys = queryKeysFactory(PRODUCTS_QUERY_KEY) | ||
type ProductsQueryKey = typeof productsQueryKeys | ||
|
||
export type UseProductsParams = NonNullable< | ||
Parameters<Moltin["ShopperCatalog"]["Products"]["All"]> | ||
>[0] | ||
|
||
export function useProducts( | ||
params: UseProductsParams & { | ||
limit?: number | ||
offset?: number | ||
filter?: object | ||
}, | ||
options?: UseQueryOptionsWrapper< | ||
ShopperCatalogResourcePage<ProductResponse>, | ||
Error, | ||
ReturnType<ProductsQueryKey["list"]> | ||
>, | ||
): UseQueryResult<ShopperCatalogResourcePage<ProductResponse>, Error> { | ||
const { client } = useElasticPath() | ||
|
||
const { limit = 25, offset = 0, filter = {} } = params | ||
|
||
return useQuery({ | ||
queryKey: [ | ||
...productsQueryKeys.list({ limit, offset, filter, ...options }), | ||
], | ||
queryFn: () => | ||
client.ShopperCatalog.Products.Limit(limit) | ||
.Offset(offset) | ||
.Filter(filter) | ||
.With(["main_image", "component_products", "files"]) | ||
.All(), | ||
...options, | ||
}) | ||
} |
Oops, something went wrong.