import { DaffCountry } from '@daffodil/geography' ; import { DaffGeographyActions , DaffGeographyActionTypes , } from '../../actions/public_api' ; import { getCountryAdapter } from './country-entities-adapter' ; import { daffCountryEntitiesInitialState } from './country-entities-initial-state' ; import { DaffCountryEntityState } from './country-entities-state.interface' ; /**
 * Reducer function that catches actions and changes/overwrites country entities state.
 */ export function daffCountryEntitiesReducer < T extends DaffCountry = DaffCountry > ( state = daffCountryEntitiesInitialState , action ) { const adapter = getCountryAdapter < T > ( ) ; switch ( action . type ) { case DaffGeographyActionTypes . CountryLoadSuccessAction : return adapter . upsertOne ( { ... action . payload , loaded : true , } , state ) ; case DaffGeographyActionTypes . CountryListSuccessAction : return adapter . upsertMany ( action . payload . map ( country => ( { ... country , loaded : ( state . entities [ country . id ] && state . entities [ country . id ] . loaded ) || false , subdivisions : country . subdivisions . length === 0 && state . entities [ country . id ] && state . entities [ country . id ] . subdivisions . length > 0 ? state . entities [ country . id ] . subdivisions : country . subdivisions , } ) ) , state ) ; default : return state ; } }