admin-funding-agencies.state.ts 6.27 KB
Newer Older
1
import {adminFundingAgenciesOrgUnitActionNameSpace} from "@admin/funding-agencies/stores/organizational-unit/admin-organizational-unit-preservation-policy.action";
Florent Poittevin's avatar
Florent Poittevin committed
2
3
4
import {
  AdminFundingAgenciesOrganizationalUnitState,
  AdminFundingAgenciesOrganizationalUnitStateModel,
5
} from "@admin/funding-agencies/stores/organizational-unit/admin-organizational-unit-preservation-policy.state";
Florent Poittevin's avatar
Florent Poittevin committed
6
7
8
import {
  AdminFundingAgenciesAction,
  adminFundingAgenciesActionNameSpace,
9
10
11
12
} from "@admin/funding-agencies/stores/admin-funding-agencies.action";
import {AdminResourceApiEnum} from "@shared/enums/api.enum";
import {LocalStateEnum} from "@shared/enums/local-state.enum";
import {RoutesEnum} from "@shared/enums/routes.enum";
Florent Poittevin's avatar
Florent Poittevin committed
13
import {FundingAgencyExtended} from "@deposit/models/funding-agency-extended.model";
14
import {Action, Actions, ofActionCompleted, State, StateContext, Store,} from "@ngxs/store";
Florent Poittevin's avatar
Florent Poittevin committed
15
16
17
18
19
import {LocalModelAttributeEnum} from "@shared/enums/model-attribute.enum";
import {SharedOrgUnitAction} from "@shared/stores/organizational-unit/shared-organizational-unit.action";
import _ from "lodash";
import {Observable} from "rxjs";
import {tap} from "rxjs/operators";
20
21
import {
  ApiService,
22
23
  AssociationActionHelper,
  ChangeModeEnum,
24
25
  defaultResourceStateInitValue,
  NotificationService,
26
  OverrideDefaultAction,
27
28
  ResourceState,
  ResourceStateModel,
29
  StoreUtil,
30
31
32
  TRANSLATE,
} from "solidify-frontend";

33
34
export interface AdminFundingAgenciesStateModel extends ResourceStateModel<FundingAgencyExtended> {
  admin_fundingAgencies_organizationalUnit?: AdminFundingAgenciesOrganizationalUnitStateModel;
35
36
37
38
39
40
41
}

@State<AdminFundingAgenciesStateModel>({
  name: LocalStateEnum.admin_fundingAgencies,
  defaults: {
    ...defaultResourceStateInitValue,
  },
42
  children: [
Florent Poittevin's avatar
Florent Poittevin committed
43
44
    AdminFundingAgenciesOrganizationalUnitState,
  ],
45
})
46
export class AdminFundingAgenciesState extends ResourceState<FundingAgencyExtended> {
47
48
49
50
51
52
53
54
55
56
57
58
59
  constructor(protected apiService: ApiService,
              protected store: Store,
              protected notificationService: NotificationService,
              protected actions$: Actions) {
    super(apiService, store, notificationService, actions$, {
      nameSpace: adminFundingAgenciesActionNameSpace,
      urlResource: AdminResourceApiEnum.fundingAgencies,
      routeRedirectUrlAfterSuccessAction: RoutesEnum.admin,
      notificationResourceCreateTextToTranslate: TRANSLATE("admin.funding-agencies.notification.resource.create"),
      notificationResourceDeleteTextToTranslate: TRANSLATE("admin.funding-agencies.notification.resource.delete"),
      notificationResourceUpdateTextToTranslate: TRANSLATE("admin.funding-agencies.notification.resource.update"),
    });
  }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

  @Action(AdminFundingAgenciesAction.LoadResource)
  loadResource(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.LoadResource): void {
    ctx.patchState({
      isLoadingCounter: ctx.getState().isLoadingCounter + 1,
    });

    ctx.dispatch([
      new SharedOrgUnitAction.GetAll(),
      new AdminFundingAgenciesAction.LoadResourceSuccess(action),
    ]);
  }

  @Action(AdminFundingAgenciesAction.LoadResourceSuccess)
  loadResourceSuccess(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.LoadResourceSuccess): void {
    ctx.patchState({
      isLoadingCounter: ctx.getState().isLoadingCounter - 1,
    });
  }

  @Action(AdminFundingAgenciesAction.LoadResourceFail)
  loadResourceFail(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.LoadResourceFail): void {
    ctx.patchState({
      isLoadingCounter: ctx.getState().isLoadingCounter - 1,
    });
  }

  @OverrideDefaultAction()
  @Action(AdminFundingAgenciesAction.Create)
  create(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.Create): Observable<FundingAgencyExtended> {
    return super.internalCreate(ctx, action)
      .pipe(
        tap(model => {
          const oldOrgUnit = [];
          const newOrgUnit = action.modelFormControlEvent.model[LocalModelAttributeEnum.organizationalUnits];
          const updateSubResourceAction = AssociationActionHelper.update(adminFundingAgenciesOrgUnitActionNameSpace, model.resId, oldOrgUnit, newOrgUnit);
          StoreUtil.updateSubResource(ChangeModeEnum.create, this.actions$, ctx, updateSubResourceAction, adminFundingAgenciesActionNameSpace, adminFundingAgenciesOrgUnitActionNameSpace);
        }),
      );
  }

  @OverrideDefaultAction()
  @Action(AdminFundingAgenciesAction.Update)
  update(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.Update): Observable<FundingAgencyExtended> {
    return super.internalUpdate(ctx, action)
      .pipe(
        tap(model => {
          const oldOrgUnit = _.map(ctx.getState().admin_fundingAgencies_organizationalUnit.selected, LocalModelAttributeEnum.resId);
          const newOrgUnit = action.modelFormControlEvent.model[LocalModelAttributeEnum.organizationalUnits];
          const updateSubResourceAction = AssociationActionHelper.update(adminFundingAgenciesOrgUnitActionNameSpace, model.resId, oldOrgUnit, newOrgUnit);
          StoreUtil.updateSubResource(ChangeModeEnum.update, this.actions$, ctx, updateSubResourceAction, adminFundingAgenciesActionNameSpace, adminFundingAgenciesOrgUnitActionNameSpace);
        }),
      );
  }

  @OverrideDefaultAction()
  @Action(AdminFundingAgenciesAction.Delete)
  delete(ctx: StateContext<AdminFundingAgenciesStateModel>, action: AdminFundingAgenciesAction.Delete): Observable<FundingAgencyExtended> | any {
    const oldOrgUnit = _.map(ctx.getState().admin_fundingAgencies_organizationalUnit.selected, LocalModelAttributeEnum.resId);

    StoreUtil.dispatchActionAndWaitForSubActionsCompletion(
      ctx,
      [
        {
          action: new adminFundingAgenciesOrgUnitActionNameSpace.DeleteList(action.resId, oldOrgUnit),
          subActionCompletions: [
            this.actions$.pipe(ofActionCompleted(adminFundingAgenciesOrgUnitActionNameSpace.DeleteListSuccess)),
            this.actions$.pipe(ofActionCompleted(adminFundingAgenciesOrgUnitActionNameSpace.DeleteListFail)),
          ],
        },
      ],
    ).subscribe(deleteSubResourceWithSuccess => {
      if (deleteSubResourceWithSuccess) {
        super.delete(ctx, action).subscribe();
      } else {
        super.deleteFail(ctx, action);
      }
    });
  }
139
}