import { createReducer } from '@reduxjs/toolkit' ; import type { ActionHandler } from '@core' ; import { updateBillingFullNameAction , updateBillingAddressAction , updateCardNumberAction , updateCardNameAction , updateCardExpiryAction , updateCardCvcAction , clearCheckoutInfoAction , } from './actions' ; import { UpdateBillingFullNameActionPayload , UpdateBillingAddressActionPayload , UpdateCardNumberActionPayload , UpdateCardNameActionPayload , UpdateCardExpiryActionPayload , UpdateCardCvcActionPayload , } from './types' ; export interface CheckoutState { billingInfo : { fullName : string ; address : string ; } ; paymentInfo : { /**
         * the card property could be ommited for our project since it is the only
         * supported payment method, but in a professional project the paymentInfo
         * would have more options.
         */ card : { number : string ; name : string ; expiry : string ; cvc : string ; } ; } ; } export const initialState = { billingInfo : { fullName : '' , address : '' , } , paymentInfo : { card : { number : '' , name : '' , expiry : '' , cvc : '' , } , } , } ; const handleUpdateBillingFullName = ( state , { payload : { fullName } } ) => { state . billingInfo . fullName = fullName ; } ; const handleUpdateBillingAddress = ( state , { payload : { address } } ) => { state . billingInfo . address = address ; } ; const handleUpdateCardNumber = ( state , { payload : { number } } ) => { state . paymentInfo . card . number = number ; } ; const handleUpdateCardName = ( state , { payload : { name } } ) => { state . paymentInfo . card . name = name ; } ; const handleUpdateCardExpiryName = ( state , { payload : { expiry } } ) => { state . paymentInfo . card . expiry = expiry ; } ; const handleUpdateCardCvcName = ( state , { payload : { cvc } } ) => { state . paymentInfo . card . cvc = cvc ; } ; const checkoutReducer = createReducer ( initialState , { [ updateBillingFullNameAction . type ] : handleUpdateBillingFullName , [ updateBillingAddressAction . type ] : handleUpdateBillingAddress , [ updateCardNumberAction . type ] : handleUpdateCardNumber , [ updateCardNameAction . type ] : handleUpdateCardName , [ updateCardExpiryAction . type ] : handleUpdateCardExpiryName , [ updateCardCvcAction . type ] : handleUpdateCardCvcName , [ clearCheckoutInfoAction . type ] : ( ) => initialState , } ) ; export default checkoutReducer ;