import { isEqual } from 'lodash' ; import { from , isObservable , Observable , Subject } from 'rxjs' ; import { distinctUntilChanged , flatMap , map , scan , shareReplay } from 'rxjs/operators' ; import { DeepPartial } from '../../../common/types/common' ; import { jobSelectionActionCreator } from './actions' ; import { ExplorerChartsData } from './explorer_charts/explorer_charts_container_service' ; import { EXPLORER_ACTION } from './explorer_constants' ; import { AppStateSelectedCells } from './explorer_utils' ; import { explorerReducer , getExplorerDefaultState , ExplorerState } from './reducers' ; import { ExplorerAppState } from '../../../common/types/locator' ; export const ALLOW_CELL_RANGE_SELECTION = true ; type ExplorerAction = Action | Observable < ActionPayload > ; export const explorerAction$ = new Subject < ExplorerAction > ( ) ; export type ActionPayload = any ; export interface Action { type : string ; payload ? : ActionPayload ; } const explorerFilteredAction$ = explorerAction$ . pipe ( flatMap ( ( action ) => isObservable ( action ) ? action : ( from ( [ action ] ) as Observable < ExplorerAction > ) ) , distinctUntilChanged ( isEqual ) ) ; const explorerState$ = explorerFilteredAction$ . pipe ( scan ( explorerReducer , getExplorerDefaultState ( ) ) , shareReplay ( 1 ) ) ; const explorerAppState$ = explorerState$ . pipe ( map ( ( state ) : ExplorerAppState => { const appState = { mlExplorerFilter : { } , mlExplorerSwimlane : { } , } ; if ( state . selectedCells !== undefined ) { const swimlaneSelectedCells = state . selectedCells ; appState . mlExplorerSwimlane . selectedType = swimlaneSelectedCells . type ; appState . mlExplorerSwimlane . selectedLanes = swimlaneSelectedCells . lanes ; appState . mlExplorerSwimlane . selectedTimes = swimlaneSelectedCells . times ; appState . mlExplorerSwimlane . showTopFieldValues = swimlaneSelectedCells . showTopFieldValues ; } if ( state . viewBySwimlaneFieldName !== undefined ) { appState . mlExplorerSwimlane . viewByFieldName = state . viewBySwimlaneFieldName ; } if ( state . viewByFromPage !== undefined ) { appState . mlExplorerSwimlane . viewByFromPage = state . viewByFromPage ; } if ( state . viewByPerPage !== undefined ) { appState . mlExplorerSwimlane . viewByPerPage = state . viewByPerPage ; } if ( state . swimLaneSeverity !== undefined ) { appState . mlExplorerSwimlane . severity = state . swimLaneSeverity ; } if ( state . showCharts !== undefined ) { appState . mlShowCharts = state . showCharts ; } if ( state . filterActive ) { appState . mlExplorerFilter . influencersFilterQuery = state . influencersFilterQuery ; appState . mlExplorerFilter . filterActive = state . filterActive ; appState . mlExplorerFilter . filteredFields = state . filteredFields ; appState . mlExplorerFilter . queryString = state . queryString ; } return appState ; } ) , distinctUntilChanged ( isEqual ) ) ; const setExplorerDataActionCreator = ( payload ) => ( { type : EXPLORER_ACTION . SET_EXPLORER_DATA , payload , } ) ; const setFilterDataActionCreator = ( payload ) => ( { type : EXPLORER_ACTION . SET_FILTER_DATA , payload , } ) ; export const explorerService = { appState$ : explorerAppState$ , state$ : explorerState$ , clearExplorerData : ( ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . CLEAR_EXPLORER_DATA } ) ; } , clearInfluencerFilterSettings : ( ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . CLEAR_INFLUENCER_FILTER_SETTINGS } ) ; } , clearJobs : ( ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . CLEAR_JOBS } ) ; } , updateJobSelection : ( selectedJobIds ) => { explorerAction$ . next ( jobSelectionActionCreator ( selectedJobIds ) ) ; } , setCharts : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_CHARTS , payload } ) ; } , setInfluencerFilterSettings : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_INFLUENCER_FILTER_SETTINGS , payload , } ) ; } , setSelectedCells : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_SELECTED_CELLS , payload , } ) ; } , setExplorerData : ( payload ) => { explorerAction$ . next ( setExplorerDataActionCreator ( payload ) ) ; } , setFilterData : ( payload ) => { explorerAction$ . next ( setFilterDataActionCreator ( payload ) ) ; } , setChartsDataLoading : ( ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_CHARTS_DATA_LOADING } ) ; } , setSwimlaneContainerWidth : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_SWIMLANE_CONTAINER_WIDTH , payload , } ) ; } , setViewBySwimlaneFieldName : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_VIEW_BY_SWIMLANE_FIELD_NAME , payload } ) ; } , setViewBySwimlaneLoading : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_VIEW_BY_SWIMLANE_LOADING , payload } ) ; } , setViewByFromPage : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_VIEW_BY_FROM_PAGE , payload } ) ; } , setViewByPerPage : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_VIEW_BY_PER_PAGE , payload } ) ; } , setSwimLaneSeverity : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_SWIM_LANE_SEVERITY , payload } ) ; } , setShowCharts : ( payload ) => { explorerAction$ . next ( { type : EXPLORER_ACTION . SET_SHOW_CHARTS , payload } ) ; } , } ; export type ExplorerService = typeof explorerService ;