import { createContainerModule } from 'redux-with-domain' ; import update from 'immutability-helper-x' ; import _ from 'lodash' ; import datasetDomain , { DatasetItem } from '../../../domain/dataset' ; import chartDomain from '../../../domain/chart' ; export const kSelectedDatasetIdChangeEmitName = 'setSelectedDatasetId' ; /**
 * {page-module}
 */ export default createContainerModule ( 'workbook/datasetList' , { initialState : { selectedDatasetId : '' } , selectors : { getDatasetList : ( state , { payload : id } ) => { return datasetDomain . entities . datasetList . select ( ) ; } , getSelectedDatasetId : state => state . selectedDatasetId , getSelectedChartId : ( state , { pageSelectors } ) => { const id = pageSelectors . getSelectedChartId ( ) ; return id ; } } , reducers : { saveSelectedDataset : ( state , { payload } ) => { return update . $set ( state , 'selectedDatasetId' , payload ) ; } } , effects : ( { actions , selectors , sagaEffects : { put , select , call } , enhancer : { emit , syncPut } } ) => ( { * init ( ) { const datasetList = yield select ( selectors . getDatasetList as any ) ; if ( datasetList && datasetList . length > 0 ) { yield syncPut ( actions . saveSelectedDataset ( datasetList [ 0 ] . id ) ) ; } } , * setSelectedDataset ( { payload } ) { yield put ( actions . saveSelectedDataset ( payload ) ) ; const chartId = yield select ( selectors . getSelectedChartId as any ) ; if ( chartId ) { const chart = chartDomain . entities . chart . get ( chartId ) ; if ( chart . datasetId !== payload ) { yield emit ( { name : kSelectedDatasetIdChangeEmitName , payload } ) ; } } } } ) } ) ;