placidenduwayo пре 2 година
родитељ
комит
0c1b71c711
21 измењених фајлова са 908 додато и 403 уклоњено
  1. 35
    56
      db_users_projects_companies.json
  2. 10
    5
      src/app/ngrx/msg.state.ts
  3. 1
    0
      src/app/ngrx/users/user.state.ts
  4. 74
    48
      src/app/ngrx/users/users.actions.ts
  5. 33
    20
      src/app/ngrx/users/users.effects.ts
  6. 74
    36
      src/app/ngrx/users/users.reducer.ts
  7. 206
    198
      src/app/pages/users-manager/users-management/user-create/user-create.component.html
  8. 27
    14
      src/app/pages/users-manager/users-management/user-create/user-create.component.ts
  9. 1
    1
      src/app/pages/users-manager/users-management/user-nav-bar/user-nav-bar.component.ts
  10. 0
    0
      src/app/pages/users-manager/users-management/user-update/user-update.component.css
  11. 269
    0
      src/app/pages/users-manager/users-management/user-update/user-update.component.html
  12. 25
    0
      src/app/pages/users-manager/users-management/user-update/user-update.component.spec.ts
  13. 110
    0
      src/app/pages/users-manager/users-management/user-update/user-update.component.ts
  14. 3
    3
      src/app/pages/users-manager/users-management/users-list/users-list.component.html
  15. 19
    13
      src/app/pages/users-manager/users-management/users-list/users-list.component.ts
  16. 1
    1
      src/app/pages/users-manager/users-management/users-management.component.html
  17. 4
    3
      src/app/pages/users-manager/users-management/users-management.component.ts
  18. 4
    2
      src/app/pages/users-manager/users-manager-routing.module.ts
  19. 3
    1
      src/app/pages/users-manager/users-manager.module.ts
  20. 4
    0
      src/app/shared/models/company.type.ts
  21. 5
    2
      src/app/shared/services/users.service.ts

+ 35
- 56
db_users_projects_companies.json Прегледај датотеку

@@ -21,43 +21,30 @@
21 21
   ],
22 22
   "users": [
23 23
     {
24
+      "id": 3,
25
+      "firstname": "David",
26
+      "lastname": "Touitou",
27
+      "email": "david.touitou@natan.fr",
24 28
       "address": {
25 29
         "numRue": 44,
26 30
         "nomRue": "Rue Notre Dame de Paris",
27 31
         "bp": 75002,
28 32
         "ville": "Paris",
29 33
         "pays": "France"
30
-      },
31
-      "firstname": "Placide",
32
-      "lastname": "Nduwayo",
33
-      "email": "placide.nduwayo@natan.fr",
34
-      "id": 2
35
-    },
36
-    {
37
-      "address": {
38
-        "numRue": 44,
39
-        "nomRue": "Rue Notre Dame de Paris",
40
-        "bp": 75002,
41
-        "ville": "Paris",
42
-        "pays": "France"
43
-      },
44
-      "firstname": "David",
45
-      "lastname": "Touitou",
46
-      "email": "david.touitou@natan.fr",
47
-      "id": 3
34
+      }
48 35
     },
49 36
     {
37
+      "id": 4,
38
+      "firstname": "Nadhir",
39
+      "lastname": "Ben Mohamed",
40
+      "email": "nadhir.ben-mohamed@natan.fr",
50 41
       "address": {
51 42
         "numRue": 44,
52 43
         "nomRue": "Rue Notre Dame de Paris",
53 44
         "bp": 75002,
54 45
         "ville": "Paris",
55 46
         "pays": "France"
56
-      },
57
-      "firstname": "Nadhir",
58
-      "lastname": "Ben Mohamed",
59
-      "email": "nadhir.ben-mohamed@natan.fr",
60
-      "id": 4
47
+      }
61 48
     },
62 49
     {
63 50
       "address": {
@@ -93,19 +80,6 @@
93 80
         "ville": "Paris",
94 81
         "pays": "France"
95 82
       },
96
-      "firstname": "Naolie",
97
-      "lastname": "Boudouka",
98
-      "email": "naolie.boudouka@natan.fr",
99
-      "id": 7
100
-    },
101
-    {
102
-      "address": {
103
-        "numRue": 44,
104
-        "nomRue": "Rue Notre Dame de Paris",
105
-        "bp": 75002,
106
-        "ville": "Paris",
107
-        "pays": "France"
108
-      },
109 83
       "firstname": "Syrine",
110 84
       "lastname": "Ait Haddad",
111 85
       "email": "syrine.ait-haddadene@natan.fr",
@@ -149,28 +123,33 @@
149 123
         "ville": "Paris",
150 124
         "pays": "France"
151 125
       }
152
-    }
153
-  ],
154
-  "projects": [
126
+    },
155 127
     {
156
-      "id": 2,
157
-      "projectName": "Project-2",
158
-      "priority": 1,
159
-      "creationDate": "05 18 2022",
160
-      "companyId": 3,
161
-      "description": "Creating and training DL models",
162
-      "userId": 2,
163
-      "state": "END"
128
+      "id": 12,
129
+      "firstname": "Placide",
130
+      "lastname": "Nduwayo",
131
+      "email": "placide.nduwayo@natan.fr",
132
+      "address": {
133
+        "numRue": 44,
134
+        "nomRue": "Rue Notre Dame de Paris",
135
+        "bp": 75002,
136
+        "ville": "Paris",
137
+        "pays": "France"
138
+      }
164 139
     },
165 140
     {
166
-      "id": 3,
167
-      "projectName": "Project-3",
168
-      "priority": 1,
169
-      "creationDate": "05 18 2022",
170
-      "companyId": 2,
171
-      "description": "Project description",
172
-      "userId": 2,
173
-      "state": "IN PROGRESS"
141
+      "id": 13,
142
+      "firstname": null,
143
+      "lastname": null,
144
+      "email": null,
145
+      "address": {
146
+        "numRue": null,
147
+        "nomRue": null,
148
+        "bp": null,
149
+        "ville": null,
150
+        "pays": null
151
+      }
174 152
     }
175
-  ]
153
+  ],
154
+  "projects": []
176 155
 }

+ 10
- 5
src/app/ngrx/msg.state.ts Прегледај датотеку

@@ -1,9 +1,14 @@
1 1
 export enum MsgState {
2
-  LOADING = '[User] LOADING ...',
3
-  LOADED = '[User] LOADED',
4
-  ERROR = '[User] ERROR',
2
+  LOADING = '[User] USERS ARE LOADING ...',
3
+  LOADED = '[User] SUCCESSFUL LOADED',
4
+  ERR = 'ERROR ACCURRED',
5 5
   INIT_STATE = '[User] INITIAL STATE',
6
+  FORM_LOADING = 'FORM LOADING',
7
+  FORM_LOADED = 'FORM LOADED',
8
+  FORM_LOAD_ERR= 'FORM LOADING ERROR',
6 9
   CONFIRM = 'CONFIRM',
7
-  NEW ='NEW',
8
-  SUCCESS = 'SUCCESSFUL SAVED'
10
+  ADDED = 'SUCCESSFULLY ADDED',
11
+  EXIST = 'ALREADY EXISTS',
12
+  UPDATED = '[User] SUCCESSFULLY UPDATED',
13
+  DEFAULT_MSG = 'DEFAULT LOADING MSG'
9 14
 }

+ 1
- 0
src/app/ngrx/users/user.state.ts Прегледај датотеку

@@ -3,6 +3,7 @@ import { MsgState } from "../msg.state";
3 3
 
4 4
 //create a state
