import { NewsState } from './news.state' ; import * as newsAction from './news.action' ; import { createReducer , on , Action } from '@ngrx/store' ; export const initialState = { newsItems : [ ] , groups : [ 'IT' , 'global' , 'sport' ] , } ; const newsReducerInternal = createReducer ( initialState , on ( newsAction . recieveGroupJoinedAction , ( state , { payload } ) => { const allGroups = [ ... state . groups , payload ] ; const allGroupsWithoutDuplicates = [ ... new Set ( allGroups ) ] ; return { ... state , groups : [ ... allGroupsWithoutDuplicates ] , } ; } ) , on ( newsAction . recieveNewsItemAction , ( state , { payload } ) => { return { ... state , newsItems : [ ... state . newsItems , payload ] , } ; } ) , on ( newsAction . recieveNewsGroupHistoryAction , ( state , { payload } ) => { return { ... state , newsItems : [ ... payload ] , } ; } ) , on ( newsAction . selectAllNewsGroupsFinishedAction , ( state , { payload } ) => { const allGroups = [ ... state . groups , ... payload ] ; const allGroupsWithoutDuplicates = [ ... new Set ( allGroups ) ] ; return { ... state , groups : [ ... allGroupsWithoutDuplicates ] , } ; } ) , on ( newsAction . recieveGroupLeftAction , ( state , { payload } ) => { const data = [ ] ; for ( const entry of state . groups ) { if ( entry !== payload ) { data . push ( entry ) ; } } console . log ( data ) ; return { ... state , groups : [ ... data ] , } ; } ) ) ; export function newsReducer ( state , action ) { return newsReducerInternal ( state , action ) ; }