|
| 1 | +/** |
| 2 | + * Human Cell Atlas |
| 3 | + * https://www.humancellatlas.org/ |
| 4 | + * |
| 5 | + * Entity-related effects. An entity is currently one of Projects, Samples or Files. |
| 6 | + */ |
| 7 | + |
| 8 | +// Core dependencies |
| 9 | +import { Injectable } from "@angular/core"; |
| 10 | +import { Actions, Effect, ofType } from "@ngrx/effects"; |
| 11 | +import { Action, select, Store } from "@ngrx/store"; |
| 12 | +import { Observable, of } from "rxjs"; |
| 13 | +import { map, take } from "rxjs/operators"; |
| 14 | +import { concatMap, withLatestFrom } from "rxjs/operators"; |
| 15 | + |
| 16 | +// App dependencies |
| 17 | +import { AppState } from "../../../_ngrx/app.state"; |
| 18 | +import { SelectEntityAction } from "./select-entity.action"; |
| 19 | +import { InitEntityStateAction, NoOpAction } from "../facet/file-facet-list.actions"; |
| 20 | +import { selectTableQueryParams } from "../file.selectors"; |
| 21 | +import { GTMService } from "../../../shared/analytics/gtm.service"; |
| 22 | +import { getSelectedTable } from "../table/table.state"; |
| 23 | +import { BackToEntityAction } from "./back-to-entity.action"; |
| 24 | + |
| 25 | +@Injectable() |
| 26 | +export class EntityEffects { |
| 27 | + |
| 28 | + /** |
| 29 | + * @param {Store<AppState>} store |
| 30 | + * @param {Actions} actions$ |
| 31 | + * @param {GTMService} gtmService |
| 32 | + */ |
| 33 | + constructor(private store: Store<AppState>, |
| 34 | + private actions$: Actions, |
| 35 | + private gtmService: GTMService) { |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Handle action where tab is selected (ie Projects, Samples, Files). |
| 40 | + */ |
| 41 | + @Effect() |
| 42 | + switchTabs: Observable<Action> = this.actions$ |
| 43 | + .pipe( |
| 44 | + ofType( |
| 45 | + SelectEntityAction.ACTION_TYPE, |
| 46 | + BackToEntityAction.ACTION_TYPE |
| 47 | + ), |
| 48 | + concatMap(action => of(action).pipe( |
| 49 | + withLatestFrom(this.store.pipe(select(selectTableQueryParams), take(1))) |
| 50 | + )), |
| 51 | + map(([action, tableQueryParams]) => { |
| 52 | + |
| 53 | + // Track change of tab |
| 54 | + this.gtmService.trackEvent((action as SelectEntityAction).asEvent()); |
| 55 | + |
| 56 | + // Return cached table, if available |
| 57 | + if ( getSelectedTable(tableQueryParams.tableState).data.length ) { |
| 58 | + return new NoOpAction(); |
| 59 | + } |
| 60 | + |
| 61 | + // Table data has not been previously loaded and is therefore not cached. |
| 62 | + return new InitEntityStateAction(); |
| 63 | + }) |
| 64 | + ); |
| 65 | +} |
0 commit comments