Files
JayuCloud/FD-Frontend/src/utils/cart-state.ts
T
2026-06-15 15:53:55 +08:00

42 lines
1.7 KiB
TypeScript

import { cartApi } from '@/api/cart'
import { toOptionalIdString } from '@/utils/id'
/**
* Purpose: fetchCartProductIdSet supports cart-state.ts by holding or executing the fetch cart product id set page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
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>()
}
}
/**
* Purpose: hasCartProduct supports cart-state.ts by holding or executing the has cart product page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const hasCartProduct = (cartProductIdSet: Set<string>, productId: unknown) => {
const normalizedId = toOptionalIdString(productId)
return Boolean(normalizedId && cartProductIdSet.has(normalizedId))
}
/**
* Purpose: withAddedCartProduct supports cart-state.ts by holding or executing the with added cart product page or API role.
* Usage: import, call, read, or update this const from the owning page/module while preserving the surrounding loading and permission flow.
*/
export const withAddedCartProduct = (cartProductIdSet: Set<string>, productId: unknown) => {
const next = new Set(cartProductIdSet || [])
const normalizedId = toOptionalIdString(productId)
if (normalizedId) {
next.add(normalizedId)
}
return next
}