Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use product dynamic include #206

Merged
merged 7 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/calm-hotels-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
"@elasticpath/react-shopper-hooks": minor
---

- add product list example
- add product now takes optional params
- remove params for use delete cart query
- allow select query function
- make add product include params dynamic
42 changes: 30 additions & 12 deletions packages/react-shopper-hooks/example/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,48 @@ import CartExample from "./CartExample"
import { CartProvider } from "../src/cart"
import { gateway as EPCCGateway } from "@moltin/sdk"
import { StoreProvider } from "../src/store"
import { ElasticPathProvider } from "../src"
import { QueryClient } from "@tanstack/react-query"
import { ProductListExample } from "./ProductListExample"

const client = EPCCGateway({
name: "my_store",
client_id: import.meta.env.VITE_APP_EPCC_CLIENT_ID,
client_secret: import.meta.env.VITE_APP_EPCC_CLIENT_SECRET,
host: import.meta.env.VITE_APP_EPCC_HOST,
})

const queryClient = new QueryClient()

function App() {
const client = EPCCGateway({
name: "my_store",
client_id: import.meta.env.VITE_APP_EPCC_CLIENT_ID,
client_secret: import.meta.env.VITE_APP_EPCC_CLIENT_SECRET,
host: import.meta.env.VITE_APP_EPCC_HOST,
})
const [activeItem, setActiveItem] = React.useState<"cart" | "products">(
"cart",
)

return (
<div className="App">
<h1>React Shopper Hooks</h1>
<div>
<button onClick={() => setActiveItem("cart")}>Cart View</button>
<button onClick={() => setActiveItem("products")}>Products View</button>
</div>
<div className="card">
<StoreProvider
<ElasticPathProvider
client={client}
resolveCartId={() => client.Cart().cartId}
queryClientProviderProps={{ client: queryClient }}
>
<CartProvider resolveCartId={() => client.Cart().cartId}>
<CartExample />
</CartProvider>
</StoreProvider>
<StoreProvider cartId={client.Cart().cartId}>
<CartProvider cartId={client.Cart().cartId}>
{activeItem === "cart" && <CartExample />}
{activeItem === "products" && <ProductListExample />}
</CartProvider>
</StoreProvider>
</ElasticPathProvider>
</div>
</div>
)
}

function TanstackWrapper() {}

export default App
16 changes: 9 additions & 7 deletions packages/react-shopper-hooks/example/CartExample.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import React from "react"
import { useCart } from "../src/cart"
import { useCart, useCartAddBundleItem, useCartClear } from "../src/cart"

export default function CartExample(): JSX.Element {
const { state, addBundleProductToCart, emptyCart } = useCart()
const { state } = useCart()
const { mutate: addBundleProductToCart } = useCartAddBundleItem()
const { mutate: emptyCart } = useCartClear()
return (
<>
<button onClick={() => emptyCart()}>Empty Cart</button>
<button
onClick={() =>
addBundleProductToCart(
"14edd744-c615-4a33-a2c2-df999bbb5103",
{
addBundleProductToCart({
productId: "14edd744-c615-4a33-a2c2-df999bbb5103",
quantity: 1,
selectedOptions: {
plants: {
"a158ffa0-5d16-4325-8dcc-be8acd55eecf": 1,
},
Expand All @@ -21,8 +24,7 @@ export default function CartExample(): JSX.Element {
"7ffe107d-c5bd-4de4-b8f0-a58d90cb3cd3": 1,
},
},
1,
)
})
}
>
Add bundle to cart
Expand Down
17 changes: 17 additions & 0 deletions packages/react-shopper-hooks/example/ProductListExample.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import React from "react"
import { useProducts } from "../src"

export function ProductListExample() {
const { data: products } = useProducts()
return (
<div>
<h1>Product List</h1>
{products?.data.map((product) => (
<div key={product.id}>
<h2>{product.attributes.name}</h2>
<p>{product.id}</p>
</div>
))}
</div>
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ import { useMutation, UseMutationOptions } from "@tanstack/react-query"
import { useElasticPath } from "../../elasticpath"
import { CartItemsResponse } from "@moltin/sdk"

type CartDeleteCartItemsReq = {}

export const useDeleteCartItems = (
cartId: string,
options?: UseMutationOptions<
CartItemsResponse,
Error,
CartDeleteCartItemsReq
>,
options?: UseMutationOptions<CartItemsResponse, Error>,
) => {
const { client } = useElasticPath()
return useMutation({
Expand Down
14 changes: 10 additions & 4 deletions packages/react-shopper-hooks/src/product/hooks/use-products.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ export type UseProductsParams = NonNullable<
Parameters<Moltin["ShopperCatalog"]["Products"]["All"]>
>[0]

export type ShopperCatalogProductsInclude =
| "main_image"
| "files"
| "component_products"

export function useProducts(
params: UseProductsParams & {
params?: UseProductsParams & {
limit?: number
offset?: number
filter?: object
include?: ShopperCatalogProductsInclude | ShopperCatalogProductsInclude[]
},
options?: UseQueryOptionsWrapper<
ShopperCatalogResourcePage<ProductResponse>,
Expand All @@ -31,17 +37,17 @@ export function useProducts(
): UseQueryResult<ShopperCatalogResourcePage<ProductResponse>, Error> {
const { client } = useElasticPath()

const { limit = 25, offset = 0, filter = {} } = params
const { limit = 25, offset = 0, filter = {}, include = [] } = params ?? {}

return useQuery({
queryKey: [
...productsQueryKeys.list({ limit, offset, filter, ...options }),
...productsQueryKeys.list({ limit, offset, filter, include, ...options }),
],
queryFn: () =>
client.ShopperCatalog.Products.Limit(limit)
.Offset(offset)
.Filter(filter)
.With(["main_image", "component_products", "files"])
.With(include)
.All(),
...options,
})
Expand Down
2 changes: 1 addition & 1 deletion packages/react-shopper-hooks/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type UseQueryOptionsWrapper<
TQueryKey extends QueryKey = QueryKey,
> = Omit<
UseQueryOptions<TQueryFn, E, TQueryFn, TQueryKey>,
"queryKey" | "queryFn" | "select" | "refetchInterval"
"queryKey" | "queryFn" | "refetchInterval"
>

export type TQueryKey<TKey, TListQuery = any, TDetailQuery = string> = {
Expand Down