import { catchError, map, switchMap } from 'rxjs/operators'; import { combineEpics, ofType } from 'redux-observable'; import { dashboardFetchCurrentVesselsStatusFailure, dashboardFetchCurrentVesselsStatusRequest, dashboardFetchCurrentVesselsStatusSuccess, dashboardFetchKeyFiguresFailure, dashboardFetchKeyFiguresRequest, dashboardFetchKeyFiguresSuccess, dashboardFetchRecentVesselsStatusFailure, dashboardFetchRecentVesselsStatusRequest, dashboardFetchRecentVesselsStatusSuccess, dashboardSetCurrentPoolKeyFigures, } from './dashboard.actions'; import { DASHBOARD } from './dashboard.constants'; import { ENTERED_DASHBOARD_PAGE } from 'app/common/store/router/router.constants'; import { orderBy } from 'lodash'; import moment from 'moment'; import { resetAllFilters, setPoolFilter } from 'app/shared/store/filters/filters.actions'; import { PretypedEpic } from 'app/common/store/store.types'; import { DashboardKeyFigure, DashboardKeyFigureDto } from './dashboard.types'; import { AnyAction } from 'redux'; const numberOfRecentMonthsToFetch = 1; const getCurrentPoolKeyFigures = (keyFigures: DashboardKeyFigure[]) => { if (!keyFigures?.length) { return null; } const firstForecast = keyFigures.find(item => item.isForecast); if (firstForecast) { return firstForecast.id; } return keyFigures[keyFigures.length - 1].id; }; const mapKeyFigures = (keyFigures: DashboardKeyFigureDto[]): DashboardKeyFigure[] => { const indexedKeyFigures = keyFigures.map((keyFigure, index) => ({ ...keyFigure, id: index })); return indexedKeyFigures.length > 5 ? orderBy(indexedKeyFigures, (item) => moment(item.date), [ 'asc' ]).slice(-5) : indexedKeyFigures; }; export const enteredDashboardPageEpic: PretypedEpic = (action$, store ) => { return action$.pipe( ofType(ENTERED_DASHBOARD_PAGE), map((action) => { let actions: AnyAction[] = [ dashboardFetchKeyFiguresRequest(action.payload), dashboardFetchCurrentVesselsStatusRequest(action.payload), dashboardFetchRecentVesselsStatusRequest(action.payload), setPoolFilter(action.payload), ]; if (store.value.filters.poolId !== action.payload) { actions.push(resetAllFilters()); } return actions; }), ); }; export const fetchKeyFiguresEpic: PretypedEpic = (action$, store, services) => { return action$.pipe( ofType(DASHBOARD.FETCH_KEY_FIGURES_REQUEST), switchMap((action) => services.dashboard.getKeyFigures(action.payload).pipe( map((keyFiguresDto) => { const keyFigures = mapKeyFigures(keyFiguresDto); return [ dashboardFetchKeyFiguresSuccess(keyFigures), dashboardSetCurrentPoolKeyFigures(getCurrentPoolKeyFigures(keyFigures)), ]; }), catchError(() => [ dashboardFetchKeyFiguresFailure(), ]), )) ); }; export const fetchCurrentVesselStatusEpic: PretypedEpic = (action$, store, services) => { return action$.pipe( ofType(DASHBOARD.FETCH_CURRENT_VESSELS_STATUS_REQUEST), switchMap((action) => services.dashboard.getCurrentVesselStatus(action.payload).pipe( map((currentVesselsStatus) => { return [ dashboardFetchCurrentVesselsStatusSuccess(currentVesselsStatus), ]; }), catchError(() => [ dashboardFetchCurrentVesselsStatusFailure(), ]), )) ); }; export const fetchRecentVesselStatusEpic: PretypedEpic = (action$, store, services) => { return action$.pipe( ofType(DASHBOARD.FETCH_RECENT_VESSELS_STATUS_REQUEST), switchMap((action) => services.dashboard.getRecentVesselStatus(action.payload, numberOfRecentMonthsToFetch).pipe( map((recentVesselsStatus) => { return [ dashboardFetchRecentVesselsStatusSuccess(recentVesselsStatus), ]; }), catchError(() => [ dashboardFetchRecentVesselsStatusFailure(), ]), )) ); }; const dashboardEpic = combineEpics( enteredDashboardPageEpic, fetchKeyFiguresEpic, fetchCurrentVesselStatusEpic, fetchRecentVesselStatusEpic, ); export { dashboardEpic };