FIX: CRITICAL use nullish coalescing for product quantity in useMarket composable
- Updated the quantity assignment in the useMarket composable to use nullish coalescing (??) instead of logical OR (||). This change ensures that a quantity of 0 is preserved, preventing unintended defaults when productData.quantity is explicitly set to 0. These changes improve the accuracy of product quantity handling in the market module.
This commit is contained in:
parent
b69be281f3
commit
0447549fa5
2 changed files with 29 additions and 2 deletions
27
CLAUDE.md
27
CLAUDE.md
|
|
@ -730,6 +730,33 @@ export function useMyModule() {
|
|||
- **ALWAYS extend BaseService for module services**
|
||||
- **NEVER create direct dependencies between modules**
|
||||
|
||||
### **⚠️ CRITICAL: JavaScript Falsy Value Bug Prevention**
|
||||
|
||||
**ALWAYS use nullish coalescing (`??`) instead of logical OR (`||`) for numeric defaults:**
|
||||
|
||||
```typescript
|
||||
// ❌ WRONG: Treats 0 as falsy, defaults to 1 even when quantity is validly 0
|
||||
quantity: productData.quantity || 1
|
||||
|
||||
// ✅ CORRECT: Only defaults to 1 when quantity is null or undefined
|
||||
quantity: productData.quantity ?? 1
|
||||
```
|
||||
|
||||
**Why this matters:**
|
||||
- JavaScript falsy values include: `false`, `0`, `""`, `null`, `undefined`, `NaN`
|
||||
- Using `||` for defaults will incorrectly override valid `0` values
|
||||
- This caused a critical bug where products with quantity `0` displayed as quantity `1`
|
||||
- The `??` operator only triggers for `null` and `undefined`, preserving valid `0` values
|
||||
|
||||
**Common scenarios where this bug occurs:**
|
||||
- Product quantities, prices, counters (any numeric value where 0 is valid)
|
||||
- Boolean flags where `false` is a valid state
|
||||
- Empty strings that should be preserved vs. undefined strings
|
||||
|
||||
**Rule of thumb:**
|
||||
- Use `||` only when `0`, `false`, or `""` should trigger the default
|
||||
- Use `??` when only `null`/`undefined` should trigger the default (most cases)
|
||||
|
||||
**Build Configuration:**
|
||||
- Vite config includes PWA, image optimization, and bundle analysis
|
||||
- Manual chunking strategy for vendor libraries (vue-vendor, ui-vendor, shadcn)
|
||||
|
|
|
|||
|
|
@ -310,7 +310,7 @@ export function useMarket() {
|
|||
description: productData.description || '',
|
||||
price: productData.price || 0,
|
||||
currency: productData.currency || 'sats',
|
||||
quantity: productData.quantity || 1,
|
||||
quantity: productData.quantity ?? 1, // Use nullish coalescing to preserve 0
|
||||
images: productData.images || [],
|
||||
categories: categories,
|
||||
createdAt: latestEvent.created_at,
|
||||
|
|
@ -502,7 +502,7 @@ export function useMarket() {
|
|||
description: productData.description || '',
|
||||
price: productData.price || 0,
|
||||
currency: productData.currency || 'sats',
|
||||
quantity: productData.quantity || 1,
|
||||
quantity: productData.quantity ?? 1, // Use nullish coalescing to preserve 0
|
||||
images: productData.images || [],
|
||||
categories: categories,
|
||||
createdAt: event.created_at,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue