mirror of
https://github.com/vercel/commerce.git
synced 2025-05-18 15:36:58 +00:00
feat: Add wishlist
This commit is contained in:
parent
90fbe59c7e
commit
93d5fc0220
@ -34,7 +34,6 @@ const WishlistButton: FC<Props> = ({
|
|||||||
item.product_id === productId &&
|
item.product_id === productId &&
|
||||||
item.variant_id === variant.id
|
item.variant_id === variant.id
|
||||||
)
|
)
|
||||||
|
|
||||||
const handleWishlistChange = async (e: any) => {
|
const handleWishlistChange = async (e: any) => {
|
||||||
e.preventDefault()
|
e.preventDefault()
|
||||||
|
|
||||||
|
@ -53,6 +53,7 @@ const WishlistCard: FC<Props> = ({ item }) => {
|
|||||||
await addItem({
|
await addItem({
|
||||||
productId: String(product.id),
|
productId: String(product.id),
|
||||||
variantId: String(product.variants[0].id),
|
variantId: String(product.variants[0].id),
|
||||||
|
sizeId: String(product.variants[0].options[0].id),
|
||||||
})
|
})
|
||||||
openSidebar()
|
openSidebar()
|
||||||
setLoading(false)
|
setLoading(false)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
import { SWRHook } from '@commerce/utils/types'
|
import { SWRHook } from '@commerce/utils/types'
|
||||||
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
import useSearch, { UseSearch } from '@commerce/product/use-search'
|
||||||
import data from '../data.json'
|
import data from '../data.json'
|
||||||
import useCallback from 'react'
|
|
||||||
export default useSearch as UseSearch<typeof handler>
|
export default useSearch as UseSearch<typeof handler>
|
||||||
|
|
||||||
const productsFinder = (s: string) => {
|
const productsFinder = (s: string) => {
|
||||||
@ -31,7 +30,7 @@ export const handler: SWRHook<any> = {
|
|||||||
products,
|
products,
|
||||||
found: true,
|
found: true,
|
||||||
}
|
}
|
||||||
: null,
|
: data,
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,19 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback, useMemo } from 'react'
|
||||||
|
|
||||||
export function emptyHook() {
|
export function emptyHook() {
|
||||||
const useEmptyHook = async (options = {}) => {
|
const useEmptyHook = async (options: any = {}) => {
|
||||||
return useCallback(async function () {
|
let wishlist = []
|
||||||
return Promise.resolve()
|
const localWishlist = localStorage.getItem('wishlist')
|
||||||
}, [])
|
if (localWishlist) {
|
||||||
|
wishlist = JSON.parse(localWishlist)
|
||||||
|
if (!wishlist.includes(options.variantId)) {
|
||||||
|
wishlist.push(options.variantId)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
wishlist.push(options.variantId)
|
||||||
|
}
|
||||||
|
localStorage.setItem('wishlist', JSON.stringify(wishlist))
|
||||||
|
return wishlist
|
||||||
}
|
}
|
||||||
|
|
||||||
return useEmptyHook
|
return useEmptyHook
|
||||||
|
@ -1,14 +1,17 @@
|
|||||||
import { useCallback } from 'react'
|
|
||||||
|
|
||||||
type Options = {
|
type Options = {
|
||||||
includeProducts?: boolean
|
includeProducts?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
export function emptyHook(options?: Options) {
|
export function emptyHook(options?: Options) {
|
||||||
const useEmptyHook = async ({ id }: { id: string | number }) => {
|
const useEmptyHook = async ({ id }: { id: string | number }) => {
|
||||||
return useCallback(async function () {
|
let wishlist = []
|
||||||
return Promise.resolve()
|
const localWishlist = localStorage.getItem('wishlist')
|
||||||
}, [])
|
if (localWishlist) {
|
||||||
|
wishlist = JSON.parse(localWishlist)
|
||||||
|
wishlist = wishlist.filter((p: string) => p !== id)
|
||||||
|
}
|
||||||
|
localStorage.setItem('wishlist', JSON.stringify(wishlist))
|
||||||
|
return Promise.resolve()
|
||||||
}
|
}
|
||||||
|
|
||||||
return useEmptyHook
|
return useEmptyHook
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
import { HookFetcher } from '@commerce/utils/types'
|
import { HookFetcher } from '@commerce/utils/types'
|
||||||
import type { Product } from '@commerce/types/product'
|
import type { Product } from '@commerce/types/product'
|
||||||
|
import { products } from '../data.json'
|
||||||
|
import { useCustomer } from '@framework/customer'
|
||||||
|
|
||||||
const defaultOpts = {}
|
const defaultOpts = {}
|
||||||
|
|
||||||
@ -32,6 +34,22 @@ export function extendHook(
|
|||||||
swrOptions?: any
|
swrOptions?: any
|
||||||
) {
|
) {
|
||||||
const useWishlist = ({ includeProducts }: UseWishlistOptions = {}) => {
|
const useWishlist = ({ includeProducts }: UseWishlistOptions = {}) => {
|
||||||
|
const { data: customer } = useCustomer()
|
||||||
|
const getWishlist =
|
||||||
|
typeof localStorage !== 'undefined' && localStorage.getItem('wishlist')
|
||||||
|
if (getWishlist && customer?.email) {
|
||||||
|
const wishlist = JSON.parse(getWishlist)
|
||||||
|
const items = wishlist.map((wishlist: string) => {
|
||||||
|
const [product] = products.filter((p) => p.id === wishlist) as any
|
||||||
|
return {
|
||||||
|
variant_id: wishlist,
|
||||||
|
product_id: wishlist,
|
||||||
|
id: wishlist,
|
||||||
|
product,
|
||||||
|
}
|
||||||
|
})
|
||||||
|
return { data: { items } }
|
||||||
|
}
|
||||||
return { data: null }
|
return { data: null }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user