Angular project using NgRx pattern to manage state of a system

projects.reducer.ts 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { ProjectActionUnion } from './projects.actions';
  2. import { Action } from '@ngrx/store';
  3. import { MsgState } from '../msg.state';
  4. import { ProjectState } from './project.state';
  5. import { ProjectActionTypes } from './project.action.types';
  6. import { Project } from 'src/app/shared/models/project/project.model';
  7. /** a reducer need first initial state and action to perform */
  8. const projectInitState: ProjectState = {
  9. currentProject: null,
  10. projectsList: [],
  11. msgState: MsgState.INIT_STATE,
  12. serverErrorMsg: 'no error massage'
  13. };
  14. export function projectReducer(
  15. state: ProjectState = projectInitState,
  16. projectAction: Action
  17. ): ProjectState {
  18. switch (projectAction.type) {
  19. // get all projects action reducer
  20. case ProjectActionTypes.GET_ALL_PROJECTS:
  21. return {
  22. ...state,
  23. msgState: MsgState.LOADING,
  24. };
  25. case ProjectActionTypes.GET_ALL_PROJECTS_SUCCESS:
  26. return {
  27. ...state,
  28. msgState: MsgState.LOADED,
  29. projectsList: (<ProjectActionUnion>projectAction).payload,
  30. };
  31. case ProjectActionTypes.GET_ALL_PROJECTS_ERROR:
  32. return {
  33. ...state,
  34. msgState: MsgState.ERR,
  35. serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
  36. };
  37. // create project form to add action reducer
  38. case ProjectActionTypes.CREATE_PROJECT_FORM_ADD:
  39. return {
  40. ...state,
  41. msgState: MsgState.FORM_LOADING,
  42. };
  43. case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_SUCCESS:
  44. return {
  45. ...state,
  46. msgState: MsgState.FORM_LOADED,
  47. };
  48. case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_ERROR:
  49. return {
  50. ...state,
  51. msgState: MsgState.ERR,
  52. serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
  53. };
  54. //saving project form data
  55. case ProjectActionTypes.SAVE_PROJECT_FORM_DATA:
  56. return {
  57. ...state,
  58. msgState: MsgState.LOADING,
  59. };
  60. case ProjectActionTypes.CREATE_PROJECT_FORM_ADD_SUCCESS:
  61. let createdProject: Project = (<ProjectActionUnion>projectAction).payload;
  62. let projectsListCopy1: Project[] = [...state.projectsList];
  63. projectsListCopy1.push(createdProject);
  64. return {
  65. ...state,
  66. msgState: MsgState.ADDED,
  67. projectsList: projectsListCopy1,
  68. };
  69. // create form to update project
  70. case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE:
  71. return {
  72. ...state,
  73. msgState: MsgState.FORM_LOADING,
  74. };
  75. case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE_SUCCESS:
  76. let projectToUpdate: Project = (<ProjectActionUnion>projectAction)
  77. .payload;
  78. return {
  79. ...state,
  80. msgState: MsgState.FORM_LOADED,
  81. currentProject: projectToUpdate,
  82. };
  83. case ProjectActionTypes.CREATE_PROJECT_FORM_UPDATE_ERROR:
  84. return {
  85. ...state,
  86. msgState: MsgState.FORM_LOAD_ERR,
  87. serverErrorMsg: (<ProjectActionUnion>projectAction).payload,
  88. };
  89. //update project with form data
  90. case ProjectActionTypes.UPDATE_PROJECT_WITH_FORM_DATA:
  91. return {
  92. ...state,
  93. msgState: MsgState.DEFAULT_MSG,
  94. };
  95. case ProjectActionTypes.UPDATE_PROJECT_WITH_FORM_DATA_SUCCESS:
  96. let updatedProject: Project = (<ProjectActionUnion>projectAction).payload;
  97. let projectsListCopy2 = [...state.projectsList];
  98. projectsListCopy2 = projectsListCopy2.map((project) =>
  99. project.id == updatedProject.id ? updatedProject : project
  100. );
  101. return{
  102. ...state,
  103. msgState: MsgState.UPDATED,
  104. projectsList : projectsListCopy2
  105. }
  106. // delete project
  107. case ProjectActionTypes.DELETE_PROJECT:
  108. return {
  109. ...state,
  110. msgState: MsgState.DELETE
  111. }
  112. case ProjectActionTypes.DELETE_PROJECT_SUCCESS:
  113. let deletedProject = (<ProjectActionUnion> projectAction).payload;
  114. let deletedProjectIndex = state.projectsList.indexOf(deletedProject);
  115. let projectsListCopy3 : Project[] = [...state.projectsList];
  116. projectsListCopy3 = projectsListCopy3.splice(deletedProjectIndex);
  117. return{
  118. ...state,
  119. msgState: MsgState.DELETED,
  120. projectsList: projectsListCopy3
  121. };
  122. case ProjectActionTypes.DELETE_PROJECT_ERROR:
  123. return{
  124. ...state,
  125. msgState: MsgState.ERR,
  126. serverErrorMsg: (<ProjectActionUnion>projectAction).payload
  127. };
  128. default:
  129. return { ...state };
  130. }
  131. }

Powered by TurnKey Linux.