Skip to content
Snippets Groups Projects
access.state.ts 2.63 KiB
Newer Older
import {GetAllDip, SearchDip, SearchDipFail, SearchDipSuccess} from "@app/access/access.action";
import {BaseStateModel} from "@app/base.state";
import {StateEnum} from "@app/shared/enums/state.enum";
import {DipModel} from "@app/generated-api";
import {CollectionTypedModel} from "@app/shared/models/collection-typed.model";
import {Action, State, StateContext} from "@ngxs/store";
import {tap} from "rxjs/internal/operators/tap";
import {catchError} from "rxjs/operators";
import {ApiService} from "@app/shared/services/api.service";
import {AccessResourceApiEnum} from "@app/shared/enums/api.enum";
export interface AccessStateModel extends BaseStateModel {
}

@State<AccessStateModel>({
  name: StateEnum.access,
  defaults: {
    isLoading: false,
  },
})
export class AccessState {
Florent Poittevin's avatar
Florent Poittevin committed
  constructor(private apiService: ApiService) {
  }

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

Florent Poittevin's avatar
Florent Poittevin committed
    return this.apiService.get<DipModel>(AccessResourceApiEnum.dip)
      .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,
Florent Poittevin's avatar
Florent Poittevin committed
    // return this.dlcmService.accessDipGetFilter(null, null, null, filters)
    return this.apiService.get<DipModel>(AccessResourceApiEnum.dip, null, null, null, filters)
      .pipe(
        tap((collectionDips: CollectionTypedModel<DipModel>) => {
          ctx.dispatch(new SearchDipSuccess(collectionDips));
        catchError(error => {
          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,
    });
  }