import baseReducer from '#utils/baseReducer' ; import { Dispatch } from 'react' ; import { CommerceLayerConfig } from '#context/CommerceLayerContext' ; import getSdk from '#utils/getSdk' ; type SkuListsActionType = 'getSkuList' | 'setSkuList' ; const actionType = [ 'getSkuList' , 'setSkuList' ] ; export type SkuListsState = Partial < { listIds : string [ ] ; skuLists : Record < string , any > ; } > ; export const skuListsInitialState = { listIds : [ ] , } ; export interface SkuListsAction { payload : Partial < SkuListsState > ; type : SkuListsActionType ; } export type GetSkuList = ( params : { config : CommerceLayerConfig ; dispatch : Dispatch < SkuListsAction > ; listIds : string [ ] ; state : SkuListsState ; } ) => Promise < void > ; export const getSkuList = async ( { listIds , config , dispatch } ) => { const skuLists = { } ; try { const sdk = getSdk ( config ) ; listIds . forEach ( async ( id ) => { const skuList = await sdk . sku_lists . retrieve ( id , { include : [ 'skus' ] , fields : { skus : [ 'code' ] } , } ) ; const skuCodes = skuList . skus ; skuLists [ id ] = skuCodes ; } ) ; dispatch ( { payload : { skuLists , } , type : 'getSkuList' , } ) ; } catch ( error ) { console . error ( error ) ; } } ; const skuListsReducer = ( state , reducer ) : SkuListsState => baseReducer < SkuListsState , SkuListsAction , SkuListsActionType [ ] > ( state , reducer , actionType ) ; export default skuListsReducer ;