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";

Florent Poittevin
committed
export interface AccessStateModel extends BaseStateModel {
totalDip: number;
dips: DipModel[];
totalDip: 0,
dips: [],
}
@Action(GetAllDip)
getAllDip(ctx: StateContext<AccessStateModel>, action: GetAllDip) {
ctx.patchState({
isLoading: true,
});
return this.apiService.get<DipModel>(AccessResourceApiEnum.dip)
.pipe(
tap((collectionDips: CollectionTypedModel<DipModel>) => {
ctx.patchState({
dips: collectionDips._data,
totalDip: collectionDips._page.totalItems,
isLoading: false,
});
// const state = ctx.getState();
// ctx.setState({
// ...state,
// dips: collectionDips.data as Dip[],
// isLoading: false,
// });
}),
);
@Action(SearchDip, {cancelUncompleted: true})
searchDip(ctx: StateContext<AccessStateModel>, action: SearchDip) {
const filters = {
// 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));
@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,
});
}