fullcart
Version:
Add stripe checkout to any website
22 lines (19 loc) • 661 B
text/typescript
import type { Customer } from '../schemas/customerSchema'
import type { Product } from '../schemas/productSchema'
import { customer } from '../stores/customer'
import { removeItem } from './removeItem'
export const setQuantity = (
productId: Product['id'],
quantity: Customer['lines'][number]['quantity'],
) => {
const lines = customer.get().lines
const index = lines.findIndex((line) => line.productId === productId)
if (quantity <= 0) removeItem(productId)
else if (index < 0) {
lines.push({ productId, quantity })
customer.setKey('lines', lines)
} else {
lines[index].quantity = quantity
customer.setKey('lines', lines)
}
}