Skip to content

Commit

Permalink
expose cookie options (#51)
Browse files Browse the repository at this point in the history
* expose cookie options

* changeset
  • Loading branch information
julianbenegas committed Jun 26, 2023
1 parent bcf244b commit f2c1a3c
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-vans-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@bsmnt/storefront-hooks': patch
---

Expose cookie options
10 changes: 7 additions & 3 deletions packages/hooks/src/helpers/use-cart-cookie-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,19 @@ import * as React from 'react'

import { isClient } from './misc'

export const useCartCookieManager = (key: string) => {
export const useCartCookieManager = (
key: string,
opts?: Cookies.CookieAttributes
) => {
return React.useMemo(() => {
return {
set: (id: string) => {
if (!isClient) return
Cookies.set(key, id, {
sameSite: 'strict',
secure: true,
expires: 365 // one year
expires: 365, // one year
...opts
})
},
get: () => {
Expand All @@ -23,5 +27,5 @@ export const useCartCookieManager = (key: string) => {
return Cookies.remove(key)
}
}
}, [key])
}, [key, opts])
}
9 changes: 7 additions & 2 deletions packages/hooks/src/mutations/add-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export const useAddLineItemsToCartMutation = <Cart extends BarebonesCart>({
mutators,
cartCookieKey,
options,
logging
logging,
cartCookieOptions
}: {
mutators: Pick<
CartMutators<Cart>,
Expand All @@ -26,8 +27,12 @@ export const useAddLineItemsToCartMutation = <Cart extends BarebonesCart>({
cartCookieKey: string
options: InternalOptions<Cart>
logging?: Logging<Cart>
cartCookieOptions?: Cookies.CookieAttributes
}) => {
const cartCookieManager = useCartCookieManager(cartCookieKey)
const cartCookieManager = useCartCookieManager(
cartCookieKey,
cartCookieOptions
)
const optimisticCartUpdate = useOptimisticCartUpdate<Cart>()

return useMutation(
Expand Down
9 changes: 7 additions & 2 deletions packages/hooks/src/mutations/remove-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export const useRemoveLineItemsFromCartMutation = <Cart extends BarebonesCart>({
mutators,
cartCookieKey,
options,
logging
logging,
cartCookieOptions
}: {
mutators: Pick<CartMutators<Cart>, 'removeLineItemsFromCart'>
cartCookieKey: string
options: InternalOptions<Cart>
logging?: Logging<Cart>
cartCookieOptions?: Cookies.CookieAttributes
}) => {
const cartCookieManager = useCartCookieManager(cartCookieKey)
const cartCookieManager = useCartCookieManager(
cartCookieKey,
cartCookieOptions
)
const optimisticCartUpdate = useOptimisticCartUpdate<Cart>()

return useMutation(
Expand Down
9 changes: 7 additions & 2 deletions packages/hooks/src/mutations/update-lines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ export const useUpdateLineItemsInCartMutation = <Cart extends BarebonesCart>({
mutators,
cartCookieKey,
options,
logging
logging,
cartCookieOptions
}: {
mutators: Pick<CartMutators<Cart>, 'updateLineItemsInCart'>
cartCookieKey: string
options: InternalOptions<Cart>
logging?: Logging<Cart>
cartCookieOptions?: Cookies.CookieAttributes
}) => {
const cartCookieManager = useCartCookieManager(cartCookieKey)
const cartCookieManager = useCartCookieManager(
cartCookieKey,
cartCookieOptions
)
const optimisticCartUpdate = useOptimisticCartUpdate<Cart>()

return useMutation(
Expand Down
9 changes: 7 additions & 2 deletions packages/hooks/src/queries/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,20 @@ export const useCartQuery = <Cart extends BarebonesCart>({
mutators,
cartCookieKey,
options,
logging
logging,
cartCookieOptions
}: {
fetchCart: CartFetcher<Cart | null>
mutators: Pick<CartMutators<Cart>, 'createCart'>
cartCookieKey: string
options: Options<Cart>
logging?: Logging<Cart>
cartCookieOptions?: Cookies.CookieAttributes
}) => {
const cartCookieManager = useCartCookieManager(cartCookieKey)
const cartCookieManager = useCartCookieManager(
cartCookieKey,
cartCookieOptions
)

const createCart = React.useCallback(async () => {
const { data, userErrors, silenceUserErrors } = await mutators.createCart()
Expand Down
8 changes: 7 additions & 1 deletion packages/hooks/src/storefront-hooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
createCartIfNotFound,
queryClientConfig,
cartOpenStateOptions,
logging
logging,
cartCookieOptions
}: {
cartCookieKey: string
fetchers: {
Expand All @@ -100,6 +101,7 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
queryClientConfig?: QueryClientConfig
cartOpenStateOptions?: cartOpenState.UseCartOpenStateOptions
logging?: Logging<Cart>
cartCookieOptions?: Cookies.CookieAttributes
}) {
const queryClient = new QueryClient(queryClientConfig)

Expand All @@ -112,6 +114,7 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
useCartQuery: (options?: cartQuery.UseCartQueryUserOptions<Cart>) => {
return cartQuery.useCartQuery({
cartCookieKey,
cartCookieOptions,
fetchCart: fetchers.fetchCart,
mutators: { createCart: mutators.createCart },
options: {
Expand All @@ -135,6 +138,7 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
createCartWithLines: mutators.createCartWithLines
},
cartCookieKey,
cartCookieOptions,
options: { ...options },
logging
})
Expand All @@ -145,6 +149,7 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
return updateLines.useUpdateLineItemsInCartMutation<Cart>({
mutators: { updateLineItemsInCart: mutators.updateLineItemsInCart },
cartCookieKey,
cartCookieOptions,
options: {
...options,
mutationOptions: options?.mutationOptions
Expand All @@ -158,6 +163,7 @@ export function createStorefrontHooks<Cart extends BarebonesCart>({
return removeLines.useRemoveLineItemsFromCartMutation<Cart>({
mutators: { removeLineItemsFromCart: mutators.removeLineItemsFromCart },
cartCookieKey,
cartCookieOptions,
options: {
...options,
mutationOptions: options?.mutationOptions
Expand Down

2 comments on commit f2c1a3c

@vercel
Copy link

@vercel vercel bot commented on f2c1a3c Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

commerce-toolkit-nextjs-shopify – ./examples/nextjs-shopify

commerce-toolkit-nextjs-shopify-git-main-basement.vercel.app
commerce-toolkit-nextjs-shopify.vercel.app
commerce-toolkit-nextjs-shopify-basement.vercel.app

@vercel
Copy link

@vercel vercel bot commented on f2c1a3c Jun 26, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

commerce-toolkit-nextjs-localstorage – ./examples/nextjs-localstorage

commerce-toolkit-nextjs-localstorage.vercel.app
commerce-toolkit-nextjs-localstorage-basement.vercel.app
commerce-toolkit-nextjs-localstorage-git-main-basement.vercel.app

Please sign in to comment.