GiftCards
Functions
- useGiftCards(props) ⇒
GiftCardsTalonProps
Handles the logic for a component that renders a list of gift cards. It performs effects and returns the prop data necessary for rendering the component.
This talon performs the following effects:
- Fetch the currently applied gift cards for a cart
- Manage the updating state of the cart while a gift card is being applied or removed
- useGiftCard(props) ⇒
GiftCardTalonProps
Provide logic for a single gift card component.
Typedefs
- GiftCardsMutations :
Object
GraphQL mutations for Gift Cards.
- GiftCardsQueries :
Object
GraphQL queries for Gift Cards.
- GiftCardsTalonProps :
Object
Props data to use when rendering a list of gift cards.
- GiftCardTalonProps :
Object
Props data to use when rendering a single gift card component.
Handles the logic for a component that renders a list of gift cards. It performs effects and returns the prop data necessary for rendering the component.
This talon performs the following effects:
- Fetch the currently applied gift cards for a cart
- Manage the updating state of the cart while a gift card is being applied or removed
Returns: Parameters
Name | Type | Description |
---|---|---|
props | Object | |
props.setIsCartUpdating | function | Callback function for setting the update state for the cart. |
props.mutations | GiftCardsMutations | GraphQL mutations for Gift Cards |
props.queries | GiftCardsQueries | GraphQL queries for Gift Cards |
Example (Importing into your project)
Copied to your clipboardimport { useGiftCards } from '@magento/peregrine/lib/talons/CartPage/GiftCards'
Provide logic for a single gift card component.
Returns: Parameters
Name | Type | Description |
---|---|---|
props | Object | |
props.code | String | Gift card's code |
props.removeGiftCard | function | A function that removes a gift card when provided a code |
Example (Importing into your project)
Copied to your clipboardimport { useGiftCard } from '@magento/peregrine/lib/talons/CartPage/GiftCards/useGiftCard';
GraphQL mutations for Gift Cards.
See: giftCardQueries.ee.js
for queries used in Venia
Properties
Name | Type | Description |
---|---|---|
applyGiftCardMutation | GraphQLAST | The mutation used to apply a gift card to the cart. |
removeGiftCardMutation | GraphQLAST | The mutation used to remove a gift card from the cart. |
GraphQL queries for Gift Cards.
See: giftCardQueries.ee.js
for queries used in Venia
Properties
Name | Type | Description |
---|---|---|
getAppliedGiftCardsQuery | GraphQLAST | The query used to get the gift cards currently applied to the cart. |
getGiftCardBalanceQuery | GraphQLAST | The query used to get the gift cards currently applied to the cart. |
Props data to use when rendering a list of gift cards.
Properties
Name | Type | Description |
---|---|---|
applyGiftCard | function | A callback to apply a gift card to the cart. |
checkBalanceData | Object | The giftCardAccount object of the most recent successful check balance GraphQL query. |
checkGiftCardBalance | function | A callback to check the balance of a gift card. |
errorLoadingGiftCards | boolean | Whether there was an error loading the cart's gift cards. |
errorApplyingCard | boolean | Whether there was an error applying the gift card. |
errorCheckingBalance | boolean | Whether there was an error checking the balance of the gift card. |
errorRemovingCard | boolean | Whether there was an error removing the gift card. |
giftCardsData | Array | The applied_gift_cards object of the cart query. |
isLoadingGiftCards | boolean | Whether the cart's gift card data is loading. |
isApplyingCard | boolean | Whether the apply gift card operation is in progress. |
isCheckingBalance | boolean | Whether the check gift card balance operation is in progress. |
isRemovingCard | boolean | Whether the remove gift card operation is in progress. |
removeGiftCard | function | A callback to remove a gift card from the cart. |
shouldDisplayCardBalance | boolean | Whether to display the gift card balance to the user |
shouldDisplayCardError | boolean | Whether to display an error message under the card input field. |
Props data to use when rendering a single gift card component.
Properties
Name | Type | Description |
---|---|---|
removeGiftCardWithCode | function | Function for removing a gift card associated with the code passed into this talon. |
Source Code: pwa-studio/packages/peregrine/lib/talons/CartPage/GiftCards/useGiftCards.js
Examples#
useGiftCards()#
Copied to your clipboard1import React from 'react'2import { useGiftCards } from '@magento/peregrine/lib/talons/CartPage/GiftCards'3import {4 GET_CART_GIFT_CARDS_QUERY,5 GET_GIFT_CARD_BALANCE_QUERY,6 APPLY_GIFT_CARD_MUTATION,7 REMOVE_GIFT_CARD_MUTATION8} from './myGiftCardQueries';910const MyGiftCards = props => {1112 const talonProps = useGiftCards({13 setIsCartUpdating: props.setIsCartUpdating,14 mutations: {15 applyCardMutation: APPLY_GIFT_CARD_MUTATION,16 removeCardMutation: REMOVE_GIFT_CARD_MUTATION17 },18 queries: {19 appliedCardsQuery: GET_CART_GIFT_CARDS_QUERY,20 cardBalanceQuery: GET_GIFT_CARD_BALANCE_QUERY21 }22 });2324 const propsFromTalon = useGiftCards(talonProps)2526 const {27 applyGiftCard,28 checkBalanceData,29 checkGiftCardBalance,30 errorLoadingGiftCards,31 errorRemovingCard,32 giftCardsData,33 isLoadingGiftCards,34 isApplyingCard,35 isCheckingBalance,36 isRemovingCard,37 removeGiftCard,38 setFormApi,39 shouldDisplayCardBalance,40 shouldDisplayCardError41 } = propsFromTalon;4243 return (44 // JSX using the props from the talon45 )46}4748export default MyGiftCards
useGiftCard()#
Copied to your clipboard1import React from 'react'2import { useGiftCard } from '@magento/peregrine/lib/talons/CartPage/GiftCards/useGiftCard';34const MyGiftCard = props => {5 const { code } = props;67 const removeGiftCard = code => {8 // Logic for removing a gift card using its code9 // This can also be passed in as a prop10 }1112 const { removeGiftCardWithCode } = useGiftCard({13 code,14 removeGiftCard15 });1617 return (18 // JSX for rendering a single Gift Card using data from the talon19 )20}2122export default MyGiftCard