Skip to content
Snippets Groups Projects
access.state.ts 2.38 KiB
Newer Older
import {GetAllDip, SearchDip, SearchDipFail, SearchDipSuccess} from '@app/access/access.action';
import {AppStateModel} from '@app/app.state';
import {DipModel} from '@app/generated-api';
import {CollectionTypedModel} from '@app/model/collection-typed.model';
import {DlcmExtendedService} from '@app/service/dlcm.extended.service';
import {Action, State, StateContext} from '@ngxs/store';
import {tap} from 'rxjs/internal/operators/tap';
import {catchError} from 'rxjs/operators';
export interface AccessStateModel extends AppStateModel {
}

@State<AccessStateModel>({
  name: 'access',
  defaults: {
    isLoading: false,
  },
})
export class AccessState {
  constructor(private dlcmService: DlcmExtendedService) {
  }

  @Action(GetAllDip)
  getAllDip(ctx: StateContext<AccessStateModel>, action: GetAllDip) {
    ctx.patchState({
      isLoading: true,
    });

    return this.dlcmService.accessDipGet()
      .pipe(
        tap((collectionDips: CollectionTypedModel<DipModel>) => {
          ctx.patchState({
            dips: collectionDips._data,
            totalDip: collectionDips._page.totalItems,
            isLoading: false,
          });
          // Equals to :
          // const state = ctx.getState();
          // ctx.setState({
          //   ...state,
          //   dips: collectionDips.data as Dip[],
          //   isLoading: false,
          // });
        }),
      );
  @Action(SearchDip, {cancelUncompleted: true})
  searchDip(ctx: StateContext<AccessStateModel>, action: SearchDip) {
Florent Poittevin's avatar
Florent Poittevin committed
    ctx.patchState({
      isLoading: true,
    });
      'info.name': action.search,
    return this.dlcmService.accessDipGetFilter(null, null, null, filters)
      .pipe(
        tap((collectionDips: CollectionTypedModel<DipModel>) => {
          ctx.dispatch(new SearchDipSuccess(collectionDips));
        }),
        catchError(e => {
          ctx.dispatch(new SearchDipFail());

  @Action(SearchDipSuccess)
  searchDipSuccess(ctx: StateContext<AccessStateModel>, action: SearchDipSuccess) {
    ctx.patchState({
      dips: action.aips._data,
      totalDip: action.aips._page.totalItems,
      isLoading: false,
    });
  }

  @Action(SearchDipFail)
  searchDipFail(ctx: StateContext<AccessStateModel>) {
    ctx.patchState({
      isLoading: false,
    });
  }