import { combineReducers , Reducer } from 'redux' ; import { LogGroupAction , LogStreamAction , LogTextAction , LogEventAction , WindowAction , DateRangeAction , SettingsAction , MessageAction } from '../actions' ; import { LogGroupsState , LogStreamsState , LogTextState , LogEventsState , WindowState , DateRangeState , SettingsState , AsyncCallState , MessageState , initialState } from '../types' ; import { ActionTypes } from '../constants' ; import { DownloadJob , Filter } from '../common-interfaces' ; import { reducer as formReducer } from 'redux-form' ; export function window ( state , action ) { if ( ! state ) { return { ... initialState . window } ; } switch ( action . type ) { case ActionTypes . SHOW_WINDOW_CONTENT : return { ... state , windowContent : action . windowContent } ; default : return state ; } } export function logGroups ( state , action ) { if ( ! state ) { return { ... initialState . logGroups } ; } switch ( action . type ) { case ActionTypes . REQUEST_LOG_GROUPS : return { ... state } ; case ActionTypes . RECEIVE_LOG_GROUPS : return { ... state , logGroups : action . logGroups , lastModified : action . lastModified , errorMessage : undefined } ; case ActionTypes . ERROR_LOG_GROUPS : return { ... state , errorMessage : action . errorMessage } ; case ActionTypes . SELECT_LOG_GROUP : return { ... state , selectedName : action . selectedName } ; default : return state ; } } export function logStreams ( state , action ) { if ( ! state ) { return { ... initialState . logStreams } ; } switch ( action . type ) { case ActionTypes . REQUEST_LOG_STREAMS : return { ... state } ; case ActionTypes . RECEIVE_LOG_STREAMS : return { ... state , logStreams : action . logStreams , lastModified : action . lastModified , errorMessage : undefined } ; case ActionTypes . ERROR_LOG_STREAMS : return { ... state , errorMessage : action . errorMessage } ; case ActionTypes . SELECT_LOG_STREAM : return { ... state , selectedName : action . selectedName } ; case ActionTypes . SELECT_LOG_GROUP : return { ... state , selectedName : undefined } ; default : return state ; } } export function logText ( state , action ) { if ( ! state ) { return { ... initialState . logText } ; } switch ( action . type ) { case ActionTypes . REQUEST_LOG_TEXT : return { ... state } ; case ActionTypes . RECEIVE_LOG_TEXT : return { ... state , text : action . text } ; case ActionTypes . ERROR_LOG_TEXT : return { ... state , text : '' } ; case ActionTypes . SELECT_LOG_GROUP : return { ... state , text : '' } ; default : return state ; } } export function logEvents ( state , action ) { if ( ! state ) { return { ... initialState . logEvents } ; } let filteredRunningJobs = ( job ) => state . runningJobs . filter ( j => j . id !== job . id ) ; switch ( action . type ) { case ActionTypes . REQUEST_LOG_EVENTS : return { ... state , runningJobs : state . runningJobs . concat ( action . job ) } ; case ActionTypes . PROGRESS_LOG_EVENTS : return { ... state , runningJobs : filteredRunningJobs ( action . job ) . concat ( action . job ) } ; case ActionTypes . RECEIVE_LOG_EVENTS : return { ... state , runningJobs : filteredRunningJobs ( action . job ) , finishedJobs : state . finishedJobs . concat ( action . job ) } ; case ActionTypes . ERROR_LOG_EVENTS : return { ... state , runningJobs : filteredRunningJobs ( action . job ) , errorJobs : state . errorJobs . concat ( action . job ) } ; default : return state ; } } export function dateRange ( state , action ) { if ( ! state ) { return { ... initialState . dateRange } ; } switch ( action . type ) { case ActionTypes . SET_DATERANGE : return { ... state , startDate : action . startDate , endDate : action . endDate } ; default : return state ; } } export function settings ( state , action ) { if ( ! state ) { return { ... initialState . settings } ; } let validFilters = ( filters ) => filters . filter ( f => f . type ) ; switch ( action . type ) { case ActionTypes . SAVE_SETTINGS : return { ... state , settings : { ... state . settings , ... action . settings , filters : validFilters ( action . settings . filters ) } , lastModified : action . lastModified } ; case ActionTypes . RECEIVE_SETTINGS : return { ... state , settings : { ... state . settings , ... action . settings , filters : validFilters ( action . settings . filters ) } , lastModified : action . lastModified } ; default : return state ; } } export function asyncCalls ( state , action ) { if ( ! state ) { return { ... initialState . asyncCalls } ; } switch ( action . type ) { case ActionTypes . REQUEST_LOG_GROUPS : case ActionTypes . REQUEST_LOG_STREAMS : case ActionTypes . REQUEST_LOG_TEXT : return { ... state , active : state . active + 1 } ; case ActionTypes . RECEIVE_LOG_GROUPS : case ActionTypes . RECEIVE_LOG_STREAMS : case ActionTypes . RECEIVE_LOG_TEXT : case ActionTypes . ERROR_LOG_GROUPS : case ActionTypes . ERROR_LOG_STREAMS : case ActionTypes . ERROR_LOG_TEXT : return { ... state , active : ( state . active > 0 ) ? state . active - 1 : 0 } ; default : return state ; } } export function message ( state , action ) { if ( ! state ) { return { ... initialState . message } ; } switch ( action . type ) { case ActionTypes . SHOW_MESSAGE : return { ... state , message : action . message , subMessage : undefined , visible : true } ; case ActionTypes . ERROR_LOG_GROUPS : return { ... state , message : 'Error' , subMessage : action . errorMessage , visible : true } ; case ActionTypes . ERROR_LOG_STREAMS : return { ... state , message : 'Error' , subMessage : action . errorMessage , visible : true } ; case ActionTypes . ERROR_LOG_TEXT : return { ... state , message : 'Error' , subMessage : action . errorMessage , visible : true } ; case ActionTypes . HIDE_MESSAGE : return { ... state , message : '' , subMessage : undefined , visible : false } ; default : return state ; } } const reducers = combineReducers ( { window , logGroups , logStreams , logText , logEvents , dateRange , settings , asyncCalls , message , form : formReducer , } ) ; export default reducers ;