@verisoft/store 18.0.1 → 18.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@verisoft/store",
3
- "version": "18.0.1",
3
+ "version": "18.3.0",
4
4
  "peerDependencies": {
5
5
  "@ngrx/store": "^18.0.2",
6
- "@verisoft/core": "18.0.0"
6
+ "@verisoft/core": "18.3.0",
7
+ "@ngrx/effects": "18.0.2",
8
+ "rxjs": "~7.8.0",
9
+ "@angular/router": "18.2.8"
7
10
  },
8
11
  "dependencies": {},
9
12
  "sideEffects": false
package/src/index.ts CHANGED
@@ -7,3 +7,6 @@ export * from './lib/table-state/actions';
7
7
  export * from './lib/table-state/reducers';
8
8
  export * from './lib/table-state/effects';
9
9
  export * from './lib/table-state/models';
10
+
11
+ export * from './lib/binding-state/binding.actions';
12
+ export * from './lib/binding-state/binding.effects';
@@ -0,0 +1,76 @@
1
+ import { createAction, props } from '@ngrx/store';
2
+
3
+ export function createAddBindingAction<TCreate>(bindingsRepository: string) {
4
+ return createAction(
5
+ `[${bindingsRepository}/API] Add bindings`,
6
+ props<{ binding: TCreate }>()
7
+ );
8
+ }
9
+
10
+ export function createAddBindingsAction<TCreate>(bindingsRepository: string) {
11
+ return createAction(
12
+ `[${bindingsRepository}/API] Add bindings`,
13
+ props<{ bindings: TCreate[] }>()
14
+ );
15
+ }
16
+
17
+ export function createAddBindingsFailureAction(bindingsRepository: string) {
18
+ return createAction(
19
+ `[${bindingsRepository}/API] Add bindings failure`,
20
+ props<{ error: any }>()
21
+ );
22
+ }
23
+
24
+ export function createAddBindingsSuccessAction(bindingsRepository: string) {
25
+ return createAction(
26
+ `[${bindingsRepository}/API] Add bindings success`
27
+ );
28
+ }
29
+
30
+ export function createEditBindingAction<TEdit>(bindingsRepository: string) {
31
+ return createAction(
32
+ `[${bindingsRepository}/API] Edit binding`,
33
+ props<{ binding: TEdit }>()
34
+ );
35
+ }
36
+
37
+ export function createEditBindingFailureAction(bindingsRepository: string) {
38
+ return createAction(
39
+ `[${bindingsRepository}/API] Edit binding failure`,
40
+ props<{ error: any }>()
41
+ );
42
+ }
43
+
44
+ export function createEditBindingSuccessAction(bindingsRepository: string) {
45
+ return createAction(
46
+ `[${bindingsRepository}/API] Edit binding success`
47
+ );
48
+ }
49
+
50
+ export function createDeleteBindingAction(bindingsRepository: string) {
51
+ return createAction(
52
+ `[${bindingsRepository}/API] Delete binding`,
53
+ props<{ id: number | string }>()
54
+ );
55
+ }
56
+
57
+ export function createDeleteBindingsAction(bindingsRepository: string) {
58
+ return createAction(
59
+ `[${bindingsRepository}/API] Delete bindings`,
60
+ props<{ bindingIds: number[] }>()
61
+ );
62
+ }
63
+
64
+ export function createDeleteBindingsFailureAction(bindingsRepository: string) {
65
+ return createAction(
66
+ `[${bindingsRepository}/API] Delete bindings failure`,
67
+ props<{ error: any }>()
68
+ );
69
+ }
70
+
71
+ export function createDeleteBindingsSuccessAction(bindingsRepository: string) {
72
+ return createAction(
73
+ `[${bindingsRepository}/API] Delete bindings success`
74
+ );
75
+ }
76
+
@@ -0,0 +1,472 @@
1
+ import { Actions, createEffect, ofType } from '@ngrx/effects';
2
+ import {
3
+ catchError,
4
+ map,
5
+ Observable,
6
+ of,
7
+ switchMap,
8
+ tap,
9
+ withLatestFrom,
10
+ } from 'rxjs';
11
+ import {
12
+ createAddBindingAction,
13
+ createAddBindingsAction,
14
+ createAddBindingsFailureAction,
15
+ createAddBindingsSuccessAction,
16
+ createDeleteBindingAction,
17
+ createDeleteBindingsAction,
18
+ createDeleteBindingsFailureAction,
19
+ createDeleteBindingsSuccessAction,
20
+ createEditBindingAction,
21
+ createEditBindingFailureAction,
22
+ createEditBindingSuccessAction,
23
+ } from './binding.actions';
24
+
25
+ export function createSingleBindingEffectsWithEdit<
26
+ T extends { id: number | string },
27
+ TCreate,
28
+ TEdit extends { id?: number | string }
29
+ >(
30
+ bindingsRepository: string,
31
+ actions$: Actions,
32
+ detail$: Observable<T | undefined>,
33
+ bindingSingularName: string,
34
+ bindingPluralName: string,
35
+ addFunction: (
36
+ bindings: TCreate,
37
+ ) => Observable<any>,
38
+ editFunction: (binding: TEdit, detailId: number | string) => Observable<any>,
39
+ deleteFunction: (
40
+ bindingIds: string | number,
41
+ detailId: number | string
42
+ ) => Observable<any>,
43
+ snackbar: any,
44
+ translateService: any,
45
+ tableService: any,
46
+ overrideDetailWithBinding = false,
47
+ ) {
48
+ const addEffect = createAddBindingEffect(
49
+ bindingsRepository,
50
+ actions$,
51
+ detail$,
52
+ bindingPluralName,
53
+ addFunction,
54
+ snackbar,
55
+ translateService,
56
+ );
57
+
58
+ const editEffect = createEditBindingEffect(
59
+ bindingsRepository,
60
+ actions$,
61
+ detail$,
62
+ bindingSingularName,
63
+ editFunction,
64
+ snackbar,
65
+ translateService,
66
+ overrideDetailWithBinding
67
+ );
68
+
69
+ const deleteEffect = createDeleteBindingEffect(
70
+ bindingsRepository,
71
+ actions$,
72
+ detail$,
73
+ bindingPluralName,
74
+ deleteFunction,
75
+ snackbar,
76
+ translateService
77
+ );
78
+
79
+ const modifiedSuccessEffect = createBindingModifiedSuccessEffect(
80
+ bindingsRepository,
81
+ actions$,
82
+ tableService
83
+ );
84
+
85
+ return { addEffect, editEffect, deleteEffect, modifiedSuccessEffect };
86
+ }
87
+
88
+ export function createBindingsEffectsWithEdit<
89
+ T extends { id: number | string },
90
+ TCreate,
91
+ TEdit extends { id?: number | string }
92
+ >(
93
+ bindingsRepository: string,
94
+ actions$: Actions,
95
+ detail$: Observable<T | undefined>,
96
+ bindingSingularName: string,
97
+ bindingPluralName: string,
98
+ addFunction: (
99
+ bindings: TCreate[],
100
+ detailId: number | string
101
+ ) => Observable<any>,
102
+ editFunction: (binding: TEdit, detailId: number | string) => Observable<any>,
103
+ deleteFunction: (
104
+ bindingIds: number[],
105
+ detailId: number | string
106
+ ) => Observable<any>,
107
+ snackbar: any,
108
+ translateService: any,
109
+ tableService: any,
110
+ overrideDetailWithBinding = false,
111
+ ) {
112
+ const addEffect = createAddBindingsEffect(
113
+ bindingsRepository,
114
+ actions$,
115
+ detail$,
116
+ bindingPluralName,
117
+ addFunction,
118
+ snackbar,
119
+ translateService
120
+ );
121
+
122
+ const editEffect = createEditBindingEffect(
123
+ bindingsRepository,
124
+ actions$,
125
+ detail$,
126
+ bindingSingularName,
127
+ editFunction,
128
+ snackbar,
129
+ translateService,
130
+ overrideDetailWithBinding
131
+ );
132
+
133
+ const deleteEffect = createDeleteBindingsEffect(
134
+ bindingsRepository,
135
+ actions$,
136
+ detail$,
137
+ bindingPluralName,
138
+ deleteFunction,
139
+ snackbar,
140
+ translateService
141
+ );
142
+
143
+ const modifiedSuccessEffect = createBindingModifiedSuccessEffect(
144
+ bindingsRepository,
145
+ actions$,
146
+ tableService
147
+ );
148
+
149
+ return { addEffect, editEffect, deleteEffect, modifiedSuccessEffect };
150
+ }
151
+
152
+ export function createBindingsEffects<
153
+ T extends { id: number | string },
154
+ TCreate
155
+ >(
156
+ bindingsRepository: string,
157
+ actions$: Actions,
158
+ detail$: Observable<T | undefined>,
159
+ bindingPluralName: string,
160
+ addFunction: (
161
+ bindings: TCreate[],
162
+ detailId: number | string
163
+ ) => Observable<any>,
164
+ deleteFunction: (
165
+ bindingIds: number[],
166
+ detailId: number | string
167
+ ) => Observable<any>,
168
+ snackbar: any,
169
+ translateService: any,
170
+ tableService: any
171
+ ) {
172
+ const addEffect = createAddBindingsEffect(
173
+ bindingsRepository,
174
+ actions$,
175
+ detail$,
176
+ bindingPluralName,
177
+ addFunction,
178
+ snackbar,
179
+ translateService
180
+ );
181
+
182
+ const deleteEffect = createDeleteBindingsEffect(
183
+ bindingsRepository,
184
+ actions$,
185
+ detail$,
186
+ bindingPluralName,
187
+ deleteFunction,
188
+ snackbar,
189
+ translateService
190
+ );
191
+
192
+ const modifiedSuccessEffect = createBindingModifiedSuccessEffect(
193
+ bindingsRepository,
194
+ actions$,
195
+ tableService
196
+ );
197
+
198
+ return { addEffect, deleteEffect, modifiedSuccessEffect };
199
+ }
200
+
201
+ function createAddBindingEffect<T extends { id: number | string }, TCreate>(
202
+ bindingsRepository: string,
203
+ actions$: Actions,
204
+ detail$: Observable<T | undefined>,
205
+ bindingPluralName: string,
206
+ addFunction: (
207
+ bindings: TCreate,
208
+ ) => Observable<any>,
209
+ snackbar: any,
210
+ translateService: any,
211
+ ) {
212
+ return createEffect(() =>
213
+ actions$.pipe(
214
+ ofType(createAddBindingAction<TCreate>(bindingsRepository)),
215
+ withLatestFrom(detail$),
216
+ switchMap(([{ binding }, detail]) => {
217
+ const translatedPluralName =
218
+ translateService.instant(bindingPluralName);
219
+ const successMessage = `${translateService.instant(
220
+ 'BINDINGS_EFFECTS.SUCCESS_ADD'
221
+ )} ${translatedPluralName}!`;
222
+ const failureMessage = `${translateService.instant(
223
+ 'BINDINGS_EFFECTS.FAILURE_ADD'
224
+ )} ${translatedPluralName}!`;
225
+
226
+ return addFunction(binding).pipe(
227
+ map(() => {
228
+ snackbar.showSuccess(successMessage);
229
+ return createAddBindingsSuccessAction(bindingsRepository)();
230
+ }),
231
+ catchError((error) => {
232
+ snackbar.showError(failureMessage);
233
+ return of(
234
+ createAddBindingsFailureAction(bindingsRepository)({ error })
235
+ );
236
+ })
237
+ );
238
+ })
239
+ )
240
+ );
241
+ }
242
+
243
+ function createAddBindingsEffect<T extends { id: number | string }, TCreate>(
244
+ bindingsRepository: string,
245
+ actions$: Actions,
246
+ detail$: Observable<T | undefined>,
247
+ bindingPluralName: string,
248
+ addFunction: (
249
+ bindings: TCreate[],
250
+ detailId: number | string
251
+ ) => Observable<any>,
252
+ snackbar: any,
253
+ translateService: any
254
+ ) {
255
+ return createEffect(() =>
256
+ actions$.pipe(
257
+ ofType(createAddBindingsAction<TCreate>(bindingsRepository)),
258
+ withLatestFrom(detail$),
259
+ switchMap(([{ bindings }, detail]) => {
260
+ const translatedPluralName =
261
+ translateService.instant(bindingPluralName);
262
+ const successMessage = `${translateService.instant(
263
+ 'BINDINGS_EFFECTS.SUCCESS_ADD'
264
+ )} ${translatedPluralName}!`;
265
+ const failureMessage = `${translateService.instant(
266
+ 'BINDINGS_EFFECTS.FAILURE_ADD'
267
+ )} ${translatedPluralName}!`;
268
+
269
+ if (!detail?.id) {
270
+ snackbar.showError(failureMessage);
271
+ return of(
272
+ createAddBindingsFailureAction(bindingsRepository)({
273
+ error: failureMessage,
274
+ })
275
+ );
276
+ }
277
+
278
+ return addFunction(bindings, detail.id).pipe(
279
+ map(() => {
280
+ snackbar.showSuccess(successMessage);
281
+ return createAddBindingsSuccessAction(bindingsRepository)();
282
+ }),
283
+ catchError((error) => {
284
+ snackbar.showError(failureMessage);
285
+ return of(
286
+ createAddBindingsFailureAction(bindingsRepository)({ error })
287
+ );
288
+ })
289
+ );
290
+ })
291
+ )
292
+ );
293
+ }
294
+
295
+ function createEditBindingEffect<
296
+ T extends { id: number | string },
297
+ TEdit extends { id?: number | string }
298
+ >(
299
+ bindingsRepository: string,
300
+ actions$: Actions,
301
+ detail$: Observable<T | undefined>,
302
+ bindingSingularName: string,
303
+ editFunction: (binding: TEdit, detailId: number | string) => Observable<any>,
304
+ snackbar: any,
305
+ translateService: any,
306
+ overrideDetailWithBinding = false,
307
+ ) {
308
+ return createEffect(() =>
309
+ actions$.pipe(
310
+ ofType(createEditBindingAction<TEdit>(bindingsRepository)),
311
+ withLatestFrom(detail$),
312
+ switchMap(([{ binding }, detail]) => {
313
+ const translatedPluralName =
314
+ translateService.instant(bindingSingularName);
315
+ const successMessage = `${translateService.instant(
316
+ 'BINDINGS_EFFECTS.SUCCESS_EDIT'
317
+ )} ${translatedPluralName}!`;
318
+ const failureMessage = `${translateService.instant(
319
+ 'BINDINGS_EFFECTS.FAILURE_EDIT'
320
+ )} ${translatedPluralName}!`;
321
+
322
+ const id = overrideDetailWithBinding ? binding?.id : detail?.id;
323
+ if (!id) {
324
+ snackbar.showError(failureMessage);
325
+ return of(
326
+ createEditBindingFailureAction(bindingsRepository)({
327
+ error: failureMessage,
328
+ })
329
+ );
330
+ }
331
+
332
+ return editFunction(binding, id).pipe(
333
+ map(() => {
334
+ snackbar.showSuccess(successMessage);
335
+ return createEditBindingSuccessAction(bindingsRepository)();
336
+ }),
337
+ catchError((error) => {
338
+ snackbar.showError(failureMessage);
339
+ return of(
340
+ createEditBindingFailureAction(bindingsRepository)({ error })
341
+ );
342
+ })
343
+ );
344
+ })
345
+ )
346
+ );
347
+ }
348
+
349
+ function createDeleteBindingEffect<T extends { id: number | string }>(
350
+ bindingsRepository: string,
351
+ actions$: Actions,
352
+ detail$: Observable<T | undefined>,
353
+ bindingPluralName: string,
354
+ deleteFunction: (
355
+ bindingIds: string | number,
356
+ detailId: string | number
357
+ ) => Observable<any>,
358
+ snackbar: any,
359
+ translateService: any
360
+ ) {
361
+ return createEffect(() =>
362
+ actions$.pipe(
363
+ ofType(createDeleteBindingAction(bindingsRepository)),
364
+ withLatestFrom(detail$),
365
+ switchMap(([{ id }, detail]) => {
366
+ const translatedPluralName =
367
+ translateService.instant(bindingPluralName);
368
+ const successMessage = `${translateService.instant(
369
+ 'BINDINGS_EFFECTS.SUCCESS_DELETE'
370
+ )} ${translatedPluralName}!`;
371
+ const failureMessage = `${translateService.instant(
372
+ 'BINDINGS_EFFECTS.FAILURE_DELETE'
373
+ )} ${translatedPluralName}!`;
374
+
375
+ if (!id && !detail?.id) {
376
+ snackbar.showError(failureMessage);
377
+ return of(
378
+ createDeleteBindingsFailureAction(bindingsRepository)({
379
+ error: failureMessage,
380
+ })
381
+ );
382
+ }
383
+
384
+ return deleteFunction(id, id ?? detail?.id).pipe(
385
+ map(() => {
386
+ snackbar.showSuccess(successMessage);
387
+ return createDeleteBindingsSuccessAction(bindingsRepository)();
388
+ }),
389
+ catchError((error) => {
390
+ snackbar.showError(failureMessage);
391
+ return of(
392
+ createDeleteBindingsFailureAction(bindingsRepository)({ error })
393
+ );
394
+ })
395
+ );
396
+ })
397
+ )
398
+ );
399
+ }
400
+
401
+ function createDeleteBindingsEffect<T extends { id: number | string }>(
402
+ bindingsRepository: string,
403
+ actions$: Actions,
404
+ detail$: Observable<T | undefined>,
405
+ bindingPluralName: string,
406
+ deleteFunction: (
407
+ bindingIds: number[],
408
+ detailId: string | number
409
+ ) => Observable<any>,
410
+ snackbar: any,
411
+ translateService: any
412
+ ) {
413
+ return createEffect(() =>
414
+ actions$.pipe(
415
+ ofType(createDeleteBindingsAction(bindingsRepository)),
416
+ withLatestFrom(detail$),
417
+ switchMap(([{ bindingIds }, detail]) => {
418
+ const translatedPluralName =
419
+ translateService.instant(bindingPluralName);
420
+ const successMessage = `${translateService.instant(
421
+ 'BINDINGS_EFFECTS.SUCCESS_DELETE'
422
+ )} ${translatedPluralName}!`;
423
+ const failureMessage = `${translateService.instant(
424
+ 'BINDINGS_EFFECTS.FAILURE_DELETE'
425
+ )} ${translatedPluralName}!`;
426
+
427
+ if (!detail?.id) {
428
+ snackbar.showError(failureMessage);
429
+ return of(
430
+ createDeleteBindingsFailureAction(bindingsRepository)({
431
+ error: failureMessage,
432
+ })
433
+ );
434
+ }
435
+
436
+ return deleteFunction(bindingIds, detail.id).pipe(
437
+ map(() => {
438
+ snackbar.showSuccess(successMessage);
439
+ return createDeleteBindingsSuccessAction(bindingsRepository)();
440
+ }),
441
+ catchError((error) => {
442
+ snackbar.showError(failureMessage);
443
+ return of(
444
+ createDeleteBindingsFailureAction(bindingsRepository)({ error })
445
+ );
446
+ })
447
+ );
448
+ })
449
+ )
450
+ );
451
+ }
452
+
453
+ function createBindingModifiedSuccessEffect(
454
+ bindingsRepository: string,
455
+ actions$: Actions,
456
+ tableService: any
457
+ ) {
458
+ return createEffect(
459
+ () =>
460
+ actions$.pipe(
461
+ ofType(
462
+ createAddBindingsSuccessAction(bindingsRepository),
463
+ createEditBindingSuccessAction(bindingsRepository),
464
+ createDeleteBindingsSuccessAction(bindingsRepository)
465
+ ),
466
+ tap(() => {
467
+ tableService.forceReload();
468
+ })
469
+ ),
470
+ { dispatch: false }
471
+ );
472
+ }
@@ -8,6 +8,10 @@ export function createInitDetailAction(detailsRepository: string) {
8
8
  );
