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 {
totalDip: number;
dips: DipModel[];
}
@State<AccessStateModel>({
name: 'access',
defaults: {
totalDip: 0,
dips: [],
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,
});
// 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)
.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,
});
}