Skip to content

♻️ refactor(gift_cards): standalone GiftCard, remove reducer, rapid-form v4, 100% coverage#779

Merged
acasazza merged 11 commits into
v5.0.0from
refactor/gift-cards
May 29, 2026
Merged

♻️ refactor(gift_cards): standalone GiftCard, remove reducer, rapid-form v4, 100% coverage#779
acasazza merged 11 commits into
v5.0.0from
refactor/gift-cards

Conversation

@acasazza
Copy link
Copy Markdown
Member

Closes #778

Summary

Refactors the gift_cards domain to follow the same patterns as prices, line-items, and orders.

Changes

✨ Standalone <GiftCard>

  • Manages its own state/context — no GiftCardContainer parent required
  • Auto-detects whether it's inside GiftCardContainer for backward compat

🗑️ Remove GiftCardReducer.ts

  • useReducer + reducer replaced by useState + createGiftCard from @commercelayer/core
  • All types moved to GiftCardContext.ts

⚠️ Deprecate GiftCardContainer

  • Dev-mode console warning pointing to the new standalone API
  • Fully backward compatible

♻️ Rewrite GiftCardOrCouponForm for rapid-form v4

  • Properly typed useRapidForm() (no as any)
  • refValidation on <form> (form-level event delegation)
  • Fixed double-firing useEffect bug
  • Native e.currentTarget.reset() replaces removed reset()
  • Removed dead validation from CouponAndGiftCardFormContext

🐛 Fix deprecated React 19 type

  • React.FormEventReact.SyntheticEvent

✅ 100% test coverage

  • 43 tests across 4 new spec files in specs/gift_cards/
  • 100% statements / branches / functions / lines

Alessandro Casazza and others added 11 commits May 28, 2026 16:42
- Delete GiftCardRecipientInput.tsx (entirely commented-out dead code)
- GiftCard: fix ref never attached to <form>; use e.currentTarget for
  submit logic; forward external ref prop; remove unused fragment wrapper
  and key prop; drop now-unnecessary @ts-expect-error directive
- GiftCardOrCouponCode: fix payment-provider branch rendering the correct
  display string in the default <span> (was rendering order[codeType]
  instead of the computed cardBrand+cardSummary)
- GiftCardOrCouponRemoveButton: consolidate duplicated return block into
  one (payment-provider branch only sets hide=false before shared render)
- GiftCardOrCouponSubmit: remove large commented-out dead code block

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- GiftCardOrCouponCode: let → const for `hide` (never reassigned)
- isJSON, PaypalPayment, WireTransferPayment, CustomerReducer (×3):
  simplify unused catch bindings to optional catch {}

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
GiftCard now manages its own state and context internally without
requiring a GiftCardContainer parent.

Standalone detection: checks if GiftCardContext.addGiftCard is null
(default context) to determine whether a parent GiftCardContainer is
present. When standalone, useReducer + CommerceLayerContext +
OrderContext are used directly and the form is wrapped in its own
GiftCardContext.Provider. When inside GiftCardContainer, the container's
context is re-exposed so all descendants stay in sync.

GiftCardContainer is marked @deprecated with a dev-only console.warn
(fires at most once per session via a module-level flag) and migration
examples in its JSDoc. Its existing logic is preserved for full backward
compatibility.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Core: getGiftCards, retrieveGiftCard, createGiftCard, updateGiftCard
  with interceptors support and full spec coverage (100%)
- Hook: useGiftCards with SWR caching, action state tracking,
  CRUD operations, and 100% coverage
- Interceptors spec + integration spec for each core function
- useGiftCards.test.ts (integration) + useGiftCards.interceptors.test.ts
  (unit, mocked) with 22 tests total
- Export new modules from packages/core and packages/hooks index files

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

- Moved GiftCardI, GiftCardRecipientI, GiftCardState, giftCardInitialState into GiftCardContext.ts
- Replaced useReducer in GiftCard.tsx with useState (errors, loading, giftCardRecipient)
- Replaced useReducer in GiftCardContainer.tsx with useState
- Inlined addGiftCard business logic using createGiftCard from @commercelayer/core
- Deleted GiftCardReducer.ts

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Replace config.accessToken! non-null assertions with ?? "" fallback
- Add biome-ignore for necessary any casts (camelCase form values at runtime)
- Add biome-ignore for ref forwarding any cast in GiftCardOrCouponInput

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Replace (useRapidForm as any)() with typed useRapidForm()
- Use refValidation as form ref (v4 API — no per-input refs needed)
- Fix double-firing useEffect bug (both branches had the same condition)
- Fix useEffect exhaustive deps (errors, type, setOrderErrors, onSubmit, codeType)
- Replace removed reset() with native e.currentTarget.reset()
- Add FormCodeType to properly handle 'gift_card_or_coupon_code' fallback
- Remove unused validation from CouponAndGiftCardFormContext
- Remove dead ref={validation} from GiftCardOrCouponInput

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
FormEvent doesn't actually exist in the DOM — React 19 marks it @deprecated.
Replaced with React.SyntheticEvent<HTMLFormElement> in gift card form handlers.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Refactor await-in-ternary to explicit if/else in GiftCard.tsx and
  GiftCardContainer.tsx to fix v8 branch instrumentation issue
- Add edge-case tests for order existing with no id in both spec files

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- Add biome-ignore suppressions for necessary as-any casts in test helpers
- Remove unused imports auto-fixed by biome --unsafe

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

pkg-pr-new Bot commented May 28, 2026

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

commit: 381ade7

@acasazza acasazza added enhancement New feature or request task labels May 29, 2026
@acasazza acasazza self-assigned this May 29, 2026
@acasazza acasazza requested review from gciotola and pfferrari May 29, 2026 07:30
@acasazza acasazza merged commit 61bc454 into v5.0.0 May 29, 2026
2 checks passed
@acasazza acasazza deleted the refactor/gift-cards branch May 29, 2026 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request task

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants