Angular project using NgRx pattern to manage state of a system

project-create.component.ts 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import { GetAllCompaniesAction } from './../../../../ngrx/companies/companies.actions';
  2. import { CompanyState } from './../../../../ngrx/companies/company.state';
  3. import { Router } from '@angular/router';
  4. import { ProjectsService } from './../../../../shared/services/projects.service';
  5. import { MsgState } from './../../../../ngrx/msg.state';
  6. import { DatePipe } from '@angular/common';
  7. import { GetAllUsersAction } from './../../../../ngrx/users/users.actions';
  8. import { CreateProjectFormAddAction, SaveProjectFormDataAction } from './../../../../ngrx/projects/projects.actions';
  9. import { ProjectState } from './../../../../ngrx/projects/project.state';
  10. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  11. import { Component, OnInit } from '@angular/core';
  12. import { Store } from '@ngrx/store';
  13. import { Priority } from 'src/app/shared/models/project/project.priority';
  14. import { ProjectMsgState } from 'src/app/shared/models/project/project.msgstate';
  15. import { UserState } from 'src/app/ngrx/users/user.state';
  16. import { Project } from 'src/app/shared/models/project/project.model';
  17. @Component({
  18. selector: 'app-project-create',
  19. templateUrl: './project-create.component.html',
  20. styleUrls: ['./project-create.component.css'],
  21. })
  22. export class ProjectCreateComponent implements OnInit {
  23. projectFormGroup!: FormGroup;
  24. projectState!: ProjectState;
  25. priorities = [
  26. Priority.PRIORITY1,
  27. Priority.PRIORITY2,
  28. Priority.PRIORITY3,
  29. Priority.PRIORITY4,
  30. Priority.PRIORITY5,
  31. ];
  32. projectMsgState = ProjectMsgState;
  33. msgState = MsgState;
  34. userState!: UserState;
  35. companyState!: CompanyState;
  36. constructor(
  37. private fb: FormBuilder,
  38. private store: Store<{
  39. projectReducerKey: ProjectState,
  40. userReducerKey: UserState,
  41. companyReducerKey: CompanyState
  42. }>,
  43. private datePipe: DatePipe,
  44. private projectsServices: ProjectsService,
  45. private router: Router
  46. ) {}
  47. ngOnInit(): void {
  48. this.store.dispatch(new GetAllUsersAction({}));
  49. this.store.subscribe(
  50. (userState) => (this.userState = userState.userReducerKey)
  51. );
  52. this.store.dispatch(new GetAllCompaniesAction({}));
  53. this.store.subscribe(companyState=>(this.companyState = companyState.companyReducerKey));
  54. this.store.dispatch(new CreateProjectFormAddAction({}));
  55. this.store.subscribe((projectState) => {
  56. this.projectState = projectState.projectReducerKey;
  57. this.projectFormGroup = this.fb.group({
  58. id: [0, Validators.required],
  59. projectName: [
  60. 'Project-',
  61. [
  62. Validators.required,
  63. Validators.minLength(5),
  64. Validators.maxLength(15),
  65. ],
  66. ],
  67. priority: [this.priorities[0], Validators.required],
  68. creationDate: [
  69. this.datePipe.transform(new Date(), 'MM dd yyyy'),
  70. Validators.required,
  71. ],
  72. companyId: ['', Validators.required],
  73. description: ['', [Validators.required, Validators.minLength(20)]],
  74. userId: ['', Validators.required],
  75. state: [this.projectMsgState.INPROGRESS, Validators.required]
  76. });
  77. });
  78. }
  79. onProjectSave(){
  80. let project: Project = this.projectFormGroup.value;
  81. let projectName: string = project.projectName;
  82. this.projectsServices.getProjectByName(projectName).subscribe((projects: Project[])=>{
  83. if(projects.length>0){
  84. alert(projectName+' '+this.msgState.EXIST);
  85. return;
  86. }
  87. else{
  88. if(window.confirm(this.msgState.CONFIRM_ADD)){
  89. this.store.dispatch(new SaveProjectFormDataAction(project));
  90. }
  91. }
  92. })
  93. }
  94. onSaveOk() {
  95. this.router.navigateByUrl('/projects-management')
  96. }
  97. tryEgain() {
  98. this.router.navigateByUrl('/project-create')
  99. }
  100. }

Powered by TurnKey Linux.