Skip to content

Refactor Availability components and reducer using hooks and core packages #744

@acasazza

Description

@acasazza

🎯 Summary

Complete the refactor of the Availability feature by removing the legacy AvailabilityReducer (now dead code) and consolidating all logic and types around the @commercelayer/hooks and @commercelayer/core workspace packages.

🔍 Current state

AvailabilityContainer has already been migrated to use useAvailability from @commercelayer/hooks, which internally calls getSkuAvailability from @commercelayer/core. However, the legacy reducer has not been cleaned up and the useAvailability hook is missing tests.

File Status
src/components/skus/AvailabilityContainer.tsx ✅ Already uses useAvailability from @commercelayer/hooks
src/components/skus/AvailabilityTemplate.tsx ✅ Already imports types from @commercelayer/core
src/context/AvailabilityContext.ts ✅ Already imports DeliveryLeadTime / LeadTimes from @commercelayer/core
src/reducers/AvailabilityReducer.ts ❌ Dead code — no longer referenced anywhere
packages/hooks/src/availability/useAvailability.ts ⚠️ No test file

💡 What needs to be done

🗑️ 1. Remove AvailabilityReducer.ts

The reducer (getAvailability, availabilityReducer, availabilityInitialState) is no longer called by any component or utility. All its logic has been superseded by:

  • getSkuAvailability in packages/core
  • useAvailability in packages/hooks

🏷️ 2. Remove duplicated local types

The reducer still defines local copies of types now owned by @commercelayer/core:

Local type Replace with
DeliveryLeadTime DeliveryLeadTime from @commercelayer/core
LeadTimes LeadTimes from @commercelayer/core
SkuInventory, AvailabilityPayload, AvailabilityState SkuAvailability from @commercelayer/core

📤 3. Verify public API exports

Ensure all types needed by consumers (SkuAvailability, DeliveryLeadTime, LeadTimes) are re-exported from the main index.ts.

🧪 4. Add tests for useAvailability hook

packages/hooks/src/availability/ has no test file. Add useAvailability.test.ts following the pattern of existing hook tests (usePrices.test.ts, useSkus.test.ts). The test should cover:

  • Fetching availability by skuCode
  • Fetching availability by skuId
  • clearAvailability resets state
  • Loading/error states

📦 Relevant packages

@commercelayer/core

export type { SkuAvailability, DeliveryLeadTime, LeadTimes } from "./availability"
export { getSkuAvailability } from "./availability"

@commercelayer/hooks

export { useAvailability } from "./availability/useAvailability"
// returns: { availability, fetchAvailability, clearAvailability, isLoading, isValidating, error, mutate }

✅ Acceptance criteria

  • src/reducers/AvailabilityReducer.ts is deleted.
  • No local type duplicates remain — all types sourced from @commercelayer/core.
  • Public type exports are preserved for consumers.
  • useAvailability.test.ts added in packages/hooks/src/availability/.
  • All existing tests still pass.
  • Build passes cleanly.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions