Angular project using NgRx pattern to manage state of a system

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. import { Action } from '@ngrx/store';
  2. import { User } from 'src/app/shared/models/user/user.model';
  3. import { MsgState } from '../msg.state';
  4. import { UserActionTypes } from './user.action.types';
  5. import { UserState } from './user.state';
  6. import { UserActionUnion } from './users.actions';
  7. /** create reducer,
  8. *
  9. * a reducer need a initial state and an action to perform*/
  10. const initialUserState: UserState = {
  11. usersList: [],
  12. currentUser: null,
  13. msgState: MsgState.INIT_STATE,
  14. serverMsgError: 'No error message'
  15. };
  16. export function userReducer(
  17. state: UserState = initialUserState,
  18. action: Action
  19. ): UserState {
  20. switch (action.type) {
  21. // create a user form action to add a suer
  22. case UserActionTypes.CREATE_FORM_ADD_USER:
  23. return {
  24. ...state,
  25. msgState: MsgState.FORM_LOADING
  26. };
  27. case UserActionTypes.CREATE_FORM_ADD_USER_SUCCESS:
  28. return {
  29. ...state,
  30. msgState: MsgState.FORM_LOADED
  31. };
  32. case UserActionTypes.CREATE_FORM_ADD_USER_ERROR:
  33. return {
  34. ...state,
  35. msgState: MsgState.FORM_LOAD_ERR,
  36. serverMsgError: (<UserActionUnion>action).payload
  37. };
  38. // save a user (data of form) action
  39. case UserActionTypes.SAVE_USERFORM_ADD:
  40. return {
  41. ...state,
  42. msgState: MsgState.ADD
  43. };
  44. case UserActionTypes.SAVE_USERFORM_ADD_SUCCESS:
  45. let createdUser: User = (<UserActionUnion>action).payload;
  46. let usersListCopy1: User[] = [...state.usersList];
  47. usersListCopy1.push(createdUser);
  48. return {
  49. ...state,
  50. msgState: MsgState.ADDED,
  51. usersList: usersListCopy1
  52. };
  53. case UserActionTypes.SAVE_USERFORM_ADD_ERROR:
  54. return {
  55. ...state,
  56. msgState: MsgState.ERR,
  57. serverMsgError: (<UserActionUnion>action).payload
  58. };
  59. // get all users actions
  60. case UserActionTypes.GET_ALL_USERS:
  61. return { ...state, msgState: MsgState.LOADING };
  62. case UserActionTypes.GET_ALL_USERS_SUCCESS:
  63. return {
  64. ...state,
  65. msgState: MsgState.LOADED,
  66. usersList: (<UserActionUnion>action).payload,
  67. };
  68. case UserActionTypes.GET_ALL_USERS_ERROR:
  69. return {
  70. ...state,
  71. msgState: MsgState.ERR,
  72. serverMsgError: (<UserActionUnion>action).payload
  73. };
  74. // create a edit user form actions
  75. case UserActionTypes.CREATE_FORM_USER_EDIT:
  76. return {
  77. ...state,
  78. msgState: MsgState.FORM_LOADING,
  79. };
  80. case UserActionTypes.CREATE_FORM_USER_EDIT_SUCCESS:
  81. let modifiedUser: User = (<UserActionUnion>action).payload;
  82. return {
  83. ...state,
  84. msgState: MsgState.FORM_LOADED,
  85. currentUser: modifiedUser
  86. };
  87. case UserActionTypes.CREATE_FORM_USER_EDIT_ERROR:
  88. return {
  89. ...state,
  90. msgState: MsgState.ERR,
  91. serverMsgError: (<UserActionUnion>action).payload
  92. };
  93. // update user with user form data
  94. case UserActionTypes.UPDATE_USER_FORM:
  95. return {
  96. ...state,
  97. msgState: MsgState.UPDATE
  98. };
  99. case UserActionTypes.UPDATE_USER_FORM_SUCCESS:
  100. let updatedUser: User = (<UserActionUnion>action).payload;
  101. let usersListCopy2: User[] = [...state.usersList];
  102. usersListCopy2 = usersListCopy2.map((user) =>
  103. user.id == updatedUser.id ? updatedUser : user
  104. );
  105. return {
  106. ...state,
  107. usersList: usersListCopy2,
  108. msgState: MsgState.UPDATED
  109. };
  110. case UserActionTypes.UPDATE_USER_FORM_ERROR:
  111. return {
  112. ...state,
  113. msgState: MsgState.ERR,
  114. serverMsgError: (<UserActionUnion>action).payload
  115. };
  116. // delete a user actions
  117. case UserActionTypes.DELETE_A_USER:
  118. return {
  119. ...state,
  120. msgState: MsgState.LOADING
  121. };
  122. case UserActionTypes.DELETE_A_USER_SUCCESS:
  123. let deletedUser: User = (<UserActionUnion>action).payload;
  124. let deletedUserIndex = state.usersList.indexOf(deletedUser);
  125. let usersListCopy3: User[] = [...state.usersList];
  126. usersListCopy3.splice(deletedUserIndex, 1);
  127. return {
  128. ...state,
  129. msgState: MsgState.LOADED,
  130. usersList: usersListCopy3
  131. };
  132. case UserActionTypes.DELETE_A_USER_ERROR:
  133. return {
  134. ...state,
  135. msgState: MsgState.ERR,
  136. serverMsgError: (<UserActionUnion>action).payload
  137. };
  138. //load user's project actions
  139. case UserActionTypes.GET_USER_S_PROJECTS:
  140. return{
  141. ... state,
  142. msgState: MsgState.LOADING
  143. };
  144. case UserActionTypes.GET_USER_S_PROJECTS_SUCCESS:
  145. return{
  146. ...state,
  147. msgState: MsgState.LOADED,
  148. usersList: (<UserActionUnion> action).payload
  149. };
  150. case UserActionTypes.GET_USER_S_PROJECTS_ERROR:
  151. return{
  152. ...state,
  153. msgState: MsgState.ERR,
  154. serverMsgError: (<UserActionUnion> action).payload
  155. };
  156. case UserActionTypes.NAVIGATE_TO_USER_S_PROJECTS_COMPONENT_SUCCESS:
  157. return{
  158. ...state,
  159. msgState: MsgState.UPDATE
  160. };
  161. default:
  162. return { ...state };
  163. }
  164. }

Powered by TurnKey Linux.