|
@@ -1,109 +1,129 @@
|
1
|
|
-import { UserActionType, GetAllUsersActionSuccess, GetAllUsersActionError, UserActionUnion, SearchUserActionSuccess, SearchUserActionError, CreareFormUserEditActionSuccess, CreareFormUserEditActionActionError, DeleteUserActionSuccess, DeleteUserActionError, CreateFormUserAddActionSuccess, CreateFormUserAddActionError, SaveUserFormActionSuccess, SaveUserFormActionError, UpdateUserFormAction, UpdateUserFormActionError, UpdateUserFormActionSuccess } from './users.actions';
|
|
1
|
+import {
|
|
2
|
+ UserActionType,
|
|
3
|
+ GetAllUsersActionSuccess,
|
|
4
|
+ GetAllUsersActionError,
|
|
5
|
+ UserActionUnion,
|
|
6
|
+ SearchUserActionSuccess,
|
|
7
|
+ SearchUserActionError,
|
|
8
|
+ CreareFormUserEditActionSuccess,
|
|
9
|
+ CreareFormUserEditActionActionError,
|
|
10
|
+ DeleteUserActionSuccess,
|
|
11
|
+ DeleteUserActionError,
|
|
12
|
+ CreateFormUserAddActionSuccess,
|
|
13
|
+ CreateFormUserAddActionError,
|
|
14
|
+ SaveUserFormActionSuccess,
|
|
15
|
+ SaveUserFormActionError,
|
|
16
|
+ UpdateUserFormAction,
|
|
17
|
+ UpdateUserFormActionError,
|
|
18
|
+ UpdateUserFormActionSuccess,
|
|
19
|
+} from './users.actions';
|
2
|
20
|
import { map, mergeMap, Observable, catchError, of } from 'rxjs';
|
3
|
21
|
import { UsersService } from './../../shared/services/users.service';
|
4
|
|
-import { Injectable } from "@angular/core";
|
|
22
|
+import { Injectable } from '@angular/core';
|
5
|
23
|
import { Actions, createEffect, ofType } from '@ngrx/effects';
|
6
|
24
|
import { User } from 'src/app/shared/models/user.model';
|
7
|
25
|
|
8
|
26
|
@Injectable()
|
9
|
|
-export class UserEffects{
|
|
27
|
+export class UserEffects {
|
|
28
|
+ constructor(
|
|
29
|
+ private usersService: UsersService,
|
|
30
|
+ private effectActions: Actions
|
|
31
|
+ ) {}
|
10
|
32
|
|
11
|
|
- constructor(private usersService: UsersService, private effectActions: Actions){}
|
|
33
|
+ /** creation of effects about all cations related to a user */
|
12
|
34
|
|
13
|
|
- //create getAllUsers effects
|
|
35
|
+ //create getAllUsers effect
|
14
|
36
|
|
15
|
|
- getAllUsersEffect: Observable<UserActionUnion> = createEffect(
|
16
|
|
-
|
17
|
|
- () => this.effectActions.pipe(
|
|
37
|
+ getAllUsersEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
38
|
+ this.effectActions.pipe(
|
18
|
39
|
ofType(UserActionType.GET_ALL_USERS),
|
19
|
|
- mergeMap(
|
20
|
|
- () =>{
|
21
|
|
-
|
22
|
|
- return this.usersService.getAllUsers().pipe(
|
23
|
|
- map((users: User[])=> new GetAllUsersActionSuccess(users)),
|
24
|
|
- catchError((err) => of(new GetAllUsersActionError(err.message)))
|
25
|
|
- );
|
26
|
|
- }
|
27
|
|
- )
|
28
|
|
-
|
29
|
|
- )
|
30
|
|
- );
|
31
|
|
- //create searchUser effects
|
32
|
|
- searchUserEffect: Observable<UserActionUnion> = createEffect(
|
33
|
|
-
|
34
|
|
- () => this.effectActions.pipe(
|
35
|
|
- ofType(UserActionType.SEARCH_A_USER),
|
36
|
|
- mergeMap(
|
37
|
|
- (userAction: UserActionUnion) =>{
|
38
|
|
-
|
39
|
|
- return this.usersService.searchUser(userAction.payload).pipe(
|
40
|
|
- map((users: User[])=> new SearchUserActionSuccess(users)),
|
41
|
|
- catchError((err) => of(new SearchUserActionError(err.message)))
|
42
|
|
- );
|
43
|
|
- }
|
44
|
|
- )
|
45
|
|
-
|
|
40
|
+ mergeMap(() => {
|
|
41
|
+ return this.usersService.getAllUsers().pipe(
|
|
42
|
+ map((users: User[]) => new GetAllUsersActionSuccess(users)),
|
|
43
|
+ catchError((err) => of(new GetAllUsersActionError(err.message)))
|
|
44
|
+ );
|
|
45
|
+ })
|
46
|
46
|
)
|
47
|
47
|
);
|
48
|
48
|
|
|
49
|
+ //create searchUser effect
|
49
|
50
|
|
50
|
|
- //create deleteUser effect
|
51
|
|
- deleteUserEffect: Observable<UserActionUnion> = createEffect(
|
52
|
|
- ()=> this.effectActions.pipe(
|
53
|
|
- ofType(UserActionType.DELETE_A_USER),
|
54
|
|
- mergeMap( (userAction: UserActionUnion) => {
|
55
|
|
- return this.usersService.deleteUser(userAction.payload.id).pipe(
|
56
|
|
- map( ()=> new DeleteUserActionSuccess(userAction.payload)),
|
57
|
|
- catchError( (err) => of(new DeleteUserActionError(err.message)))
|
58
|
|
- )
|
|
51
|
+ searchUserEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
52
|
+ this.effectActions.pipe(
|
|
53
|
+ ofType(UserActionType.SEARCH_A_USER),
|
|
54
|
+ mergeMap((userAction: UserActionUnion) => {
|
|
55
|
+ return this.usersService.searchUser(userAction.payload).pipe(
|
|
56
|
+ map((users: User[]) => new SearchUserActionSuccess(users)),
|
|
57
|
+ catchError((err) => of(new SearchUserActionError(err.message)))
|
|
58
|
+ );
|
59
|
59
|
})
|
60
|
60
|
)
|
61
|
61
|
);
|
62
|
62
|
|
63
|
63
|
//create a user add form effect
|
64
|
|
- createAddUserFormEffect: Observable<UserActionUnion> = createEffect(
|
65
|
|
- () => this.effectActions.pipe(
|
|
64
|
+ createAddUserFormEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
65
|
+ this.effectActions.pipe(
|
66
|
66
|
ofType(UserActionType.CREATE_FORM_ADD_USER),
|
67
|
|
- map( ()=>{
|
|
67
|
+ map(() => {
|
68
|
68
|
return new CreateFormUserAddActionSuccess({});
|
69
|
69
|
}),
|
70
|
|
- catchError( (err) => of(new CreateFormUserAddActionError(err.message)))
|
|
70
|
+ catchError((err) => of(new CreateFormUserAddActionError(err.message)))
|
71
|
71
|
)
|
72
|
72
|
);
|
73
|
73
|
|
74
|
|
- //action of saving user form data effect
|
75
|
|
- saveUserFormEffect : Observable<UserActionUnion> = createEffect(
|
76
|
|
- () => this.effectActions.pipe(
|
|
74
|
+ //saving user form data effect
|
|
75
|
+
|
|
76
|
+ saveUserFormEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
77
|
+ this.effectActions.pipe(
|
77
|
78
|
ofType(UserActionType.SAVE_USERFORM_ADD),
|
78
|
|
- mergeMap( (userAction: UserActionUnion) => {
|
|
79
|
+ mergeMap((userAction: UserActionUnion) => {
|
79
|
80
|
return this.usersService.createUser(userAction.payload).pipe(
|
80
|
|
- map (user => new SaveUserFormActionSuccess(user)),
|
81
|
|
- catchError( err => of(new SaveUserFormActionError(err.message)))
|
|
81
|
+ map((user) => new SaveUserFormActionSuccess(user)),
|
|
82
|
+ catchError((err) => of(new SaveUserFormActionError(err.message)))
|
82
|
83
|
);
|
83
|
84
|
})
|
84
|
85
|
)
|
85
|
86
|
);
|
86
|
87
|
|
87
|
|
- //create update user form with data of user to update effect
|
88
|
|
- createUpdateUserFormEffect: Observable<UserActionUnion> = createEffect(
|
89
|
|
- () => this.effectActions.pipe(
|
|
88
|
+ // create an update user form effect and fill it with data of a user to update
|
|
89
|
+
|
|
90
|
+ createUpdateUserFormEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
91
|
+ this.effectActions.pipe(
|
90
|
92
|
ofType(UserActionType.CREATE_FORM_USER_EDIT),
|
91
|
|
- mergeMap( (userAction: UserActionUnion)=>{
|
|
93
|
+ mergeMap((userAction: UserActionUnion) => {
|
92
|
94
|
return this.usersService.getUserById(userAction.payload.id).pipe(
|
93
|
|
- map( (user: User)=> new CreareFormUserEditActionSuccess(user)),
|
94
|
|
- catchError( (err) => of(new CreareFormUserEditActionActionError(err.message)))
|
95
|
|
- )
|
|
95
|
+ map((user: User) => new CreareFormUserEditActionSuccess(user)),
|
|
96
|
+ catchError((err) =>
|
|
97
|
+ of(new CreareFormUserEditActionActionError(err.message))
|
|
98
|
+ )
|
|
99
|
+ );
|
96
|
100
|
})
|
97
|
101
|
)
|
98
|
102
|
);
|
99
|
103
|
|
100
|
|
- updateUserFormDataEffect: Observable<UserActionUnion> = createEffect(
|
101
|
|
- () => this.effectActions.pipe(
|
|
104
|
+ // update a user with data of form effect
|
|
105
|
+
|
|
106
|
+ updateUserFormDataEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
107
|
+ this.effectActions.pipe(
|
102
|
108
|
ofType(UserActionType.UPDATE_USER_FORM),
|
103
|
|
- mergeMap( (userAction: UserActionUnion) =>{
|
|
109
|
+ mergeMap((userAction: UserActionUnion) => {
|
104
|
110
|
return this.usersService.updateUser(userAction.payload).pipe(
|
105
|
|
- map( (user: User) => new UpdateUserFormActionSuccess(user)),
|
106
|
|
- catchError( err => of(new UpdateUserFormActionError(err.message)))
|
|
111
|
+ map((user: User) => new UpdateUserFormActionSuccess(user)),
|
|
112
|
+ catchError((err) => of(new UpdateUserFormActionError(err.message)))
|
|
113
|
+ );
|
|
114
|
+ })
|
|
115
|
+ )
|
|
116
|
+ );
|
|
117
|
+
|
|
118
|
+ //deleteUser effect
|
|
119
|
+
|
|
120
|
+ deleteUserEffect: Observable<UserActionUnion> = createEffect(() =>
|
|
121
|
+ this.effectActions.pipe(
|
|
122
|
+ ofType(UserActionType.DELETE_A_USER),
|
|
123
|
+ mergeMap((userAction: UserActionUnion) => {
|
|
124
|
+ return this.usersService.deleteUser(userAction.payload.id).pipe(
|
|
125
|
+ map(() => new DeleteUserActionSuccess(userAction.payload)),
|
|
126
|
+ catchError((err) => of(new DeleteUserActionError(err.message)))
|
107
|
127
|
);
|
108
|
128
|
})
|
109
|
129
|
)
|