Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
julianbenegas committed Apr 18, 2023
1 parent da584ce commit dc2552c
Showing 1 changed file with 111 additions and 1 deletion.
112 changes: 111 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,117 @@ Take a look at some examples:
<summary>Simple example, with <code>localStorage</code></summary>

```ts
// todo
import { createStorefrontHooks } from '@bsmnt/storefront-hooks'

type LineItem = {
merchandiseId: string
quantity: number
}

type Cart = {
id: string
lines: LineItem[]
}

export const {
QueryClientProvider,
useCartQuery,
useAddLineItemsToCartMutation,
useOptimisticCartUpdate,
useRemoveLineItemsFromCartMutation,
useUpdateLineItemsInCartMutation
} = createStorefrontHooks<Cart>({
cartCookieKey: 'example-nextjs-localstorage',
fetchers: {
fetchCart: (cartId: string) => {
const cartFromLocalStorage = localStorage.getItem(cartId)

if (!cartFromLocalStorage) throw new Error('Cart not found')

const cart: Cart = JSON.parse(cartFromLocalStorage)

return cart
}
},
mutators: {
addLineItemsToCart: (cartId, lines) => {
const cartFromLocalStorage = localStorage.getItem(cartId)

if (!cartFromLocalStorage) throw new Error('Cart not found')

const cart: Cart = JSON.parse(cartFromLocalStorage)
// Add line if not exists, update quantity if exists
const updatedCart = lines.reduce((cart, line) => {
const lineIndex = cart.lines.findIndex(
(cartLine) => cartLine.merchandiseId === line.merchandiseId
)

if (lineIndex === -1) {
cart.lines.push(line)
} else {
cart.lines[lineIndex]!.quantity += line.quantity
}

return cart
}, cart)

localStorage.setItem(cartId, JSON.stringify(updatedCart))

return {
data: updatedCart
}
},
createCart: () => {
const cart: Cart = { id: 'cart', lines: [] }
localStorage.setItem(cart.id, JSON.stringify(cart))

return { data: cart }
},
createCartWithLines: (lines) => {
const cart = { id: 'cart', lines }
localStorage.setItem(cart.id, JSON.stringify(cart))

return { data: cart }
},
removeLineItemsFromCart: (cartId, lineIds) => {
const cartFromLocalStorage = localStorage.getItem(cartId)

if (!cartFromLocalStorage) throw new Error('Cart not found')

const cart: Cart = JSON.parse(cartFromLocalStorage)
cart.lines = cart.lines.filter(
(line) => !lineIds.includes(line.merchandiseId)
)
localStorage.setItem(cart.id, JSON.stringify(cart))

return {
data: cart
}
},
updateLineItemsInCart: (cartId, lines) => {
const cartFromLocalStorage = localStorage.getItem(cartId)

if (!cartFromLocalStorage) throw new Error('Cart not found')

const cart: Cart = JSON.parse(cartFromLocalStorage)
cart.lines = lines
localStorage.setItem(cart.id, JSON.stringify(cart))

return {
data: cart
}
}
},
logging: {
onError(type, error) {
console.info({ type, error })
},
onSuccess(type, data) {
console.info({ type, data })
}
}
})

```
</details>
<details>
Expand Down

1 comment on commit dc2552c

@vercel
Copy link

@vercel vercel bot commented on dc2552c Apr 18, 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-git-main-basement.vercel.app
commerce-toolkit-nextjs-localstorage-basement.vercel.app
commerce-toolkit-nextjs-localstorage.vercel.app

Please sign in to comment.