30 lines
967 B
TypeScript
30 lines
967 B
TypeScript
import { cartApi } from '@/api/cart'
|
|
import { toOptionalIdString } from '@/utils/id'
|
|
|
|
export const fetchCartProductIdSet = async () => {
|
|
try {
|
|
const response = await cartApi.getCartList() as unknown as Array<{ productId?: string | number }>
|
|
return new Set(
|
|
(Array.isArray(response) ? response : [])
|
|
.map(item => toOptionalIdString(item && item.productId))
|
|
.filter((id): id is string => Boolean(id))
|
|
)
|
|
} catch (error) {
|
|
return new Set<string>()
|
|
}
|
|
}
|
|
|
|
export const hasCartProduct = (cartProductIdSet: Set<string>, productId: unknown) => {
|
|
const normalizedId = toOptionalIdString(productId)
|
|
return Boolean(normalizedId && cartProductIdSet.has(normalizedId))
|
|
}
|
|
|
|
export const withAddedCartProduct = (cartProductIdSet: Set<string>, productId: unknown) => {
|
|
const next = new Set(cartProductIdSet || [])
|
|
const normalizedId = toOptionalIdString(productId)
|
|
if (normalizedId) {
|
|
next.add(normalizedId)
|
|
}
|
|
return next
|
|
}
|