import baseReducer from '#utils/baseReducer' ; import { Customer , Market , GiftCardRecipient , GiftCardRecipientCreate , Order , GiftCardRecipientUpdate , } from '@commercelayer/sdk' ; import { BaseMetadata } from '#typings' ; import { Dispatch } from 'react' ; import { CommerceLayerConfig } from '#context/CommerceLayerContext' ; import { isEmpty } from 'lodash' ; import { BaseError } from '#typings/errors' ; import getErrors from '#utils/getErrors' ; import getSdk from '#utils/getSdk' ; import { CreateOrder } from './OrderReducer' ; export type GiftCardActionType = 'setAvailability' | 'setGiftCardRecipient' | 'setGiftCardErrors' | 'setGiftCardLoading' ; export interface GiftCardRecipientI { email : string ; firstName ? : string ; lastName ? : string ; referenceOrigin ? : string ; reference ? : string ; metadata ? : BaseMetadata ; customer ? : Customer ; } export interface GiftCardI { currencyCode ? : string ; balanceCent ? : number ; balanceMaxCents ? : number ; singleUse ? : boolean ; rechargeable ? : boolean ; imageUrl ? : string ; expiresAt ? : null | Date ; firstName ? : string ; lastName ? : string ; email ? : string ; referenceOrigin ? : string ; recipientEmail ? : string ; reference ? : string ; metadata ? : BaseMetadata ; orderId ? : string ; } export interface GiftCardActionPayload extends GiftCardI { market ? : Market ; giftCardRecipient ? : GiftCardRecipient ; errors ? : BaseError [ ] ; loading ? : boolean ; } export interface GiftCardState extends GiftCardActionPayload { currencyCode : string ; balanceCent : number ; addGiftCardRecipient ? : ( values : GiftCardRecipientI & Record < string , any > ) => void ; addGiftCard ? : ( values : GiftCardI & Record < string , any > ) => void ; addGiftCardError ? : ( errors : BaseError [ ] ) => void ; addGiftCardLoading ? : ( loading : boolean ) => void ; } export interface GiftCardAction { type : GiftCardActionType ; payload : GiftCardActionPayload ; } export const giftCardInitialState = { currencyCode : '' , balanceCent : 0 , singleUse : false , rechargeable : true , loading : false , expiresAt : null , errors : [ ] , } ; export interface AddGiftCardRecipient { < V extends GiftCardRecipientCreate > ( values , config , dispatch ) : void ; } export interface AddGiftCard { < V extends GiftCardI > ( values , configParameters ) : void ; } export interface AddGiftCardError { < V extends BaseError [ ] > ( errors , dispatch ) : void ; } export interface AddGiftCardLoading { < V extends boolean > ( loading , dispatch ) : void ; } export const addGiftCardRecipient = async ( values , config , dispatch ) => { try { const sdk = getSdk ( config ) ; const recipient = await sdk . gift_card_recipients . create ( values ) ; dispatch ( { type : 'setGiftCardRecipient' , payload : { giftCardRecipient : recipient , } , } ) ; } catch ( error ) { console . error ( error ) ; } } ; export const addGiftCardLoading = ( loading , dispatch ) => { dispatch ( { type : 'setGiftCardLoading' , payload : { loading , } , } ) ; } ; export const addGiftCard = async ( values , { config , dispatch , getOrder , createOrder , order } ) => { try { const sdk = getSdk ( config ) ; addGiftCardLoading ( true , dispatch ) ; const { firstName , lastName , email , ... val } = values ; const giftCardValue = { recipient_email : email , ... val , } ; const giftCard = await sdk . gift_cards . create ( giftCardValue , { include : [ 'gift_card_recipient' ] , } ) ; const recipientValues = { id : giftCard . gift_card_recipient ?. id as string , } ; if ( firstName ) recipientValues [ 'first_name' ] = firstName ; if ( lastName ) recipientValues [ 'last_name' ] = lastName ; if ( ! isEmpty ( recipientValues ) ) { await sdk . gift_card_recipients . update ( recipientValues ) ; } if ( createOrder && getOrder ) { const id = order ? order . id : await createOrder ( ) ; if ( id ) { const order = sdk . orders . relationship ( id ) ; const item = sdk . gift_cards . relationship ( giftCard . id ) ; await sdk . line_items . create ( { quantity : 1 , order , item , } ) ; getOrder && getOrder ( id ) ; } } dispatch ( { type : 'setGiftCardRecipient' , payload : { ... giftCardValue , } , } ) ; addGiftCardLoading ( false , dispatch ) ; } catch ( error ) { const errors = getErrors ( error , 'gift_cards' ) ; dispatch ( { type : 'setGiftCardErrors' , payload : { errors , } , } ) ; addGiftCardLoading ( false , dispatch ) ; } } ; export const addGiftCardError = ( errors , dispatch ) => { dispatch ( { type : 'setGiftCardErrors' , payload : { errors , } , } ) ; } ; const type = [ 'setAvailability' , 'setGiftCardRecipient' , 'setGiftCardErrors' , 'setGiftCardLoading' , ] ; const giftCardReducer = ( state , reducer ) : GiftCardState => baseReducer < GiftCardState , GiftCardAction , GiftCardActionType [ ] > ( state , reducer , type ) ; export default giftCardReducer ;