5 5
 export class UserState {
6
+  currentUser!: User | null;
6 7
   usersList!: User[];
7 8
   msgState!: MsgState;
8 9
   userServerErrorMsg!: string;

+ 74
- 48
src/app/ngrx/users/users.actions.ts Прегледај датотеку

@@ -2,6 +2,7 @@ import { Action } from '@ngrx/store';
2 2
 import { User } from 'src/app/shared/models/user.model';
3 3
 
4 4
 //type of actions
5
+
5 6
 export enum UserActionType {
6 7
   /* action: get all users */
7 8
   GET_ALL_USERS = '[User] GET ALL USERS',
@@ -9,32 +10,36 @@ export enum UserActionType {
9 10
   GET_ALL_USERS_ERROR = '[User] GET-ALL USERS ERROR',
10 11
 
11 12
   /* action: search a user */
12
-
13 13
   SEARCH_A_USER = '[User] SEARCH A USER',
14 14
   SEARCH_A_USER_SUCCESS = '[User] SEARCH A USER SUCCESS',
15 15
   SEARCH_A_USER_ERROR = '[User] SEARCH A USER ERROR',
16 16
 
17
-  /* action: edit a user */
18 17
 
19
-  EDIT_A_USER = '[User] EDIT A USER',
20
-  EDIT_A_USER_SUCCESS = '[User] EDIT A USER SUCCESS',
21
-  EDIT_A_USER_ERROR = '[User] EDIT A USER ERROR',
18
+  /** action of button: create a user form*/
19
+  CREATE_FORM_ADD_USER = '[User] USER FORM ADD',
20
+  CREATE_FORM_ADD_USER_SUCCESS = '[User] USER FORM ADD SUCCESS',
21
+  CREATE_FORM_ADD_USER_ERROR = '[User] USER FORM ADD ERROR',
22 22
 
23
-  /* action of button: create a user form*/
23
+  /** action of saving user data in the form */
24
+  SAVE_USERFORM_ADD = '[User] SAVE USER',
25
+  SAVE_USERFORM_ADD_SUCCESS = '[User] SAVE USER SUCCESS',
26
+  SAVE_USERFORM_ADD_ERROR = '[User] SAVE USER ERROR',
24 27
 
25
-  CREATE_A_USERFORM = '[User] CREATE USER FORM',
26
-  CREATE_A_USERFORM_SUCCESS = '[User] CREATE A USER FORM SUCCESS',
27
-  CREATE_A_USERFORM_ERROR = '[User] CREATE A USER FORM ERROR',
28
+  /* action: create form to edit a user */
29
+  CREATE_FORM_USER_EDIT = '[User] USER FORM EDIT',
30
+  CREATE_FORM_USER_EDIT_SUCCESS = '[User] USER FORM EDIT SUCCESS',
31
+  CREATE_FORM_USER_EDIT_ERROR = '[User] USER FORM EDIT ERROR',
28 32
 
29
-  SAVE_A_USERFORM ='[User] SAVE A USER',
30
-  SAVE_A_USERFORM_SUCCESS ='[User] SAVE A USER SUCCESS',
31
-  SAVE_A_USERFORM_ERROR='[User] SAVE A USER ERROR',
33
+  /** action of updating user from user form */
34
+  UPDATE_USER_FORM = '[User] USER FORM UPDATE',
35
+  UPDATE_USER_FORM_SUCCESS = '[User] USER FORM UPDATE SUCCESS',
36
+  UPDATE_USER_FORM_ERROR = '[User] USER FORM UPDATE ERROR',
32 37
 
33
-   /* action: delete a user */
34 38
 
35
-   DELETE_A_USER = '[User] DELETE A USER',
36
-   DELETE_A_USER_SUCCESS = '[User] DELETE A USER SUCCESS',
37
-   DELETE_A_USER_ERROR = '[User] DELETE A USER ERROR'
39
+  /* action: delete a user */
40
+  DELETE_A_USER = '[User] DELETE A USER',
41
+  DELETE_A_USER_SUCCESS = '[User] DELETE A USER SUCCESS',
42
+  DELETE_A_USER_ERROR = '[User] DELETE A USER ERROR',
38 43
 }
39 44
 
40 45
 //create actions
@@ -73,58 +78,76 @@ export class SearchUserActionError implements Action {
73 78
   constructor(public payload: string) {}
74 79
 }
75 80
 
76
-/* Actios of editing a user */
81
+/* Actios of creating a user form to add user*/
77 82
 
78
-export class EditUsersAction implements Action {
79
-  type: UserActionType = UserActionType.EDIT_A_USER;
80
-
81
-  constructor(public payload: {user: User}) {}
83
+export class CreateFormUserAddAction implements Action {
84
+  type: UserActionType = UserActionType.CREATE_FORM_ADD_USER;
85
+  constructor(public payload: void) {}
82 86
 }
83 87
 
84
-export class EditUserActionSuccess implements Action {
85
-  type: UserActionType = UserActionType.EDIT_A_USER_SUCCESS;
86
-  constructor(public payload: User) {}
88
+export class CreateFormUserAddActionSuccess implements Action {
89
+  type: UserActionType = UserActionType.CREATE_FORM_ADD_USER_SUCCESS;
90
+  constructor(public payload: any) {}
87 91
 }
88 92
 
89
-export class EditUserActionError implements Action {
90
-  type: UserActionType = UserActionType.EDIT_A_USER_ERROR;
93
+export class CreateFormUserAddActionError implements Action {
94
+  type: UserActionType = UserActionType.CREATE_FORM_ADD_USER_ERROR;
91 95
   constructor(public payload: string) {}
92 96
 }
93 97
 
94
-/* Actios of creating a user form */
98
+/**action of saving a user form data*/
99
+export class SaveUserFormAction implements Action{
100
+  type: UserActionType = UserActionType.SAVE_USERFORM_ADD;
101
+  constructor(public payload: User){}
102
+}
95 103
 
96
-export class CreateUserFormAction implements Action {
97
-  type: UserActionType = UserActionType.CREATE_A_USERFORM;
104
+export class SaveUserFormActionSuccess implements Action{
105
+  type: UserActionType = UserActionType.SAVE_USERFORM_ADD_SUCCESS;
106
+  constructor(public payload: User){}
107
+}
98 108
 
99
-  constructor(public payload: any) {}
109
+export class SaveUserFormActionError implements Action{
110
+  type: UserActionType = UserActionType.SAVE_USERFORM_ADD_ERROR;
111
+  constructor(public payload: string){}
100 112
 }
101 113
 
102
-export class CreateUserFormActionSuccess implements Action {
103
-  type: UserActionType = UserActionType.CREATE_A_USERFORM_SUCCESS;
104
-  constructor(public payload: any) {}
114
+/* Actios of creating a user form to edit user*/
115
+
116
+export class CreareFormUserEditAction implements Action {
117
+  type: UserActionType = UserActionType.CREATE_FORM_USER_EDIT;
118
+
119
+  constructor(public payload: User) {}
120
+}
121
+
122
+export class CreareFormUserEditActionSuccess implements Action {
123
+  type: UserActionType = UserActionType.CREATE_FORM_USER_EDIT_SUCCESS;
124
+  constructor(public payload: User) {}
105 125
 }
106 126
 
107
-export class CreateUserFormActionError implements Action {
108
-  type: UserActionType = UserActionType.CREATE_A_USERFORM_ERROR;
127
+export class CreareFormUserEditActionActionError implements Action {
128
+  type: UserActionType = UserActionType.CREATE_FORM_USER_EDIT_ERROR;
109 129
   constructor(public payload: string) {}
110 130
 }
111 131
 
112
-/**action of saving a user */
113
-export class SaveUserFormAction implements Action{
114
-  type: UserActionType = UserActionType.SAVE_A_USERFORM;
132
+/** action of updating user from user data form */
133
+
134
+export class UpdateUserFormAction implements Action{
135
+  type: UserActionType = UserActionType.UPDATE_USER_FORM;
115 136
   constructor(public payload: User){}
116 137
 }
117 138
 
118
-export class SaveUserFormActionSuccess implements Action{
119
-  type: UserActionType = UserActionType.SAVE_A_USERFORM_SUCCESS;
139
+export class UpdateUserFormActionSuccess implements Action{
140
+  type: UserActionType = UserActionType.UPDATE_USER_FORM_SUCCESS;
120 141
   constructor(public payload: User){}
121 142
 }
122 143
 
123
-export class SaveUserFormActionError implements Action{
124
-  type: UserActionType = UserActionType.SAVE_A_USERFORM_ERROR;
144
+export class UpdateUserFormActionError implements Action{
145
+
146
+  type: UserActionType = UserActionType.UPDATE_USER_FORM_ERROR;
125 147
   constructor(public payload: string){}
126 148
 }
127 149
 
150
+
128 151
 /* Actios of deleting a user */
129 152
 
130 153
 export class DeleteUsersAction implements Action {
@@ -150,15 +173,18 @@ export type UserActionUnion =
150 173
   | SearchUsersAction
151 174
   | SearchUserActionSuccess
152 175
   | SearchUserActionError
153
-  | EditUsersAction
154
-  | EditUserActionSuccess
155
-  | EditUserActionError
156
-  | CreateUserFormAction
157
-  | CreateUserFormActionSuccess
158
-  | CreateUserFormActionError
176
+  | CreateFormUserAddAction
177
+  | CreateFormUserAddActionSuccess
178
+  | CreateFormUserAddActionError
159 179
   | SaveUserFormAction
160 180
   | SaveUserFormActionSuccess
161 181
   | SaveUserFormActionError
182
+  | CreareFormUserEditAction
183
+  | CreareFormUserEditActionSuccess
184
+  | CreareFormUserEditActionActionError
185
+  | UpdateUserFormAction
186
+  | UpdateUserFormActionSuccess
187
+  | UpdateUserFormActionError
162 188
   | DeleteUsersAction
163 189
   | DeleteUserActionSuccess
164 190
   | DeleteUserActionError;

+ 33
- 20
src/app/ngrx/users/users.effects.ts Прегледај датотеку

@@ -1,4 +1,4 @@
1
-import { UserActionType, GetAllUsersActionSuccess, GetAllUsersActionError, UserActionUnion, SearchUserActionSuccess, SearchUserActionError, EditUserActionSuccess, EditUserActionError, DeleteUserActionSuccess, DeleteUserActionError, CreateUserFormActionSuccess, CreateUserFormActionError, SaveUserFormActionSuccess, SaveUserFormActionError } from './users.actions';
1
+import { UserActionType, GetAllUsersActionSuccess, GetAllUsersActionError, UserActionUnion, SearchUserActionSuccess, SearchUserActionError, CreareFormUserEditActionSuccess, CreareFormUserEditActionActionError, DeleteUserActionSuccess, DeleteUserActionError, CreateFormUserAddActionSuccess, CreateFormUserAddActionError, SaveUserFormActionSuccess, SaveUserFormActionError, UpdateUserFormAction, UpdateUserFormActionError, UpdateUserFormActionSuccess } from './users.actions';
2 2
 import { map, mergeMap, Observable, catchError, of } from 'rxjs';
3 3
 import { UsersService } from './../../shared/services/users.service';
4 4
 import { Injectable } from "@angular/core";
@@ -46,18 +46,6 @@ export class UserEffects{
46 46
     )
47 47
   );
48 48
 
49
-  //create editUser effect
50
-  editUserEffect: Observable<UserActionUnion> = createEffect(
51
-    () => this.effectActions.pipe(
52
-      ofType(UserActionType.EDIT_A_USER),
53
-      mergeMap( (userAction: UserActionUnion)=>{
54
-        return this.usersService.updateUser(userAction.payload).pipe(
55
-          map( (user: User)=> new EditUserActionSuccess(user)),
56
-          catchError( (err) => of(new EditUserActionError(err.message)))
57
-        )
58
-      })
59
-    )
60
-  );
61 49
 
62 50
   //create deleteUser effect
63 51
   deleteUserEffect: Observable<UserActionUnion> = createEffect(
@@ -72,21 +60,21 @@ export class UserEffects{
72 60
     )
73 61
   );
74 62
 
75
-  //create createUser effect: action of button to create user form
76
-  createUserFormEffect: Observable<UserActionUnion> = createEffect(
63
+  //create a user add form effect
64
+  createAddUserFormEffect: Observable<UserActionUnion> = createEffect(
77 65
     () => this.effectActions.pipe(
78
-      ofType(UserActionType.CREATE_A_USERFORM),
66
+      ofType(UserActionType.CREATE_FORM_ADD_USER),
79 67
       map( ()=>{
80
-        return new CreateUserFormActionSuccess({});
68
+        return new CreateFormUserAddActionSuccess({});
81 69
       }),
82
-      catchError( (err) => of(new CreateUserFormActionError(err.message)))
70
+      catchError( (err) => of(new CreateFormUserAddActionError(err.message)))
83 71
     )
84 72
   );
85 73
 
86
-  //create save a user effect: action of saving data of the form
74
+  //action of saving user form data effect
87 75
   saveUserFormEffect : Observable<UserActionUnion> = createEffect(
88 76
     () => this.effectActions.pipe(
89
-      ofType(UserActionType.SAVE_A_USERFORM),
77
+      ofType(UserActionType.SAVE_USERFORM_ADD),
90 78
       mergeMap( (userAction: UserActionUnion) => {
91 79
         return this.usersService.createUser(userAction.payload).pipe(
92 80
           map (user => new SaveUserFormActionSuccess(user)),
@@ -95,4 +83,29 @@ export class UserEffects{
95 83
       })
96 84
     )
97 85
   );
86
+
87
+  //create update user form with data of user to update effect
88
+  createUpdateUserFormEffect: Observable<UserActionUnion> = createEffect(
89
+    () => this.effectActions.pipe(
90
+      ofType(UserActionType.CREATE_FORM_USER_EDIT),
91
+      mergeMap( (userAction: UserActionUnion)=>{
92
+        return this.usersService.getUserById(userAction.payload.id).pipe(
93
+          map( (user: User)=> new CreareFormUserEditActionSuccess(user)),
94
+          catchError( (err) => of(new CreareFormUserEditActionActionError(err.message)))
95
+        )
96
+      })
97
+    )
98
+  );
99
+
100
+  updateUserFormDataEffect: Observable<UserActionUnion> = createEffect(
101
+    () => this.effectActions.pipe(
102
+      ofType(UserActionType.UPDATE_USER_FORM),
103
+      mergeMap( (userAction: UserActionUnion) =>{
104
+        return this.usersService.updateUser(userAction.payload).pipe(
105
+          map( (user: User) => new UpdateUserFormActionSuccess(user)),
106
+          catchError( err => of(new UpdateUserFormActionError(err.message)))
107
+        );
108
+      })
109
+    )
110
+  );
98 111
 }

+ 74
- 36
src/app/ngrx/users/users.reducer.ts Прегледај датотеку

@@ -5,6 +5,7 @@ import { UserActionUnion, UserActionType } from './users.actions';
5 5
 
6 6
 //create a state
7 7
 export class UserState {
8
+  currentUser!: User | null;
8 9
   usersList!: User[];
9 10
   msgState!: MsgState;
10 11
   userServerErrorMsg!: string;
@@ -13,6 +14,7 @@ export class UserState {
13 14
 //create reducer, a reducer need a initial state and an action
14 15
 const initUserState: UserState = {
15 16
   usersList: [],
17
+  currentUser: null,
16 18
   msgState: MsgState.INIT_STATE,
17 19
   userServerErrorMsg: 'No error message',
18 20
 };
@@ -21,7 +23,9 @@ export function userReducer(
21 23
   state: UserState = initUserState,
22 24
   action: Action
23 25
 ): UserState {
26
+
24 27
   switch (action.type) {
28
+
25 29
     /** get all users actions */
26 30
     case UserActionType.GET_ALL_USERS:
27 31
       return { ...state, msgState: MsgState.LOADING };
@@ -30,16 +34,17 @@ export function userReducer(
30 34
       return {
31 35
         ...state,
32 36
         msgState: MsgState.LOADED,
33
-        usersList: (<UserActionUnion>action).payload,
37
+        usersList: (<UserActionUnion> action).payload,
34 38
       };
35 39
 
36 40
     case UserActionType.GET_ALL_USERS_ERROR:
37 41
       return {
38 42
         ...state,
39
-        msgState: MsgState.ERROR,
43
+        msgState: MsgState.ERR,
40 44
         userServerErrorMsg: (<UserActionUnion>action).payload,
41 45
       };
42 46
 
47
+
43 48
     /** search a user actions */
44 49
     case UserActionType.SEARCH_A_USER:
45 50
       return { ...state, msgState: MsgState.LOADING };
@@ -54,73 +59,106 @@ export function userReducer(
54 59
     case UserActionType.SEARCH_A_USER_ERROR:
55 60
       return {
56 61
         ...state,
57
-        msgState: MsgState.ERROR,
62
+        msgState: MsgState.ERR,
58 63
         userServerErrorMsg: (<UserActionUnion>action).payload,
59 64
       };
60 65
 
61
-    /** edit a user actions */
62
-    case UserActionType.EDIT_A_USER:
66
+
67
+    /** create a user form actions */
68
+    case UserActionType.CREATE_FORM_ADD_USER:
63 69
       return {
64 70
         ...state,
65
-        msgState: MsgState.LOADING,
71
+        msgState: MsgState.FORM_LOADING,
66 72
       };
67 73
 
68
-    case UserActionType.EDIT_A_USER_SUCCESS:
69
-      let modifiedUser: User = (<UserActionUnion>action).payload;
70
-      let usersListCopy1: User[] = [...state.usersList];
71
-      let usersListNew1: User[] = usersListCopy1.map((user) =>
72
-        user.id == modifiedUser.id ? modifiedUser : user
73
-      );
74
+    case UserActionType.CREATE_FORM_ADD_USER_SUCCESS:
74 75
       return {
75 76
         ...state,
76
-        msgState: MsgState.LOADED,
77
-        usersList: usersListNew1,
77
+        msgState: MsgState.FORM_LOADED,
78 78
       };
79 79
 
80
-    case UserActionType.EDIT_A_USER_ERROR:
80
+    case UserActionType.CREATE_FORM_ADD_USER_ERROR:
81 81
       return {
82 82
         ...state,
83
-        msgState: MsgState.ERROR,
83
+        msgState: MsgState.FORM_LOAD_ERR,
84 84
         userServerErrorMsg: (<UserActionUnion>action).payload,
85 85
       };
86 86
 
87
-    /** create a user form actions */
88
-    case UserActionType.CREATE_A_USERFORM:
87
+    /** save a user (data of form) action*/
88
+    case UserActionType.SAVE_USERFORM_ADD:
89 89
       return {
90 90
         ...state,
91
-        msgState: MsgState.LOADING,
91
+        msgState: MsgState.DEFAULT_MSG,
92 92
       };
93
+    case UserActionType.SAVE_USERFORM_ADD_SUCCESS:
94
+      let createdUser: User = (<UserActionUnion>action).payload;
95
+      let usersListCopy1: User[] = [...state.usersList];
96
+      usersListCopy1.push(createdUser);
93 97
 
94
-    case UserActionType.CREATE_A_USERFORM_SUCCESS:
95 98
       return {
96 99
         ...state,
97
-        msgState: MsgState.NEW,
100
+        msgState: MsgState.LOADED,
101
+        usersList: usersListCopy1,
98 102
       };
99 103
 
100
-    case UserActionType.CREATE_A_USERFORM_ERROR:
104
+    case UserActionType.SAVE_USERFORM_ADD_ERROR:
101 105
       return {
102 106
         ...state,
103
-        msgState: MsgState.ERROR,
107
+        msgState: MsgState.ERR,
104 108
         userServerErrorMsg: (<UserActionUnion>action).payload,
105 109
       };
106 110
 
107
-    /** save a user (data of form) action*/
108
-    case UserActionType.SAVE_A_USERFORM:
111
+
112
+      /**create a edit user form actions */
113
+    case UserActionType.CREATE_FORM_USER_EDIT:
109 114
       return {
110 115
         ...state,
111
-        msgState: MsgState.LOADING,
116
+        msgState: MsgState.FORM_LOADING,
117
+      };
118
+
119
+    case UserActionType.CREATE_FORM_USER_EDIT_SUCCESS:
120
+      let modifiedUser: User = (<UserActionUnion>action).payload;
121
+      return {
122
+        ...state,
123
+        msgState: MsgState.FORM_LOADED,
124
+        currentUser: modifiedUser
112 125
       };
113
-    case UserActionType.CREATE_A_USERFORM_SUCCESS:
114
-      let createdUser: User = (<UserActionUnion>action).payload;
115
-      let usersListCopy2: User[] = [...state.usersList];
116
-      usersListCopy2.push(createdUser);
117 126
 
127
+    case UserActionType.CREATE_FORM_USER_EDIT_ERROR:
118 128
       return {
119 129
         ...state,
120
-        msgState: MsgState.SUCCESS,
121
-        usersList: usersListCopy2,
130
+        msgState: MsgState.ERR,
131
+        userServerErrorMsg: (<UserActionUnion>action).payload,
122 132
       };
123 133
 
134
+
135
+      /** update user with user form data*/
136
+
137
+      case UserActionType.UPDATE_USER_FORM:
138
+        return {
139
+          ...state,
140
+          msgState:MsgState.DEFAULT_MSG
141
+        };
142
+
143
+      case UserActionType.UPDATE_USER_FORM_SUCCESS:
144
+        let updatedUser: User = (<UserActionUnion> action).payload;
145
+        let usersListCopy2 : User[] = state.usersList.map(
146
+          (user)=>(user.id==updatedUser.id)?updatedUser:user);
147
+
148
+          return{
149
+          ...state,
150
+          currentUser: updatedUser,
151
+          msgState: MsgState.UPDATED
152
+        }
153
+
154
+      case UserActionType.UPDATE_USER_FORM_ERROR:
155
+        return{
156
+          ...state,
157
+          msgState: MsgState.ERR,
158
+          userServerErrorMsg: (<UserActionUnion>action).payload,
159
+        }
160
+
161
+
124 162
     /** delete a user actions */
125 163
     case UserActionType.DELETE_A_USER:
126 164
       return {
@@ -131,18 +169,18 @@ export function userReducer(
131 169
     case UserActionType.DELETE_A_USER_SUCCESS:
132 170
       let deletedUser: User = (<UserActionUnion>action).payload;
133 171
       let deletedUserIndex = state.usersList.indexOf(deletedUser);
134
-      let usersListNew3: User[] = [...state.usersList];
135
-      usersListNew3.splice(deletedUserIndex, 1);
172
+      let usersListCopy3: User[] = [...state.usersList];
173
+      usersListCopy3.splice(deletedUserIndex, 1);
136 174
       return {
137 175
         ...state,
138 176
         msgState: MsgState.LOADED,
139
-        usersList: usersListNew3,
177
+        usersList: usersListCopy3,
140 178
       };
141 179
 
142 180
     case UserActionType.DELETE_A_USER_ERROR:
143 181
       return {
144 182
         ...state,
145
-        msgState: MsgState.ERROR,
183
+        msgState: MsgState.ERR,
146 184
         userServerErrorMsg: (<UserActionUnion>action).payload,
147 185
       };
148 186
 

+ 206
- 198
src/app/pages/users-manager/users-management/user-create/user-create.component.html Прегледај датотеку

@@ -1,267 +1,275 @@
1
-<div class="container" *ngIf="userFormGroup">
2
-  <ng-container *ngIf="userState as userData">
3
-   
4
-    <ng-container *ngIf="userData.msgState == msgState.LOADED">
5
-      <div class="alert-success container p-3 m-3">
6
-        <span>  User form is loaded</span>
7
-        <button class="btn btn-success ml-3" (click)="onNewUser()"> create user</button>
8
-      </div>
9
-    </ng-container>
10
-
11
-    <ng-container *ngIf="userData.msgState==msgState.NEW">
12
-
13
-      <div class="card mt-3">
14
-        <div class="card-header bg-light">Update user informations</div>
15
-        <div class="card-body">
16
-          <form [formGroup]="userFormGroup" (ngSubmit)="onUserSave(); userFormGroup.reset()">
17
-
18
-            <div class="row">
19
-
20
-              <div class="col">
1
+<div class="container">
21 2
 
22
-                <div class="col-md-auto mb-3">
23
-                  <label for="id">Firstname</label>
24
-                  <input
25
-                    type="text"
26
-                    class="form-control"
27
-                    formControlName="firstname"
28
-                    [ngClass]="{
29
-                      'is-invalid':
30
-                        userFormGroup.controls['firstname'].touched &&
31
-                        !userFormGroup.controls['firstname'].valid
32
-                    }"
33
-                  />
3
+    <ng-container *ngIf="userState">
34 4
 
35
-                  <div class="invalid-feedback">
36
-                    <span
37
-                      *ngIf="userFormGroup.controls['firstname'].errors?.['required']"
38
-                      >required</span
39
-                    >
40
-                    <span
41
-                      *ngIf="userFormGroup.controls['firstname'].errors?.['minlength']"
42
-                      >min nbr chars 3</span
43
-                    >
44
-                    <span
45
-                      *ngIf="userFormGroup.controls['firstname'].errors?.['maxlength']"
46
-                      >max nbr chars 20</span
47
-                    >
48
-                  </div>
49
-                </div>
50
-
51
-                <div class="col-md-auto mb-3">
52
-                  <label for="id">Lastname</label>
53
-                  <input
54
-                    type="text"
55
-                    class="form-control"
56
-                    formControlName="lastname"
57
-                    [ngClass]="{
58
-                      'is-invalid':
59
-                        userFormGroup.controls['lastname'].touched &&
60
-                        !userFormGroup.controls['lastname'].valid
61
-                    }"
62
-                  />
5
+      <ng-container *ngIf="userState.msgState == msgState.LOADED">
6
+        <div class="alert-success container p-3 m-3">
7
+          <span> {{msgState.ADDED}}</span> &nbsp;
8
+          <button class="btn btn-success" (click)="onNewUser()"> create user</button>
9
+        </div>
10
+      </ng-container>
63 11
 
64
-                  <div class="invalid-feedback">
65
-                    <span
66
-                      *ngIf="userFormGroup.controls['lastname'].errors?.['required']"
67
-                      >required</span
68
-                    >
69
-                    <span
70
-                      *ngIf="userFormGroup.controls['lastname'].errors?.['minlength']"
71
-                      >min nbr char 3</span
72
-                    >
73
-                    <span
74
-                      *ngIf="userFormGroup.controls['lastname'].errors?.['maxlength']"
75
-                      >max nbr char 20</span
76
-                    >
77
-                  </div>
78
-                </div>
12
+      <ng-container *ngIf="userState.msgState == msgState.LOADING">
13
+        <div class="alert-danger container pt-3 mt-3">
14
+          <span> {{msgState.ERR}}</span> &nbsp;
15
+          <button class="btn btn-danger ml-3" (click)="tryEgain()"> try again</button>
16
+        </div>
17
+      </ng-container>
79 18
 
80
-                <div class="col-md-auto mb-3">
81
-                  <label for="email">Email</label>
82
-                  <input
83
-                    type="text"
84
-                    class="form-control"
85
-                    formControlName="email"
86
-                    [ngClass]="{
87
-                      'is-invalid':
88
-                        userFormGroup.controls['email'].touched &&
89
-                        !userFormGroup.controls['email'].valid
90
-                    }"
91
-                  />
19
+      <ng-container *ngIf="userState.msgState==msgState.FORM_LOADED">
92 20
 
93
-                  <div class="invalid-feedback">
94
-                    <span *ngIf="userFormGroup.controls['email'].errors?.['required']">
95
-                      required
96
-                    </span>
97
-                    <span *ngIf="userFormGroup.controls['email'].errors?.['email']">
98
-                      not valid
99
-                    </span>
100
-                  </div>
101
-                </div>
21
+        <div class="card mt-3">
22
+          <div class="card-header bg-light">Update user informations</div>
23
+          <div class="card-body">
24
+            <form [formGroup]="userFormGroup" (ngSubmit)="onUserSave(); userFormGroup.reset()">
102 25
 
103
-              </div>
26
+              <div class="row">
104 27
 
105
-              <div class="col">
28
+                <div class="col">
106 29
 
107
-                <div formGroupName="address">
108 30
                   <div class="col-md-auto mb-3">
109
-                    <label for="numRue">Street number</label>
31
+                    <label for="id">Firstname</label>
110 32
                     <input
111
-                      type="number"
33
+                      type="text"
112 34
                       class="form-control"
113
-                      formControlName="numRue"
35
+                      formControlName="firstname"
114 36
                       [ngClass]="{
115 37
                         'is-invalid':
116
-                          userFormGroup.controls['address'].get('numRue')?.touched &&
117
-                          !userFormGroup.controls['address'].get('numRue')?.valid
38
+                          userFormGroup.controls['firstname'].touched &&
39
+                          !userFormGroup.controls['firstname'].valid
118 40
                       }"
119 41
                     />
42
+
120 43
                     <div class="invalid-feedback">
121 44
                       <span
122
-                        *ngIf="userFormGroup.controls['address'].get('numRue')?.errors?.['required']"
45
+                        *ngIf="userFormGroup.controls['firstname'].errors?.['required']"
123 46
                         >required</span
124 47
                       >
125 48
                       <span
126
-                        *ngIf="userFormGroup.controls['address'].get('numRue')?.errors?.['min']"
127
-                        >street num >0
128
-                      </span>
49
+                        *ngIf="userFormGroup.controls['firstname'].errors?.['minlength']"
50
+                        >min nbr chars 3</span
51
+                      >
52
+                      <span
53
+                        *ngIf="userFormGroup.controls['firstname'].errors?.['maxlength']"
54
+                        >max nbr chars 20</span
55
+                      >
129 56
                     </div>
130 57
                   </div>
131 58
 
132 59
                   <div class="col-md-auto mb-3">
133
-                    <label for="nomRue">Street Name</label>
60
+                    <label for="id">Lastname</label>
134 61
                     <input
135 62
                       type="text"
136 63
                       class="form-control"
137
-                      formControlName="nomRue"
64
+                      formControlName="lastname"
138 65
                       [ngClass]="{
139 66
                         'is-invalid':
140
-                          userFormGroup.controls['address'].get('nomRue')?.touched &&
141
-                          !userFormGroup.controls['address'].get('nomRue')?.valid
67
+                          userFormGroup.controls['lastname'].touched &&
68
+                          !userFormGroup.controls['lastname'].valid
142 69
                       }"
143 70
                     />
71
+
144 72
                     <div class="invalid-feedback">
145 73
                       <span
146
-                        *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['required']"
74
+                        *ngIf="userFormGroup.controls['lastname'].errors?.['required']"
147 75
                         >required</span
148 76
                       >
149 77
                       <span
150
-                        *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['minlength']"
151
-                        >min chars 3
152
-                      </span>
153
-                      <span
154
-                        *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['maxlength']"
155
-                        >max chars 15
156
-                      </span>
157
-                    </div>
158
-                  </div>
159
-
160
-                  <div class="col-md-auto mb-3">
161
-                    <label for="bp">PB</label>
162
-                    <input
163
-                      type="number"
164
-                      class="form-control"
165
-                      formControlName="bp"
166
-                      [ngClass]="{
167
-                        'is-invalid':
168
-                          userFormGroup.controls['address'].get('bp')?.touched &&
169
-                          !userFormGroup.controls['address'].get('bp')?.valid
170
-                      }"
171
-                    />
172
-                    <div class="invalid-feedback">
173
-                      <span
174
-                        *ngIf="userFormGroup.controls['address'].get('bp')?.errors?.['required']"
175
-                        >required</span
78
+                        *ngIf="userFormGroup.controls['lastname'].errors?.['minlength']"
79
+                        >min nbr char 3</span
176 80
                       >
177 81
                       <span
178
-                        *ngIf="userFormGroup.controls['address'].get('bp')?.errors?.['min']"
179
-                        >pb >0
180
-                      </span>
82
+                        *ngIf="userFormGroup.controls['lastname'].errors?.['maxlength']"
83
+                        >max nbr char 20</span
84
+                      >
181 85
                     </div>
182 86
                   </div>
183 87
 
184 88
                   <div class="col-md-auto mb-3">
185
-                    <label for="city">City</label>
89
+                    <label for="email">Email</label>
186 90
                     <input
187 91
                       type="text"
188 92
                       class="form-control"
189
-                      formControlName="ville"
93
+                      formControlName="email"
190 94
                       [ngClass]="{
191 95
                         'is-invalid':
192
-                          userFormGroup.controls['address'].get('ville')?.touched &&
193
-                          !userFormGroup.controls['address'].get('vile')?.valid
96
+                          userFormGroup.controls['email'].touched &&
97
+                          !userFormGroup.controls['email'].valid
194 98
                       }"
195 99
                     />
100
+
196 101
                     <div class="invalid-feedback">
197
-                      <span
198
-                        *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['required']"
199
-                        >required</span
200
-                      >
201
-                      <span
202
-                        *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['minlength']"
203
-                        >min chars 3
102
+                      <span *ngIf="userFormGroup.controls['email'].errors?.['required']">
103
+                        required
204 104
                       </span>
205
-                      <span
206
-                        *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['maxlength']"
207
-                        >max chars 15
105
+                      <span *ngIf="userFormGroup.controls['email'].errors?.['email']">
106
+                        not valid
208 107
                       </span>
209 108
                     </div>
210 109
                   </div>
211 110
 
212
-                  <div class="col-md-auto mb-3">
213
-                    <label for="pays">Country</label>
214
-                    <input
215
-                      type="text"
216
-                      class="form-control"
217
-                      formControlName="pays"
218
-                      [ngClass]="{
219
-                        'is-invalid':
220
-                          userFormGroup.controls['address'].get('pays')?.touched &&
221
-                          !userFormGroup.controls['address'].get('pays')?.valid
222
-                      }"
223
-                    />
224
-                    <div class="invalid-feedback">
225
-                      <span
226
-                        *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['required']"
227
-                        >required</span
228
-                      >
229
-                      <span
230
-                        *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['minlength']"
231
-                        >min chars 3
232
-                      </span>
233
-                      <span
234
-                        *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['maxlength']"
235
-                        >max chars 15
236
-                      </span>
111
+                </div>
112
+
113
+                <div class="col">
114
+
115
+                  <div formGroupName="address">
116
+                    <div class="col-md-auto mb-3">
117
+                      <label for="numRue">Street number</label>
118
+                      <input
119
+                        type="number"
120
+                        class="form-control"
121
+                        formControlName="numRue"
122
+                        [ngClass]="{
123
+                          'is-invalid':
124
+                            userFormGroup.controls['address'].get('numRue')?.touched &&
125
+                            !userFormGroup.controls['address'].get('numRue')?.valid
126
+                        }"
127
+                      />
128
+                      <div class="invalid-feedback">
129
+                        <span
130
+                          *ngIf="userFormGroup.controls['address'].get('numRue')?.errors?.['required']"
131
+                          >required</span
132
+                        >
133
+                        <span
134
+                          *ngIf="userFormGroup.controls['address'].get('numRue')?.errors?.['min']"
135
+                          >street num >0
136
+                        </span>
137
+                      </div>
138
+                    </div>
139
+
140
+                    <div class="col-md-auto mb-3">
141
+                      <label for="nomRue">Street Name</label>
142
+                      <input
143
+                        type="text"
144
+                        class="form-control"
145
+                        formControlName="nomRue"
146
+                        [ngClass]="{
147
+                          'is-invalid':
148
+                            userFormGroup.controls['address'].get('nomRue')?.touched &&
149
+                            !userFormGroup.controls['address'].get('nomRue')?.valid
150
+                        }"
151
+                      />
152
+                      <div class="invalid-feedback">
153
+                        <span
154
+                          *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['required']"
155
+                          >required</span
156
+                        >
157
+                        <span
158
+                          *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['minlength']"
159
+                          >min chars 3
160
+                        </span>
161
+                        <span
162
+                          *ngIf="userFormGroup.controls['address'].get('nomRue')?.errors?.['maxlength']"
163
+                          >max chars 15
164
+                        </span>
165
+                      </div>
166
+                    </div>
167
+
168
+                    <div class="col-md-auto mb-3">
169
+                      <label for="bp">PB</label>
170
+                      <input
171
+                        type="number"
172
+                        class="form-control"
173
+                        formControlName="bp"
174
+                        [ngClass]="{
175
+                          'is-invalid':
176
+                            userFormGroup.controls['address'].get('bp')?.touched &&
177
+                            !userFormGroup.controls['address'].get('bp')?.valid
178
+                        }"
179
+                      />
180
+                      <div class="invalid-feedback">
181
+                        <span
182
+                          *ngIf="userFormGroup.controls['address'].get('bp')?.errors?.['required']"
183
+                          >required</span
184
+                        >
185
+                        <span
186
+                          *ngIf="userFormGroup.controls['address'].get('bp')?.errors?.['min']"
187
+                          >pb >0
188
+                        </span>
189
+                      </div>
190
+                    </div>
191
+
192
+                    <div class="col-md-auto mb-3">
193
+                      <label for="city">City</label>
194
+                      <input
195
+                        type="text"
196
+                        class="form-control"
197
+                        formControlName="ville"
198
+                        [ngClass]="{
199
+                          'is-invalid':
200
+                            userFormGroup.controls['address'].get('ville')?.touched &&
201
+                            !userFormGroup.controls['address'].get('vile')?.valid
202
+                        }"
203
+                      />
204
+                      <div class="invalid-feedback">
205
+                        <span
206
+                          *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['required']"
207
+                          >required</span
208
+                        >
209
+                        <span
210
+                          *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['minlength']"
211
+                          >min chars 3
212
+                        </span>
213
+                        <span
214
+                          *ngIf="userFormGroup.controls['address'].get('ville')?.errors?.['maxlength']"
215
+                          >max chars 15
216
+                        </span>
217
+                      </div>
218
+                    </div>
219
+
220
+                    <div class="col-md-auto mb-3">
221
+                      <label for="pays">Country</label>
222
+                      <input
223
+                        type="text"
224
+                        class="form-control"
225
+                        formControlName="pays"
226
+                        [ngClass]="{
227
+                          'is-invalid':
228
+                            userFormGroup.controls['address'].get('pays')?.touched &&
229
+                            !userFormGroup.controls['address'].get('pays')?.valid
230
+                        }"
231
+                      />
232
+                      <div class="invalid-feedback">
233
+                        <span
234
+                          *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['required']"
235
+                          >required</span
236
+                        >
237
+                        <span
238
+                          *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['minlength']"
239
+                          >min chars 3
240
+                        </span>
241
+                        <span
242
+                          *ngIf="userFormGroup.controls['address'].get('pays')?.errors?.['maxlength']"
243
+                          >max chars 15
244
+                        </span>
245
+                      </div>
237 246
                     </div>
238 247
                   </div>
248
+
239 249
                 </div>
240 250
 
241 251
               </div>
242 252
 
243
-            </div>
244
-
245
-            <div class="row">
246
-              <div class="text-center">
247
-                <button
248
-                  class="btn btn-outline-primary"
249
-                  type="submit"
250
-                  [disabled]="!userFormGroup.valid"
251
-                >
252
-                  save</button
253
-                >&nbsp;
254
-                <button class="btn btn-outline-danger" type="reset">reset</button>
253
+              <div class="row">
254
+                <div class="text-center">
255
+                  <button
256
+                    class="btn btn-outline-primary"
257
+                    type="submit"
258
+                    [disabled]="!userFormGroup.valid"
259
+                  >
260
+                    save</button
261
+                  >&nbsp;
262
+                  <button class="btn btn-outline-danger" type="reset">reset</button>
263
+                </div>
255 264
               </div>
256
-            </div>
257 265
 
258
-          </form>
266
+            </form>
267
+          </div>
259 268
         </div>
260
-      </div>
261 269
 
262 270
 
263
-    </ng-container>
271
+      </ng-container>
264 272
 
265
-  </ng-container>
273
+    </ng-container>
266 274
 
267 275
 </div>

+ 27
- 14
src/app/pages/users-manager/users-management/user-create/user-create.component.ts Прегледај датотеку

@@ -1,6 +1,8 @@
1
+import { UsersService } from './../../../../shared/services/users.service';
2
+import { User } from './../../../../shared/models/user.model';
1 3
 import { FormGroup, FormBuilder, Validators } from '@angular/forms';
2 4
 import {
3
-  CreateUserFormAction,
5
+  CreateFormUserAddAction,
4 6
   SaveUserFormAction,
5 7
 } from './../../../../ngrx/users/users.actions';
6 8
 import { Store } from '@ngrx/store';
@@ -8,7 +10,6 @@ import { Component, OnInit } from '@angular/core';
8 10
 import { MsgState } from 'src/app/ngrx/msg.state';
9 11
 import { UserState } from 'src/app/ngrx/users/user.state';
10 12
 
11
-
12 13
 @Component({
13 14
   selector: 'app-user-create',
14 15
   templateUrl: './user-create.component.html',
@@ -19,19 +20,17 @@ export class UserCreateComponent implements OnInit {
19 20
 
20 21
   constructor(
21 22
     private store: Store<{ userReducerKey: UserState }>,
22
-    private fb: FormBuilder
23
+    private fb: FormBuilder,
24
+    private usersService: UsersService
23 25
   ) {}
24 26
   userState!: UserState;
25 27
   readonly msgState = MsgState;
26 28
 
27 29
   ngOnInit(): void {
30
+    this.store.dispatch(new CreateFormUserAddAction());
28 31
 
29
-    this.store.dispatch(new CreateUserFormAction({}));
30
-
31
-    this.store.subscribe((state) => {
32
-      this.userState = state.userReducerKey;
33
-
34
-      if(this.userState.msgState==this.msgState.NEW){
32
+    this.store.subscribe((myState) => {
33
+      this.userState = myState.userReducerKey;
35 34
 
36 35
         this.userFormGroup = this.fb.group({
37 36
           id: [0, Validators.required],
@@ -81,17 +80,31 @@ export class UserCreateComponent implements OnInit {
81 80
             ],
82 81
           }),
83 82
         });
84
-      }
85 83
     });
86 84
   }
87 85
 
88 86
   onUserSave() {
89
-    if (window.confirm(MsgState.CONFIRM)) {
90
-      this.store.dispatch(new SaveUserFormAction(this.userFormGroup.value));
91
-    }
87
+
88
+   let user: User = this.userFormGroup.value;
89
+   let lastname: string = user.lastname;
90
+
91
+   this.usersService.getUserByName(lastname).subscribe((users: User[]) => {
92
+     if (users.length > 0) {
93
+       alert(lastname + ' ' + this.msgState.EXIST);
94
+       return;
95
+     }
96
+
97
+     if (window.confirm(MsgState.CONFIRM)) {
98
+       this.store.dispatch(new SaveUserFormAction(this.userFormGroup.value));
99
+     }
100
+   });
92 101
   }
93 102
 
94 103
   onNewUser() {
95
-    this.store.dispatch(new CreateUserFormAction({}));
104
+    this.store.dispatch(new CreateFormUserAddAction());
105
+  }
106
+
107
+  tryEgain() {
108
+    this.store.dispatch(new CreateFormUserAddAction());
96 109
   }
97 110
 }

+ 1
- 1
src/app/pages/users-manager/users-management/user-nav-bar/user-nav-bar.component.ts Прегледај датотеку

@@ -1,7 +1,7 @@
1 1
 import { UserState } from './../../../../ngrx/users/user.state';
2 2
 import {
3 3
   SearchUsersAction,
4
-  CreateUserFormAction,
4
+  CreateFormUserAddAction,
5 5
 } from './../../../../ngrx/users/users.actions';
6 6
 import { GetAllUsersAction } from '../../../../ngrx/users/users.actions';
7 7
 import { Component } from '@angular/core';

+ 0
- 0
src/app/pages/users-manager/users-management/user-update/user-update.component.css Прегледај датотеку


+ 269
- 0
src/app/pages/users-manager/users-management/user-update/user-update.component.html Прегледај датотеку

@@ -0,0 +1,269 @@
1
+<div class="container" *ngIf="userFormGrp">
2
+
3
+  <ng-container *ngIf="userState">
4
+
5
+    <ng-container *ngIf="userState.msgState==msg.ERR">
6
+      <div class="alert-danger container">
7
+        {{msg.ERR}}
8
+      </div>
9
+    </ng-container>
10
+
11
+    <ng-container *ngIf="userState.msgState == msg.UPDATED">
12
+      <div class="alert-success container p-3 m-3" style="width: 30em;">
13
+        <span>   {{msg.UPDATED}}</span> &nbsp;&nbsp;
14
+        <button class="btn btn-success" (click)="onUpdateOk()"> OK </button>
15
+      </div>
16
+    </ng-container>
17
+
18
+    <ng-container *ngIf="userState.msgState==msg.FORM_LOADED">
19
+
20
+      <div class="card mt-3">
21
+        <div class="card-header bg-light">User informations to modify: ID of user: <b>{{userId}}</b> </div>
22
+        <div class="card-body">
23
+          <form [formGroup]="userFormGrp" (ngSubmit)="onUserUpdate(); userFormGrp.reset()">
24
+
25
+            <div class="row">
26
+
27
+              <div class="col">
28
+
29
+                <div class="col-md-auto mb-3">
30
+                  <label for="id">Firstname</label>
31
+                  <input
32
+                    type="text"
33
+                    class="form-control"
34
+                    formControlName="firstname"
35
+                    [ngClass]="{
36
+                      'is-invalid':
37
+                      userFormGrp.controls['firstname'].touched &&
38
+                        !userFormGrp.controls['firstname'].valid
39
+                    }"
40
+                  />
41
+
42
+                  <div class="invalid-feedback">
43
+                    <span
44
+                      *ngIf="userFormGrp.controls['firstname'].errors?.['required']"
45
+                      >required</span
46
+                    >
47
+                    <span
48
+                      *ngIf="userFormGrp.controls['firstname'].errors?.['minlength']"
49
+                      >min nbr chars 3</span
50
+                    >
51
+                    <span
52
+                      *ngIf="userFormGrp.controls['firstname'].errors?.['maxlength']"
53
+                      >max nbr chars 20</span
54
+                    >
55
+                  </div>
56
+                </div>
57
+
58
+                <div class="col-md-auto mb-3">
59
+                  <label for="id">Lastname</label>
60
+                  <input
61
+                    type="text"
62
+                    class="form-control"
63
+                    formControlName="lastname"
64
+                    [ngClass]="{
65
+                      'is-invalid':
66
+                      userFormGrp.controls['lastname'].touched &&
67
+                        !userFormGrp.controls['lastname'].valid
68
+                    }"
69
+                  />
70
+
71
+                  <div class="invalid-feedback">
72
+                    <span
73
+                      *ngIf="userFormGrp.controls['lastname'].errors?.['required']"
74
+                      >required</span
75
+                    >
76
+                    <span
77
+                      *ngIf="userFormGrp.controls['lastname'].errors?.['minlength']"
78
+                      >min nbr char 3</span
79
+                    >
80
+                    <span
81
+                      *ngIf="userFormGrp.controls['lastname'].errors?.['maxlength']"
82
+                      >max nbr char 20</span
83
+                    >
84
+                  </div>
85
+                </div>
86
+
87
+                <div class="col-md-auto mb-3">
88
+                  <label for="email">Email</label>
89
+                  <input
90
+                    type="text"
91
+                    class="form-control"
92
+                    formControlName="email"
93
+                    [ngClass]="{
94
+                      'is-invalid':
95
+                        userFormGrp.controls['email'].touched &&
96
+                        !userFormGrp.controls['email'].valid
97
+                    }"
98
+                  />
99
+
100
+                  <div class="invalid-feedback">
101
+                    <span *ngIf="userFormGrp.controls['email'].errors?.['required']">
102
+                      required
103
+                    </span>
104
+                    <span *ngIf="userFormGrp.controls['email'].errors?.['email']">
105
+                      not valid
106
+                    </span>
107
+                  </div>
108
+                </div>
109
+
110
+              </div>
111
+
112
+              <div class="col">
113
+
114
+                <div formGroupName="address">
115
+                  <div class="col-md-auto mb-3">
116
+                    <label for="numRue">Street number</label>
117
+                    <input
118
+                      type="number"
119
+                      class="form-control"
120
+                      formControlName="numRue"
121
+                      [ngClass]="{
122
+                        'is-invalid':
123
+                          userFormGrp.controls['address'].get('numRue')?.touched &&
124
+                          !userFormGrp.controls['address'].get('numRue')?.valid
125
+                      }"
126
+                    />
127
+                    <div class="invalid-feedback">
128
+                      <span
129
+                        *ngIf="userFormGrp.controls['address'].get('numRue')?.errors?.['required']"
130
+                        >required</span
131
+                      >
132
+                      <span
133
+                        *ngIf="userFormGrp.controls['address'].get('numRue')?.errors?.['min']"
134
+                        >street num >0
135
+                      </span>
136
+                    </div>
137
+                  </div>
138
+
139
+                  <div class="col-md-auto mb-3">
140
+                    <label for="nomRue">Street Name</label>
141
+                    <input
142
+                      type="text"
143
+                      class="form-control"
144
+                      formControlName="nomRue"
145
+                      [ngClass]="{
146
+                        'is-invalid':
147
+                          userFormGrp.controls['address'].get('nomRue')?.touched &&
148
+                          !userFormGrp.controls['address'].get('nomRue')?.valid
149
+                      }"
150
+                    />
151
+                    <div class="invalid-feedback">
152
+                      <span
153
+                        *ngIf="userFormGrp.controls['address'].get('nomRue')?.errors?.['required']"
154
+                        >required</span
155
+                      >
156
+                      <span
157
+                        *ngIf="userFormGrp.controls['address'].get('nomRue')?.errors?.['minlength']"
158
+                        >min chars 3
159
+                      </span>
160
+                      <span
161
+                        *ngIf="userFormGrp.controls['address'].get('nomRue')?.errors?.['maxlength']"
162
+                        >max chars 15
163
+                      </span>
164
+                    </div>
165
+                  </div>
166
+
167
+                  <div class="col-md-auto mb-3">
168
+                    <label for="bp">PB</label>
169
+                    <input
170
+                      type="number"
171
+                      class="form-control"
172
+                      formControlName="bp"
173
+                      [ngClass]="{
174
+                        'is-invalid':
175
+                          userFormGrp.controls['address'].get('bp')?.touched &&
176
+                          !userFormGrp.controls['address'].get('bp')?.valid
177
+                      }"
178
+                    />
179
+                    <div class="invalid-feedback">
180
+                      <span
181
+                        *ngIf="userFormGrp.controls['address'].get('bp')?.errors?.['required']"
182
+                        >required</span
183
+                      >
184
+                      <span
185
+                        *ngIf="userFormGrp.controls['address'].get('bp')?.errors?.['min']"
186
+                        >pb >0
187
+                      </span>
188
+                    </div>
189
+                  </div>
190
+
191
+                  <div class="col-md-auto mb-3">
192
+                    <label for="city">City</label>
193
+                    <input
194
+                      type="text"
195
+                      class="form-control"
196
+                      formControlName="ville"
197
+                      [ngClass]="{
198
+                        'is-invalid':
199
+                           userFormGrp.controls['address'].get('ville')?.touched &&
200
+                          !userFormGrp.controls['address'].get('vile')?.valid
201
+                      }"
202
+                    />
203
+                    <div class="invalid-feedback">
204
+                      <span
205
+                        *ngIf="userFormGrp.controls['address'].get('ville')?.errors?.['required']"
206
+                        >required</span
207
+                      >
208
+                      <span
209
+                        *ngIf="userFormGrp.controls['address'].get('ville')?.errors?.['minlength']"
210
+                        >min chars 3
211
+                      </span>
212
+                      <span
213
+                        *ngIf="userFormGrp.controls['address'].get('ville')?.errors?.['maxlength']"
214
+                        >max chars 15
215
+                      </span>
216
+                    </div>
217
+                  </div>
218
+
219
+                  <div class="col-md-auto mb-3">
220
+                    <label for="pays">Country</label>
221
+                    <input
222
+                      type="text"
223
+                      class="form-control"
224
+                      formControlName="pays"
225
+                      [ngClass]="{
226
+                        'is-invalid':
227
+                          userFormGrp.controls['address'].get('pays')?.touched &&
228
+                          !userFormGrp.controls['address'].get('pays')?.valid
229
+                      }"
230
+                    />
231
+                    <div class="invalid-feedback">
232
+                      <span
233
+                        *ngIf="userFormGrp.controls['address'].get('pays')?.errors?.['required']"
234
+                        >required</span
235
+                      >
236
+                      <span
237
+                        *ngIf="userFormGrp.controls['address'].get('pays')?.errors?.['minlength']"
238
+                        >min chars 3
239
+                      </span>
240
+                      <span
241
+                        *ngIf="userFormGrp.controls['address'].get('pays')?.errors?.['maxlength']"
242
+                        >max chars 15
243
+                      </span>
244
+                    </div>
245
+                  </div>
246
+                </div>
247
+
248
+              </div>
249
+
250
+            </div>
251
+
252
+            <div class="text-center col-md-auto mt-3">
253
+              <button
254
+                class="btn btn-outline-primary"
255
+                type="submit"
256
+                [disabled]="!userFormGrp.valid"
257
+              >
258
+                update
259
+              </button>
260
+            </div>
261
+          </form>
262
+        </div>
263
+      </div>
264
+
265
+    </ng-container>
266
+
267
+  </ng-container>
268
+
269
+</div>

+ 25
- 0
src/app/pages/users-manager/users-management/user-update/user-update.component.spec.ts Прегледај датотеку

@@ -0,0 +1,25 @@
1
+import { ComponentFixture, TestBed } from '@angular/core/testing';
2
+
3
+import { UserUpdateComponent } from './user-update.component';
4
+
5
+describe('UserUpdateComponent', () => {
6
+  let component: UserUpdateComponent;
7
+  let fixture: ComponentFixture<UserUpdateComponent>;
8
+
9
+  beforeEach(async () => {
10
+    await TestBed.configureTestingModule({
11
+      declarations: [ UserUpdateComponent ]
12
+    })
13
+    .compileComponents();
14
+  });
15
+
16
+  beforeEach(() => {
17
+    fixture = TestBed.createComponent(UserUpdateComponent);
18
+    component = fixture.componentInstance;
19
+    fixture.detectChanges();
20
+  });
21
+
22
+  it('should create', () => {
23
+    expect(component).toBeTruthy();
24
+  });
25
+});

+ 110
- 0
src/app/pages/users-manager/users-management/user-update/user-update.component.ts Прегледај датотеку

@@ -0,0 +1,110 @@
1
+import { MsgState } from 'src/app/ngrx/msg.state';
2
+import { UsersService } from './../../../../shared/services/users.service';
3
+import { FormGroup, FormBuilder, Validators } from '@angular/forms';
4
+import { CreareFormUserEditAction, UpdateUserFormAction } from './../../../../ngrx/users/users.actions';
5
+import { UserState } from 'src/app/ngrx/users/user.state';
6
+import { Component, OnInit } from '@angular/core';
7
+import { ActivatedRoute, Router } from '@angular/router';
8
+import { Store } from '@ngrx/store';
9
+
10
+@Component({
11
+  selector: 'app-user-update',
12
+  templateUrl: './user-update.component.html',
13
+  styleUrls: ['./user-update.component.css'],
14
+})
15
+export class UserUpdateComponent implements OnInit {
16
+  userId: number;
17
+  userFormGrp!: FormGroup;
18
+  userState!: UserState;
19
+
20
+  constructor(
21
+    activatedRoute: ActivatedRoute,
22
+    private usersService: UsersService,
23
+    private store: Store<{ userReducerKey: UserState }>,
24
+    private fb: FormBuilder,
25
+    private router: Router
26
+  ) {
27
+    this.userId = activatedRoute.snapshot.params['id'];
28
+  }
29
+
30
+  readonly msg = MsgState;
31
+
32
+  ngOnInit(): void {
33
+    this.usersService.getUserById(this.userId).subscribe((user) => {
34
+      this.store.dispatch(new CreareFormUserEditAction(user));
35
+
36
+      this.store.subscribe((state) => {
37
+        this.userState = state.userReducerKey;
38
+
39
+        this.userFormGrp = this.fb.group({
40
+          id: [this.userState.currentUser?.id, Validators.required],
41
+          firstname: [
42
+            this.userState.currentUser?.firstname,
43
+            [
44
+                Validators.required,
45
+                Validators.minLength(3),
46
+                Validators.maxLength(15),
47
+            ],
48
+            ],
49
+            lastname: [
50
+              this.userState.currentUser?.lastname,
51
+              [
52
+                Validators.required,
53
+                Validators.minLength(3),
54
+                Validators.maxLength(15),
55
+              ],
56
+            ],
57
+            email: [
58
+              this.userState.currentUser?.email,
59
+              [Validators.required, Validators.email],
60
+            ],
61
+            address: this.fb.group({
62
+              numRue: [
63
+                this.userState.currentUser?.address.numRue,
64
+                [Validators.required, Validators.min(1)],
65
+              ],
66
+              nomRue: [
67
+                this.userState.currentUser?.address.nomRue,
68
+                [
69
+                  Validators.required,
70
+                  Validators.minLength(3),
71
+                  Validators.maxLength(25),
72
+                ],
73
+              ],
74
+              bp: [
75
+                this.userState.currentUser?.address.bp,
76
+                [Validators.required, Validators.min(1)],
77
+              ],
78
+              ville: [
79
+                this.userState.currentUser?.address.ville,
80
+                [
81
+                  Validators.required,
82
+                  Validators.minLength(3),
83
+                  Validators.maxLength(15),
84
+                ],
85
+              ],
86
+              pays: [
87
+                this.userState.currentUser?.address.pays,
88
+                [
89
+                  Validators.required,
90
+                  Validators.minLength(3),
91
+                  Validators.maxLength(15),
92
+                ],
93
+              ],
94
+            }),
95
+          });
96
+
97
+      });
98
+    });
99
+  }
100
+
101
+  onUserUpdate() {
102
+    if(window.confirm(MsgState.CONFIRM)){
103
+      this.store.dispatch(new UpdateUserFormAction(this.userFormGrp.value));
104
+    }
105
+  }
106
+
107
+  onUpdateOk(){
108
+    this.router.navigateByUrl('/users-management')
109
+  }
110
+}

+ 3
- 3
src/app/pages/users-manager/users-management/users-list/users-list.component.html Прегледај датотеку

@@ -4,10 +4,10 @@ responsiveLayout="scroll"
4 4
 *ngIf="inputState.usersList && inputState.usersList.length > 0"
5 5
 styleClass="p-datatable-striped"
6 6
 [paginator]="true"
7
-[rows]="3"
7
+[rows]="5"
8 8
 [showCurrentPageReport]="true"
9 9
 currentPageReportTemplate="{first} - {last} of {totalRecords} users"
10
-[rowsPerPageOptions]="[3, 5, 10]"
10
+[rowsPerPageOptions]="[5, 8, 10]"
11 11
 >
12 12
 <ng-template pTemplate="header">
13 13
   <tr>
@@ -38,7 +38,7 @@ currentPageReportTemplate="{first} - {last} of {totalRecords} users"
38 38
       <button class="btn">
39 39
         <i
40 40
           class="fa fa-pencil"
41
-          style="color: mediumturquoise"
41
+          style="color:cadetblue"
42 42
           (click)="onUserEdit(user)"
43 43
         ></i>
44 44
       </button>

+ 19
- 13
src/app/pages/users-manager/users-management/users-list/users-list.component.ts Прегледај датотеку

@@ -1,4 +1,9 @@
1
-import { EditUsersAction, DeleteUsersAction } from './../../../../ngrx/users/users.actions';
1
+import { Router } from '@angular/router';
2
+import { userReducer } from 'src/app/ngrx/users/users.reducer';
3
+import {
4
+  CreareFormUserEditAction,
5
+  DeleteUsersAction,
6
+} from './../../../../ngrx/users/users.actions';
2 7
 import { User } from 'src/app/shared/models/user.model';
3 8
 import { Input } from '@angular/core';
4 9
 import { Component } from '@angular/core';
@@ -9,22 +14,23 @@ import { MsgState } from 'src/app/ngrx/msg.state';
9 14
 @Component({
10 15
   selector: 'app-users-list',
11 16
   templateUrl: './users-list.component.html',
12
-  styleUrls: ['./users-list.component.css']
17
+  styleUrls: ['./users-list.component.css'],
13 18
 })
14 19
 export class UsersListComponent {
20
+  @Input() inputState!: UserState;
15 21
 
16
- @Input()  inputState!: UserState
22
+  constructor(
23
+    private store: Store<{ userReducerKey: UserState }>,
24
+    private router: Router
25
+  ) {}
17 26
 
18
- constructor(private store: Store<any>){}
19
-
20
- onUserEdit(user:User){
21
-   this.store.dispatch(new EditUsersAction({user}))
22
- }
23
-
24
- onUserDelete(user: User){
27
+  onUserEdit(user: User) {
28
+    this.router.navigateByUrl('/user-update/'+user.id)
29
+  }
25 30
 
26
-  if(window.confirm(MsgState.CONFIRM)){
27
-    this.store.dispatch(new DeleteUsersAction(user));
31
+  onUserDelete(user: User) {
32
+    if (window.confirm(MsgState.CONFIRM)) {
33
+      this.store.dispatch(new DeleteUsersAction(user));
34
+    }
28 35
   }
29
- }
30 36
 }

+ 1
- 1
src/app/pages/users-manager/users-management/users-management.component.html Прегледај датотеку

@@ -11,7 +11,7 @@
11 11
       <div class="p-2">{{userMsg.LOADING}}</div>
12 12
     </ng-container>
13 13
 
14
-    <ng-container *ngSwitchCase="userMsg.ERROR">
14
+    <ng-container *ngSwitchCase="userMsg.ERR">
15 15
       <div class="p-2 text-danger"> {{userState.userServerErrorMsg}} </div>
16 16
     </ng-container>
17 17
 

+ 4
- 3
src/app/pages/users-manager/users-management/users-management.component.ts Прегледај датотеку

@@ -13,9 +13,10 @@ import { MsgState } from 'src/app/ngrx/msg.state';
13 13
 export class UsersManagementComponent implements OnInit {
14 14
 
15 15
   userState$!: Observable<{
16
-    usersList: User[];
17
-    msgState: MsgState;
18
-    userServerErrorMsg: string;
16
+    currentUser: User | null,
17
+    usersList: User[],
18
+    msgState: MsgState,
19
+    userServerErrorMsg: string
19 20
   }>;
20 21
 
21 22
   readonly userMsg = MsgState;

+ 4
- 2
src/app/pages/users-manager/users-manager-routing.module.ts Прегледај датотеку

@@ -1,11 +1,13 @@
1
+import { UserUpdateComponent } from './users-management/user-update/user-update.component';
1 2
 import { UsersManagementComponent } from './users-management/users-management.component';
2 3
 import { NgModule } from '@angular/core';
3 4
 import { RouterModule, Routes } from '@angular/router';
4 5
 import { UserCreateComponent } from './users-management/user-create/user-create.component';
5 6
 
6 7
 const routes: Routes = [
7
-  {path: 'user-create',component: UserCreateComponent},
8
-  {path:'users-management', component: UsersManagementComponent}
8
+  { path: 'user-create', component: UserCreateComponent },
9
+  { path: 'users-management', component: UsersManagementComponent },
10
+  { path: 'user-update/:id', component: UserUpdateComponent },
9 11
 ];
10 12
 
11 13
 @NgModule({

+ 3
- 1
src/app/pages/users-manager/users-manager.module.ts Прегледај датотеку

@@ -13,6 +13,7 @@ import { userReducer } from 'src/app/ngrx/users/users.reducer';
13 13
 import { TableModule } from "primeng/table";
14 14
 import { UsersListComponent } from './users-management/users-list/users-list.component';
15 15
 import { FormsModule, ReactiveFormsModule } from '@angular/forms';
16
+import { UserUpdateComponent } from './users-management/user-update/user-update.component';
16 17
 
17 18
 
18 19
 
@@ -21,7 +22,8 @@ import { FormsModule, ReactiveFormsModule } from '@angular/forms';
21 22
     UserCreateComponent,
22 23
     UserNavBarComponent,
23 24
     UsersManagementComponent,
24
-    UsersListComponent
25
+    UsersListComponent,
26
+    UserUpdateComponent
25 27
   ],
26 28
   imports: [
27 29
     CommonModule,

+ 4
- 0
src/app/shared/models/company.type.ts Прегледај датотеку

@@ -0,0 +1,4 @@
1
+export enum CompanyType{
2
+  CLIENT = "CLIENT",
3
+  PROSPECT= "PROSPECT"
4
+}

+ 5
- 2
src/app/shared/services/users.service.ts Прегледај датотеку

@@ -25,8 +25,7 @@ export class UsersService {
25 25
     });
26 26
   }
27 27
 
28
-  baseUrl: string =
29
-    Math.random() > 0.2 ? environment.host : environment.unreachibleHost;
28
+  baseUrl: string = Math.random() > 0.2 ? environment.host : environment.unreachibleHost;
30 29
   headers = { 'content-type': 'application/json' };
31 30
 
32 31
   getAllUsers(): Observable<User[]> {
@@ -61,6 +60,10 @@ export class UsersService {
61 60
     return this.http.get<User>(this.baseUrl + '/users/' + userId);
62 61
   }
63 62
 
63
+  getUserByName(lastname: string): Observable<User[]>{
64
+    return this.http.get<User[]>(this.baseUrl+'/users?lastname='+lastname);
65
+  }
66
+
64 67
   updateUser(user: User): Observable<User> {
65 68
     return this.http
66 69
       .put<User>(this.baseUrl + '/users/' + user.id, user, {

Powered by TurnKey Linux.