import { DaffProduct } from '@daffodil/product' ; import { DaffBestSellersActionTypes , DaffBestSellersActions , } from '../../actions/best-sellers.actions' ; import { DaffBestSellersReducerState } from './best-sellers-reducer-state.interface' ; export const initialState = { productIds : [ ] , loading : false , errors : [ ] , } ; export const resetState = Object . assign ( { } , initialState ) ; /**
 * The default best sellers reducer provided by Daffodil.
 */ export function daffBestSellersReducer < T extends DaffProduct > ( state = initialState , action ) { switch ( action . type ) { case DaffBestSellersActionTypes . BestSellersLoadAction : return { ... state , loading : true } ; case DaffBestSellersActionTypes . BestSellersLoadSuccessAction : return { ... state , loading : false , productIds : getIds < T > ( action . payload ) } ; case DaffBestSellersActionTypes . BestSellersLoadFailureAction : return { ... state , loading : false , errors : state . errors . concat ( new Array ( action . payload ) ) } ; case DaffBestSellersActionTypes . BestSellersResetAction : return { ... resetState , } ; default : return state ; } } function getIds < T extends DaffProduct > ( products ) { const ids = new Array ( ) ; products . forEach ( product => { ids . push ( product . id ) ; } ) ; return ids ; }