Skip to content

📚 docs + feat(skus): getting-started docs, SkuList context fix, SkuListsContainer params#738

Merged
acasazza merged 7 commits intov5.0.0from
v5/docs/getting-started-core-hooks
Apr 10, 2026
Merged

📚 docs + feat(skus): getting-started docs, SkuList context fix, SkuListsContainer params#738
acasazza merged 7 commits intov5.0.0from
v5/docs/getting-started-core-hooks

Conversation

@acasazza
Copy link
Copy Markdown
Member

@acasazza acasazza commented Apr 7, 2026

Summary

Closes #737

📚 New Storybook getting-started pages

006.core.mdx@commercelayer/core

  • What it is and when to use it directly (no React dependency)
  • Full table of exported functions (getSkus, retrieveSku, updateSku, getPrices, retrievePrice, updatePrice, getSkuAvailability, getSkuLists, retrieveSkuList, getAccessToken)
  • Consistent function signature pattern ({ accessToken, params, options })
  • Code examples: authentication, filtered SKU list, SKU list with included SKUs

007.hooks.mdx@commercelayer/hooks

  • What it is and when to use it directly
  • Full table of exported hooks (useSkus, usePrices, useSkuLists, useAvailability)
  • Return shape reference table (data, isLoading, isValidating, error, fetch/retrieve/update/clear actions, mutate)
  • SWR caching behaviour explained
  • Code examples: useSkus, useSkuLists, usePrices, useAvailability

🛠 Storybook (packages/document) fixes

  • Remove MSW mock server — deleted mocks/ directory, public/mockServiceWorker.js, msw devDependency and all related config; stories now hit the real Commerce Layer API via useGetToken
  • Fix React is not defined in custom toolbar addon — added import React from 'react' to Tool.tsx (Storybook manager uses the classic JSX transform)
  • Fix Meta export in all MDX files — updated import path from @storybook/addon-docs@storybook/addon-docs/blocks (Storybook 10 subpath)

✨ New features (packages/react-components)

SkuListsContainer — new params prop

Callers can now pass QueryParamsRetrieve to control which SKU fields are fetched per list. include: ["skus"] is always enforced; fields.skus defaults to ["code"] for backwards compatibility.

<SkuListsContainer params={{ fields: { skus: ["code", "name", "image_url"] } }}></SkuListsContainer>

SkuList — bridge fix for <Skus> and <SkuField>

<Skus> and <SkuField> rendered nothing when used inside <SkuList> because SkuList only registered its ID in SkuListsContext but never populated SkuContext, which is what Skus reads from. SkuList now wraps its children with SkuContext.Provider containing its slice of the fetched SKU data — making Skus + SkuField work identically to how they do under SkusContainer.


🧹 Refactor (packages/react-components)

  • Deleted SkuListsReducer.ts — zero consumers after the hooks refactor (dead code)
  • Deleted SkuReducer.ts — only consumer was SkuContext.ts importing SkuState; type is now inlined directly
  • Deleted utils/getSkus.ts — zero consumers after the hooks refactor (dead code)

🧪 Tests (packages/hooks)

  • Added useSkuLists.test.ts — 10 unit tests covering: initial state, lazy fetching, query params, loading state, error handling, clear, retrieve, params forwarding, and error on empty ID
  • Added useSkus.unit.test.ts — 2 unit tests (mocked core) covering the mutate updater branches: non-matching SKUs kept unchanged, and ?? [result] fallback when cache is empty
  • Coverage: 100% statements / 100% branches / 100% functions / 100% lines across all hooks (v8)

Test plan

  • All 48 unit/integration tests pass (pnpm test in packages/hooks)
  • All 46 unit/integration tests pass (pnpm test in packages/react-components)
  • pnpm --filter hooks test -- --coverage passes all thresholds (100/100/100/100)
  • pnpm storybook starts without errors in packages/document
  • Storybook sidebar shows "Getting Started / Core package" and "Getting Started / Hooks package"
  • Skus/Stories > SkuListsContainer — list items story renders SKU name and code for each item in the list
  • Skus/Stories > SkusContainer — name and code story renders correctly
  • All tables, code blocks, and callout spans in MDX pages render correctly

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Apr 8, 2026

npm i https://pkg.pr.new/@commercelayer/react-components@738

commit: fb4270e

@acasazza acasazza force-pushed the v5/docs/getting-started-core-hooks branch from c223fc1 to 937a82c Compare April 10, 2026 11:01
…dlers

- Replace fire-and-forget worker.start() with Storybook's beforeAll hook
  so the service worker is registered before any story renders
- Set onUnhandledRequest: 'bypass' to let auth calls (auth.commercelayer.io)
  pass through to the real network without console noise
- Add POLOMXXX000000FFFFFFLXXX to mock SKU data with inventory/delivery info
- Add GET /api/skus/:id handler so getSkuAvailability can retrieve
  individual SKU inventory (previously missing, causing silent failures)
- Filter GET /api/skus list by filter[code_in] query param
- Delete mocks/ directory (browser.js, handlers.js, all data fixtures)
- Delete public/mockServiceWorker.js
- Remove msw devDependency, update-mocks script, and msw config block
- Remove worker import, beforeAll hook, and MSW imports from preview.tsx
- Stories now hit the real Commerce Layer API via useGetToken
Allow callers to customise the SKU fields fetched per list via an
optional params prop (QueryParamsRetrieve<SkuList>).

- include: ["skus"] is always enforced and merged with any
  caller-supplied includes
- fields.skus defaults to ["code"] when the caller omits it,
  preserving backwards-compat behaviour
- caller can pass fields: { skus: ["code", "name", "image_url"] }
  to render additional SKU attributes via SkuField
- params added to useEffect dependency array so fetches re-run
  when query params change
…SkuList

Skus and SkuField were rendering nothing when used inside SkuList because
SkuList was only registering its ID but never populating SkuContext, which
is what the Skus component reads from.

- SkuList now reads skuLists[id] from SkuListsContext and wraps its
  children with SkuContext.Provider, bridging the fetched skus so that
  Skus and SkuField work identically to how they do under SkusContainer
- loading is true while skuLists[id] is undefined (not yet fetched)
- Update SkuListsContainer story to request name field via params so
  SkuField attribute="name" renders correctly
@acasazza acasazza changed the title 📚 docs(document): add getting-started pages for core and hooks packages 📚 docs + feat(skus): getting-started docs, SkuList context fix, SkuListsContainer params Apr 10, 2026
…nd getSkus util

All three files are no longer used after the hooks refactor:
- SkuListsReducer.ts — replaced by useSkuLists hook, no remaining imports
- SkuReducer.ts — only provided SkuState type to SkuContext; inlined there
- utils/getSkus.ts — SKU sorting helper, no remaining imports

SkuState is now defined inline in SkuContext.ts alongside the context it shapes.
@acasazza acasazza marked this pull request as ready for review April 10, 2026 14:55
@acasazza acasazza self-assigned this Apr 10, 2026
@acasazza acasazza added enhancement New feature or request components Components package labels Apr 10, 2026
@acasazza acasazza linked an issue Apr 10, 2026 that may be closed by this pull request
6 tasks
@acasazza acasazza merged commit 50d2431 into v5.0.0 Apr 10, 2026
3 checks passed
@acasazza acasazza deleted the v5/docs/getting-started-core-hooks branch April 10, 2026 15:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

components Components package enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Refactor Availability components and reducer using hooks and core packages

1 participant