Application de gestion de projets. Un projet doit affecté à un utilisateur et une société qui existent

company.reducer.ts 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { CompanyActionTypes } from './company.action.type';
  2. import { Action } from '@ngrx/store';
  3. import { CompanyActionUnion } from './company.actions';
  4. import { MsgState } from '../msg.state';
  5. import { CompanyState } from './company.state';
  6. import { Company } from 'src/app/shared/models/company/company.model';
  7. const companyInitState : CompanyState = {
  8. currentCompany: null,
  9. companiesList: [],
  10. msgState: MsgState.INIT_STATE,
  11. errorMsg: "no error"
  12. }
  13. export function companyReducer(state: CompanyState = companyInitState, action: Action): CompanyState{
  14. switch(action.type){
  15. case CompanyActionTypes.CREATE_FORM_ADD_COMPANY:
  16. return {
  17. ...state,
  18. msgState: MsgState.FORM_LOADING
  19. };
  20. case CompanyActionTypes.CREATE_FORM_ADD_COMPANY_SUCCESS:
  21. return{
  22. ...state,
  23. msgState:MsgState.FORM_LOADED
  24. };
  25. case CompanyActionTypes.CREATE_FORM_ADD_COMPANY_ERROR:
  26. return {
  27. ...state,
  28. msgState:MsgState.ERR,
  29. errorMsg: (<CompanyActionUnion> action).payload
  30. };
  31. //save new company
  32. case CompanyActionTypes.SAVE_NEW_CAMPANY:
  33. return {
  34. ...state,
  35. msgState: MsgState.ADD
  36. };
  37. case CompanyActionTypes.SAVE_NEW_CAMPANY_SUCCESS:
  38. let newCompany: Company = (<CompanyActionUnion> action).payload;
  39. let companiesListCopy1: Array<Company> = [...state.companiesList];
  40. companiesListCopy1.push(newCompany);
  41. return {
  42. ...state,
  43. msgState: MsgState.ADDED,
  44. companiesList: companiesListCopy1,
  45. currentCompany:newCompany
  46. };
  47. case CompanyActionTypes.SAVE_NEW_CAMPANY_ERROR:
  48. return {
  49. ...state,
  50. msgState: MsgState.ERR,
  51. errorMsg:(<CompanyActionUnion> action).payload
  52. };
  53. // get all companies
  54. case CompanyActionTypes.GET_ALL_COMPANIES:
  55. return {
  56. ...state,
  57. msgState: MsgState.LOADING
  58. };
  59. case CompanyActionTypes.GET_ALL_COMPANIES_SUCCESS:
  60. let companiesListCopy2 : Array<Company> = (<CompanyActionUnion> action).payload;
  61. return {
  62. ...state,
  63. msgState: MsgState.LOADED,
  64. companiesList: companiesListCopy2
  65. };
  66. case CompanyActionTypes.GET_ALL_COMPANIES_ERROR:
  67. return {
  68. ...state,
  69. msgState:MsgState.ERR,
  70. errorMsg: (<CompanyActionUnion> action).payload
  71. };
  72. // create form to update company
  73. case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY:
  74. return{
  75. ...state,
  76. msgState: MsgState.FORM_LOADING
  77. };
  78. case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY_SUCCESS:
  79. let companyToUpdate: Company = (<CompanyActionUnion> action).payload;
  80. return {
  81. ...state,
  82. msgState: MsgState.FORM_LOADED,
  83. currentCompany:companyToUpdate
  84. };
  85. case CompanyActionTypes.CREATE_FORM_UPDATE_COMPANY_ERROR:
  86. return {
  87. ...state,
  88. msgState: MsgState.ERR,
  89. errorMsg:(<CompanyActionUnion> action).payload
  90. };
  91. // update company
  92. case CompanyActionTypes.UPDATE_COMPANY:
  93. return {
  94. ...state,
  95. msgState:MsgState.UPDATE
  96. };
  97. case CompanyActionTypes.UPDATE_COMPANY_SUCCESS:
  98. let updatedCompany: Company = (<CompanyActionUnion> action).payload;
  99. let companiesListCopy3: Array<Company> = [...state.companiesList];
  100. companiesListCopy3.map(
  101. (company: Company) => (company.companyID==updatedCompany.companyID)?updatedCompany:company
  102. );
  103. return {
  104. ...state,
  105. msgState:MsgState.UPDATED,
  106. companiesList: companiesListCopy3,
  107. currentCompany: updatedCompany
  108. };
  109. case CompanyActionTypes.UPDATE_COMPANY_ERROR:
  110. return {
  111. ...state,
  112. msgState: MsgState.ERR,
  113. errorMsg:(<CompanyActionUnion> action).payload
  114. };
  115. //delete company
  116. case CompanyActionTypes.DELETE_COMPANY:
  117. return {
  118. ...state,
  119. msgState: MsgState.DELETE
  120. };
  121. case CompanyActionTypes.DELETE_COMPANY_SUCCESS:
  122. let deletedCompany: Company = (<CompanyActionUnion> action).payload;
  123. let indexDeletedCompany = state.companiesList.indexOf(deletedCompany);
  124. let companiesListCopy4: Array<Company> = [...state.companiesList];
  125. companiesListCopy4.splice(indexDeletedCompany,1);
  126. return{
  127. ...state,
  128. msgState: MsgState.DELETED,
  129. companiesList: companiesListCopy4,
  130. currentCompany: deletedCompany
  131. };
  132. case CompanyActionTypes.DELETE_COMPANY_ERROR:
  133. return{
  134. ...state,
  135. msgState: MsgState.ERR,
  136. errorMsg:(<CompanyActionUnion> action).payload
  137. };
  138. default:
  139. return {...state};
  140. }
  141. }

Powered by TurnKey Linux.