Skip to content

♻️ refactor(line-items): deprecate LineItemsContainer, add LineItems standalone component#776

Merged
acasazza merged 6 commits into
v5.0.0from
feat/line-items-refactor
May 27, 2026
Merged

♻️ refactor(line-items): deprecate LineItemsContainer, add LineItems standalone component#776
acasazza merged 6 commits into
v5.0.0from
feat/line-items-refactor

Conversation

@acasazza
Copy link
Copy Markdown
Member

@acasazza acasazza commented May 25, 2026

Summary

Refactors the line items components following the same pattern used by orders, prices, and SKU lists.

Changes

@commercelayer/core

  • line_items/getLineItems.ts — fetches an order's line items with full includes (line_item_options.sku_option, item)
  • line_items/updateLineItem.ts — updates quantity / external price flag
  • line_items/deleteLineItem.ts — deletes a line item

@commercelayer/hooks

  • useLineItems({ accessToken?, orderId?, interceptors? }) — SWR-based hook exposing lineItems, isLoading, error, updateLineItem, deleteLineItem, reload, mutate
    • Both accessToken and orderId are optional; the hook skips fetching when either is absent

@commercelayer/react-components

  • New LineItems standalone component
    • Props: types? (filter by item_type), loader?, onUpdate?, onDelete?
    • Reads accessToken from <CommerceLayer> context and orderId from <Order> context automatically
    • Uses useLineItems internally; feeds LineItemContext privately so all child components work unchanged
  • LineItemsContainer marked @deprecated → migrate to <LineItems>
  • LineItemContext — added reload?() to context value type
  • LineItemsCount — fixed contextComponentName warning (LineItemsContainerLineItems)

Improvements over old container

LineItemsContainer LineItems
Requires explicit accessToken prop ❌ (from context)
Requires explicit orderId prop ❌ (from context)
types filter ❌ (defined, never used)
onUpdate / onDelete callbacks
reload() in context

Test coverage

All new files have 100% coverage (statements, branches, functions, lines):

  • 17 core tests (getLineItems, updateLineItem, deleteLineItem)
  • 14 hook tests (useLineItems)
  • 13 component tests (LineItems)

Usage

<CommerceLayer accessToken={token} endpoint={endpoint}>
  <Order>
    <LineItems
      types={["skus", "bundles"]}
      onDelete={(id) => toast(`Item ${id} removed`)}
    >
      <LineItem>
        <LineItemName />
        <LineItemQuantity />
        <LineItemRemoveLink />
      </LineItem>
      <LineItemsCount />
      <LineItemsEmpty />
    </LineItems>
  </Order>
</CommerceLayer>

Alessandro Casazza and others added 2 commits May 25, 2026 17:27
…standalone component

- Add core functions: getLineItems, updateLineItem, deleteLineItem
- Add useLineItems hook (SWR-based) to @commercelayer/hooks
- Add LineItems standalone component (no OrderContext required)
  - supports types prop for item_type filtering
  - supports onUpdate/onDelete callbacks
  - exposes reload() via LineItemContext
- Deprecate LineItemsContainer in favour of LineItems
- Fix LineItemsCount contextComponentName warning (LineItemsContainer → LineItems)
- Add reload to LineItemContext

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…nd LineItems component

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 25, 2026

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

commit: 156b24d

@acasazza acasazza added refactor enhancement New feature or request core Core package labels May 25, 2026
@acasazza acasazza self-assigned this May 25, 2026
@acasazza acasazza added the components Components package label May 25, 2026
Alessandro Casazza and others added 4 commits May 25, 2026 17:45
…context

- accessToken falls back to CommerceLayerContext when not provided as prop
- orderId falls back to OrderContext when not provided as prop
- prop values always take precedence over context values
- Added 4 tests covering all fallback and precedence combinations

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- UseLineItemsParams.accessToken is now optional (swrKey already guards fetch)
- Added accessToken guard in SWR fetcher, updateLineItem, and deleteLineItem
- Fixed definite assignment assertion in test to satisfy strict TS check

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…neItems

Both values are now read exclusively from context:
- accessToken from CommerceLayerContext
- orderId from OrderContext

Updated tests to use Providers wrapper instead of direct props.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Remove dead guards from SWR fetcher (swrKey already prevents unreachable paths)
- Add tests: returns empty when accessToken is undefined
- Add tests: updateLineItem and deleteLineItem throw when accessToken is missing

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@acasazza acasazza requested review from gciotola and pfferrari May 27, 2026 08:47
@acasazza acasazza merged commit 3149e3a into v5.0.0 May 27, 2026
2 checks passed
@acasazza acasazza deleted the feat/line-items-refactor branch May 27, 2026 10:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

components Components package core Core package enhancement New feature or request refactor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants