Skip to content
Snippets Groups Projects
effects.ts 2.61 KiB
Newer Older
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { visibleAudit } from './selectors';
import { UpdateUserPreferences } from './../../core/actions';
import { GlobalState } from '@app/core/state';
import { Store } from '@ngrx/store';
import { forkJoinWithKeys } from '@app/degree-planner/shared/utils';
import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { DarsActionTypes } from '@app/dars/store/actions';
import * as darsActions from '@app/dars/store/actions';
import { flatMap, map, catchError, switchMap, mergeMap } from 'rxjs/operators';
import { DarsApiService } from '../services/api.service';
import { of } from 'rxjs';
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
import { Alert, DarsDisclaimerAlert } from '@app/core/models/alert';
import { DegreePlannerApiService } from '@app/degree-planner/services/api.service';
import { INITIAL_DARS_STATE } from './state';

@Injectable()
export class DARSEffects {
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed
  constructor(
    private actions$: Actions,
    private api: DarsApiService,
    private degreeAPI: DegreePlannerApiService,
    private store$: Store<GlobalState>,
  ) {}

  @Effect()
  load$ = this.actions$.pipe(
    ofType(DarsActionTypes.StartLoadingMetadata),
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed

    flatMap(() => {
      return forkJoinWithKeys({
        metadata: this.api.getAudits(),
        userPreferences: this.degreeAPI.getUserPreferences(),
      });
    }),

    flatMap(({ metadata, userPreferences }) => {
      const alerts: Alert[] = [];
      if (userPreferences.darsHasDismissedDisclaimer !== true) {
        alerts.push(
          new DarsDisclaimerAlert(() => {
            this.store$.dispatch(
              new UpdateUserPreferences({
                darsHasDismissedDisclaimer: true,
              }),
            );
          }),
        );
      }

      return forkJoinWithKeys({
        metadata: of(metadata),
        visibleAudit: of({}),
        alerts: of(alerts),
      }).pipe(
        switchMap(payload => {
          return [
            new darsActions.AddAuditMetadata({ metadata }),
            new UpdateUserPreferences(userPreferences),
          ];
jvanboxtel@wisc.edu's avatar
jvanboxtel@wisc.edu committed

    // catchError(() => {
    //   return of(
    //     new darsActions.ErrorLoadingMetadata({
    //       message: 'Unable to load audit metadata. Please try again',
    //     }),
    //   );
    // }),

  @Effect()
  getAudit$ = this.actions$.pipe(
    ofType(DarsActionTypes.StartLoadingAudit),
    flatMap((action: darsActions.StartLoadingAudit) => {
      const metadata = action.payload;
      return this.api.getAudit(metadata.darsDegreeAuditReportId).pipe(
        map(audit => {
          return new darsActions.DoneLoadingAudit({ metadata, audit });
        }),
      );
    }),
  );