Angular project using NgRx pattern to manage state of a system

users-list.component.ts 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { GetAllUsersAction } from 'src/app/ngrx/users/users.actions';
  2. import { Router } from '@angular/router';
  3. import { DeleteUsersAction } from './../../../../ngrx/users/users.actions';
  4. import { User } from 'src/app/shared/models/user/user.model';
  5. import { Input } from '@angular/core';
  6. import { Component } from '@angular/core';
  7. import { UserState } from 'src/app/ngrx/users/user.state';
  8. import { Store } from '@ngrx/store';
  9. import { MsgState } from 'src/app/ngrx/msg.state';
  10. @Component({
  11. selector: 'app-users-list',
  12. templateUrl: './users-list.component.html',
  13. styleUrls: ['./users-list.component.css'],
  14. })
  15. export class UsersListComponent {
  16. @Input() inputUsersList!: User[];
  17. constructor(
  18. private store: Store<{ userReducerKey: UserState }>,
  19. private router: Router
  20. ) {}
  21. onUserEdit(user: User) {
  22. this.router.navigateByUrl('/user-update/' + user.id);
  23. }
  24. onUserDelete(user: User) {
  25. if (
  26. window.confirm(
  27. MsgState.CONFIRM_DEL + ' ' + user.firstname + ' ' + user.lastname
  28. )
  29. ) {
  30. this.store.dispatch(new DeleteUsersAction(user));
  31. }
  32. }
  33. firstname!: string;
  34. onSearch() {
  35. if(this.firstname==''){
  36. this.store.dispatch(new GetAllUsersAction({})); //refresh
  37. }
  38. else{
  39. this.inputUsersList = this.inputUsersList.filter((user: User) => {
  40. return user.firstname
  41. .toLocaleLowerCase()
  42. .match(this.firstname.toLocaleLowerCase());
  43. });
  44. }
  45. }
  46. }

Powered by TurnKey Linux.