import { Actions , BooksState , State , Model } from 'ngrx-domains' ; export function reducer ( state = State . books , action ) { switch ( action . constructor ) { case Actions . books . SearchCompleteAction : case Actions . collection . LoadSuccessAction : { const books = action . payload ; const newBooks = books . filter ( book => ! state . entities [ book . id ] ) ; const newBookIds = newBooks . map ( book => book . id ) ; const newBookEntities = newBooks . reduce ( ( entities , book ) => { return Object . assign ( entities , { [ book . id ] : book } ) ; } , { } ) ; return { ids : [ ... state . ids , ... newBookIds ] , entities : Object . assign ( { } , state . entities , newBookEntities ) , selectedBookId : state . selectedBookId } ; } case Actions . books . LoadAction : { const book = action . payload ; if ( state . ids . indexOf ( book . id ) > - 1 ) { return state ; } return { ids : [ ... state . ids , book . id ] , entities : Object . assign ( { } , state . entities , { [ book . id ] : book } ) , selectedBookId : state . selectedBookId } ; } case Actions . books . SelectAction : { return { ids : state . ids , entities : state . entities , selectedBookId : action . payload } ; } default : { return state ; } } }