9
9
  }
10
10
 
11
+ export function createInitNewDetailAction(detailsRepository: string) {
12
+ return createAction(`[${detailsRepository} Page] Init New`);
13
+ }
14
+
11
15
  export function createLoadDetailSuccessAction<T>(detailsRepository: string) {
12
16
  return createAction(
13
17
  `[${detailsRepository}/API] Load ${detailsRepository} Success`,
@@ -29,6 +33,13 @@ export function createUpdateDetailAction<T>(detailsRepository: string) {
29
33
  );
30
34
  }
31
35
 
36
+ export function createUpdateDetailSetErrorsAction(detailsRepository: string) {
37
+ return createAction(
38
+ `[${detailsRepository}/API] Update Validation Errors ${detailsRepository}`,
39
+ props<{ error: any }>()
40
+ )
41
+ }
42
+
32
43
  export function createUpdateFormStateAction(detailsRepository: string) {
33
44
  return createAction(
34
45
  `[${detailsRepository}/API] Update Form State`,
@@ -61,3 +72,18 @@ export function createUpdateSaveDetailAction(detailsRepository: string) {
61
72
  export function createResetStateAction(detailsRepository: string) {
62
73
  return createAction(`[${detailsRepository}/API] Reset State`);
63
74
  }
75
+
76
+ export function createDeleteDetailAction(detailsRepository: string) {
77
+ return createAction(`[${detailsRepository}/API] Delete Detail`);
78
+ }
79
+
80
+ export function createDeleteDetailSuccessAction(detailsRepository: string) {
81
+ return createAction(`[${detailsRepository}/API] Delete Detail Success`);
82
+ }
83
+
84
+ export function createDeleteDetailFailureAction(detailsRepository: string) {
85
+ return createAction(
86
+ `[${detailsRepository}/API] Delete Detail Failure`,
87
+ props<{ error: any }>()
88
+ );
89
+ }
@@ -1,3 +1,4 @@
1
+ import { Router } from '@angular/router';
1
2
  import { Actions, createEffect, ofType } from '@ngrx/effects';
2
3
  import { BaseHttpService } from '@verisoft/core';
3
4
  import {
@@ -11,7 +12,11 @@ import {
11
12
  withLatestFrom,
12
13
  } from 'rxjs';
13
14
  import {
15
+ createDeleteDetailAction,
16
+ createDeleteDetailFailureAction,
17
+ createDeleteDetailSuccessAction,
14
18
  createInitDetailAction,
19
+ createInitNewDetailAction,
15
20
  createLoadDetailFailureAction,
16
21
  createLoadDetailSuccessAction,
17
22
  createSaveDetailAction,
@@ -24,9 +29,19 @@ export interface CreateInitFormConfig<T> {
24
29
  get?: (obj: any) => Observable<T>;
25
30
  snackbar?: any;
26
31
  errorMessage?: string;
32
+ notFoundMessage?: string;
27
33
  successMessge?: string;
28
34
  }
29
35
 
36
+ export interface DetailEffectConfig<T> {
37
+ detail$: Observable<T | undefined>;
38
+ snackbar?: any;
39
+ router?: Router;
40
+ redirectPath?: string;
41
+ useItemId?: boolean;
42
+ navigationMap?: Map<keyof T, string>;
43
+ }
44
+
30
45
  export function createInitDetailEffect<T>(
31
46
  detailsRepository: string,
32
47
  actions$: Actions,
@@ -38,14 +53,19 @@ export function createInitDetailEffect<T>(
38
53
  switchMap(({ obj }) => {
39
54
  const get$: Observable<any> = (config?.service?.get(obj as any) ??
40
55
  config?.get?.(obj)) as Observable<any>;
41
- if(!get$) return EMPTY;
56
+ if (!get$) return EMPTY;
42
57
  return get$?.pipe(
43
58
  map((item) => {
44
59
  return createLoadDetailSuccessAction(detailsRepository)({ item });
45
60
  }),
46
61
  catchError((error) => {
62
+ if (error.status === 404) {
63
+ config?.snackbar?.showError(config?.notFoundMessage ?? error);
64
+ } else {
65
+ config?.snackbar?.showError(config?.errorMessage ?? error);
66
+ }
67
+
47
68
  console.error('Error', error);
48
- config?.snackbar?.showError(config?.errorMessage ?? error);
49
69
  return of(
50
70
  createLoadDetailFailureAction(detailsRepository)({ error })
51
71
  );
@@ -56,53 +76,98 @@ export function createInitDetailEffect<T>(
56
76
  });
57
77
  }
58
78
 
59
- export function createSaveDetailEffect<
60
- T extends { id: number },
61
- SERVICE extends BaseHttpService<T>
62
- >(
63
- detailsRepository: string,
64
- actions$: Actions,
65
- service: SERVICE,
66
- detail$: Observable<T | undefined>,
67
- ) {
68
- return createEffect(() =>
79
+ export function createInitNewDetailEffect(
80
+ detailsRepository: string,
81
+ actions$: Actions
82
+ ) {
83
+ return createEffect(() =>
84
+ actions$.pipe(
85
+ ofType(createInitNewDetailAction(detailsRepository)),
86
+ map(() => {
87
+ return createLoadDetailSuccessAction(detailsRepository)({ item: null });
88
+ })
89
+ )
90
+ );
91
+ }
92
+
93
+ export function createSaveDetailEffect<
94
+ T extends { id: number | string },
95
+ SERVICE extends BaseHttpService<T>
96
+ >(
97
+ detailsRepository: string,
98
+ actions$: Actions,
99
+ service: SERVICE,
100
+ config: DetailEffectConfig<T>
101
+ ) {
102
+ return createEffect(() =>
103
+ actions$.pipe(
104
+ ofType(createSaveDetailAction(detailsRepository)),
105
+ withLatestFrom(config.detail$),
106
+ switchMap(([, savedItem]) => {
107
+ const isNew = !savedItem?.id;
108
+ const saveAction = isNew
109
+ ? service.post(savedItem as T)
110
+ : service.put(savedItem.id, savedItem);
111
+ return saveAction.pipe(
112
+ map((item) => {
113
+ if (isNew && config?.router && config?.redirectPath) {
114
+ const suffix = config?.useItemId ? '/' + item.id : '';
115
+ config.router.navigateByUrl(config.redirectPath + suffix);
116
+ }
117
+
118
+ if (isNew && config?.router && config?.navigationMap) {
119
+ const url = constructUrlFromNavigationMap(item, config.navigationMap)
120
+ config.router.navigateByUrl(url);
121
+ }
122
+
123
+ return createSaveDetailSuccessAction(detailsRepository)({
124
+ item: item,
125
+ });
126
+ }),
127
+ catchError((error) => {
128
+ console.error('Error', error);
129
+ return of(
130
+ createSaveDetailFailureAction(detailsRepository)({ error })
131
+ );
132
+ })
133
+ );
134
+ })
135
+ )
136
+ );
137
+ }
138
+
139
+ export function createSaveDetailSuccessEffect(
140
+ detailsRepository: string,
141
+ actions$: Actions,
142
+ config?: CreateInitFormConfig<unknown>
143
+ ) {
144
+ return createEffect(
145
+ () =>
69
146
  actions$.pipe(
70
- ofType(createSaveDetailAction(detailsRepository)),
71
- withLatestFrom(detail$),
72
- switchMap(([, savedItem]) => {
73
- const isNew = !savedItem?.id;
74
- const saveAction = isNew
75
- ? service.post(savedItem as T)
76
- : service.put(savedItem.id, savedItem);
77
- return saveAction.pipe(
78
- map((item) => {
79
- return createSaveDetailSuccessAction(detailsRepository)({
80
- item: item,
81
- });
82
- }),
83
- catchError((error) => {
84
- console.error('Error', error);
85
- return of(
86
- createSaveDetailFailureAction(detailsRepository)({ error })
87
- );
88
- })
147
+ ofType(createSaveDetailSuccessAction(detailsRepository)),
148
+ map((data: any) => {
149
+ config?.snackbar?.showSuccess(
150
+ config?.successMessge ?? `Item successfully saved.`
89
151
  );
152
+ return createInitDetailAction(detailsRepository)({ obj: data.item.id });
90
153
  })
91
154
  )
92
- );
93
- }
155
+ );
156
+ }
94
157
 
95
- export function createSaveDetailSuccessEffect(
158
+ export function createSaveDetailFailureEffect(
96
159
  detailsRepository: string,
97
160
  actions$: Actions,
98
- config?: CreateInitFormConfig<unknown>,
161
+ config?: CreateInitFormConfig<unknown>
99
162
  ) {
100
163
  return createEffect(
101
164
  () =>
102
165
  actions$.pipe(
103
- ofType(createSaveDetailSuccessAction(detailsRepository)),
104
- tap(() => {
105
- config?.snackbar?.showSuccess(config?.successMessge ?? `Item successfully saved.`);
166
+ ofType(createSaveDetailFailureAction(detailsRepository)),
167
+ tap(({ error }) => {
168
+ config?.snackbar?.showError(
169
+ config.errorMessage ?? `Error saving item. ${error.message}`
170
+ );
106
171
  })
107
172
  ),
108
173
  {
@@ -111,21 +176,108 @@ export function createSaveDetailSuccessEffect(
111
176
  );
112
177
  }
113
178
 
114
- export function createSaveDetailFailureEffect(
115
- detailsRepository: string,
116
- actions$: Actions,
117
- config?: CreateInitFormConfig<unknown>,
118
- ) {
119
- return createEffect(
120
- () =>
121
- actions$.pipe(
122
- ofType(createSaveDetailFailureAction(detailsRepository)),
123
- tap(({error}) => {
124
- config?.snackbar?.showError(config.errorMessage ?? `Error saving item. ${error.message}`);
179
+ export function createDeleteDetailEffect<
180
+ T extends { id: number | string },
181
+ SERVICE extends BaseHttpService<T>
182
+ >(
183
+ detailsRepository: string,
184
+ actions$: Actions,
185
+ service: SERVICE,
186
+ config: DetailEffectConfig<T>,
187
+ ) {
188
+ return createEffect(() =>
189
+ actions$.pipe(
190
+ ofType(createDeleteDetailAction(detailsRepository)),
191
+ withLatestFrom(config.detail$),
192
+ switchMap(([, savedItem]) => {
193
+ const id = savedItem?.id;
194
+ if (!id) {
195
+ return of(
196
+ createSaveDetailFailureAction(detailsRepository)({
197
+ error: 'Item has no id',
198
+ })
199
+ );
200
+ }
201
+
202
+ return service.delete(id).pipe(
203
+ map(() => {
204
+ if (config?.router && config?.redirectPath) {
205
+ config.router.navigateByUrl(`${config.redirectPath}`);
206
+ }
207
+
208
+ if (config?.router && config?.navigationMap) {
209
+ const url = constructUrlFromNavigationMap(savedItem, config.navigationMap)
210
+ config.router.navigateByUrl(url);
211
+ }
212
+
213
+ return createDeleteDetailSuccessAction(detailsRepository)();
214
+ }),
215
+ catchError((error) => {
216
+ console.error('Error', error);
217
+ config.snackbar?.showError('Failed to delete item.');
218
+ return of(
219
+ createDeleteDetailFailureAction(detailsRepository)({ error })
220
+ );
125
221
  })
126
- ),
127
- {
128
- dispatch: false,
129
- }
130
- );
222
+ );
223
+ })
224
+ )
225
+ );
226
+ }
227
+
228
+ export function createDeleteDetailSuccessEffect(
229
+ detailsRepository: string,
230
+ actions$: Actions,
231
+ config?: CreateInitFormConfig<unknown>
232
+ ) {
233
+ return createEffect(
234
+ () =>
235
+ actions$.pipe(
236
+ ofType(createDeleteDetailSuccessAction(detailsRepository)),
237
+ tap(() => {
238
+ config?.snackbar?.showSuccess(
239
+ config.successMessge ?? `Item successfully deleted.`
240
+ );
241
+ })
242
+ ),
243
+ {
244
+ dispatch: false,
245
+ }
246
+ );
247
+ }
248
+
249
+ export function createDeleteDetailFailureEffect(
250
+ detailsRepository: string,
251
+ actions$: Actions,
252
+ config?: CreateInitFormConfig<unknown>
253
+ ) {
254
+ return createEffect(
255
+ () =>
256
+ actions$.pipe(
257
+ ofType(createDeleteDetailFailureAction(detailsRepository)),
258
+ tap(({ error }) => {
259
+ config?.snackbar?.showError(
260
+ config.errorMessage ?? `Error deleting item. ${error.message}`
261
+ );
262
+ })
263
+ ),
264
+ {
265
+ dispatch: false,
266
+ }
267
+ );
268
+ }
269
+
270
+ function constructUrlFromNavigationMap<T extends object>(
271
+ item: T,
272
+ navigationMap: Map<keyof T, string>)
273
+ : string {
274
+ let url = '';
275
+ navigationMap.forEach((urlPartValue, suffixPropertyKey) => {
276
+ const suffix = item[suffixPropertyKey];
277
+ if (suffix) {
278
+ url += `${urlPartValue}/${suffix}`;
279
+ }
280
+ });
281
+
282
+ return url;
131
283
  }
@@ -3,20 +3,20 @@ export interface DetailState<T> {
3
3
  item?: T;
4
4
  formState?: FormState;
5
5
  error?: string | null;
6
+ saveItemState: SaveItemState;
7
+ backendValidationErrors: BackendValidationError[];
6
8
  }
7
9
 
8
- export interface ChosenRowsState<T> {
9
- rows: Array<T>;
10
+ export interface BackendValidationError {
11
+ code: string;
12
+ message: string;
13
+ parameters: string;
10
14
  }
11
15
 
12
- export const INITIAL_CHOSEN_ROW_STATE: ChosenRowsState<any> = {
13
- rows: []
16
+ export interface ChosenRowsState<T> {
17
+ rows: Array<T>;
14
18
  }
15
19
 
16
- export const INITIAL_DETAIL_STATE: DetailState<any> = {
17
- loaded: false,
18
- };
19
-
20
20
  export interface SaveItemState {
21
21
  saveInProgress: boolean;
22
22
  error?: string | null;
@@ -26,6 +26,16 @@ export const INITIAL_SAVE_ITEM_STATE: SaveItemState = {
26
26
  saveInProgress: false,
27
27
  };
28
28
 
29
+ export const INITIAL_CHOSEN_ROW_STATE: ChosenRowsState<any> = {
30
+ rows: []
31
+ }
32
+
33
+ export const INITIAL_DETAIL_STATE: DetailState<any> = {
34
+ loaded: false,
35
+ saveItemState: INITIAL_SAVE_ITEM_STATE,
36
+ backendValidationErrors: [],
37
+ };
38
+
29
39
  export interface FormState {
30
40
  dirty: boolean;
31
41
  valid: boolean;
@@ -1,6 +1,7 @@
1
1
  import { ActionCreator, createReducer, on, ReducerTypes } from '@ngrx/store';
2
2
  import {
3
3
  createInitDetailAction,
4
+ createInitNewDetailAction,
4
5
  createLoadDetailFailureAction,
5
6
  createLoadDetailSuccessAction,
6
7
  createResetStateAction,
@@ -8,6 +9,7 @@ import {
8
9
  createSaveDetailFailureAction,
9
10
  createSaveDetailSuccessAction,
10
11
  createUpdateDetailAction,
12
+ createUpdateDetailSetErrorsAction,
11
13
  createUpdateFormStateAction,
12
14
  } from './detail.actions';
13
15
  import {
@@ -34,6 +36,14 @@ export function createDetailReducers<
34
36
  };
35
37
  }),
36
38
 
39
+ on(createInitNewDetailAction(detailRepository), (state) => {
40
+ return {
41
+ ...state,
42
+ loaded: false,
43
+ error: null,
44
+ };
45
+ }),
46
+
37
47
  on(createLoadDetailSuccessAction(detailRepository), (state, { item }) => {
38
48
  return {
39
49
  ...state,
@@ -61,6 +71,13 @@ export function createDetailReducers<
61
71
  };
62
72
  }),
63
73
 
74
+ on(createUpdateDetailSetErrorsAction(detailRepository), (state, { error }) => {
75
+ return {
76
+ ...state,
77
+ backendValidationErrors: error
78
+ }
79
+ }),
80
+
64
81
  on(
65
82
  createUpdateFormStateAction(detailRepository),
66
83
  (state, { formState }) => {
@@ -80,11 +97,19 @@ export function createDetailReducers<
80
97
  },
81
98
  };
82
99
  }),
100
+
83
101
  on(createResetStateAction(detailRepository), () => {
84
102
  return {
85
103
  ...(initialState as any),
86
104
  };
87
105
  }),
106
+
107
+ on(createSaveDetailFailureAction(detailRepository), (state, { error }) => {
108
+ return {
109
+ ...state,
110
+ backendValidationErrors: error.error
111
+ }
112
+ }),
88
113
  ...ons
89
114
  );
90
115
  }
@@ -84,13 +84,13 @@ export function createRemoveRangeTableEffect<T>(
84
84
  return actions$.pipe(
85
85
  ofType(createRemoveRangeTableAction(tableRepository)),
86
86
  withLatestFrom(selectedItems),
87
- switchMap(([, {selectedItems}]) => {
87
+ switchMap(([, { selectedItems }]) => {
88
88
  return config.service!.removeRange(selectedItems).pipe(
89
- map(() => {
89
+ map(() => {
90
90
  return createGetPageTableAction(tableRepository)
91
91
  ({ page: 0, size: DEFAULT_SEARCH_LIMIT })
92
92
  }),
93
- catchError(error => {
93
+ catchError(error => {
94
94
  return of(createDataLoadErrorTableAction(tableRepository)({ error }))
95
95
  })
96
96
  );