Angular project using NgRx pattern to manage state of a system

user-create.component.ts 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import { UsersService } from './../../../../shared/services/users.service';
  2. import { User } from './../../../../shared/models/user.model';
  3. import { FormGroup, FormBuilder, Validators } from '@angular/forms';
  4. import {
  5. CreateFormUserAddAction,
  6. SaveUserFormAction,
  7. } from './../../../../ngrx/users/users.actions';
  8. import { Store } from '@ngrx/store';
  9. import { Component, OnInit } from '@angular/core';
  10. import { MsgState } from 'src/app/ngrx/msg.state';
  11. import { UserState } from 'src/app/ngrx/users/user.state';
  12. @Component({
  13. selector: 'app-user-create',
  14. templateUrl: './user-create.component.html',
  15. styleUrls: ['./user-create.component.css'],
  16. })
  17. export class UserCreateComponent implements OnInit {
  18. userFormGroup!: FormGroup;
  19. constructor(
  20. private store: Store<{ userReducerKey: UserState }>,
  21. private fb: FormBuilder,
  22. private usersService: UsersService
  23. ) {}
  24. userState!: UserState;
  25. readonly msgState = MsgState;
  26. ngOnInit(): void {
  27. this.store.dispatch(new CreateFormUserAddAction({}));
  28. this.store.subscribe((myState) => {
  29. this.userState = myState.userReducerKey;
  30. this.userFormGroup = this.fb.group({
  31. id: [0, Validators.required],
  32. firstname: [
  33. '',
  34. [
  35. Validators.required,
  36. Validators.minLength(3),
  37. Validators.maxLength(15),
  38. ],
  39. ],
  40. lastname: [
  41. '',
  42. [
  43. Validators.required,
  44. Validators.minLength(3),
  45. Validators.maxLength(15),
  46. ],
  47. ],
  48. email: ['', [Validators.required, Validators.email]],
  49. address: this.fb.group({
  50. numRue: [0, [Validators.required, Validators.min(1)]],
  51. nomRue: [
  52. '',
  53. [
  54. Validators.required,
  55. Validators.minLength(3),
  56. Validators.maxLength(25),
  57. ],
  58. ],
  59. bp: [0, [Validators.required, Validators.min(1)]],
  60. ville: [
  61. '',
  62. [
  63. Validators.required,
  64. Validators.minLength(3),
  65. Validators.maxLength(15),
  66. ],
  67. ],
  68. pays: [
  69. '',
  70. [
  71. Validators.required,
  72. Validators.minLength(3),
  73. Validators.maxLength(15),
  74. ],
  75. ],
  76. }),
  77. });
  78. });
  79. }
  80. onUserSave() {
  81. let user: User = this.userFormGroup.value;
  82. let lastname: string = user.lastname;
  83. this.usersService.getUserByName(lastname).subscribe((users: User[]) => {
  84. if (users.length > 0) {
  85. alert(lastname + ' ' + this.msgState.EXIST);
  86. return;
  87. }
  88. else{
  89. if (window.confirm(MsgState.CONFIRM_ADD+' '+user.firstname+' '+user.lastname)) {
  90. this.store.dispatch(new SaveUserFormAction(user));
  91. }
  92. }
  93. });
  94. }
  95. onNewUser() {
  96. this.store.dispatch(new CreateFormUserAddAction({}));
  97. }
  98. tryEgain() {
  99. this.store.dispatch(new CreateFormUserAddAction({}));
  100. }
  101. }

Powered by TurnKey Linux.