| 
1 |  | -import {  | 
2 |  | -    Component, OnInit,  | 
3 |  | -    // animation imports  | 
4 |  | -    trigger, state, style, transition, animate, Inject  | 
 | 1 | +import {  | 
 | 2 | +  Component, OnInit,  | 
 | 3 | +  // animation imports  | 
 | 4 | +  trigger, state, style, transition, animate, Inject  | 
5 | 5 | } from '@angular/core';  | 
6 | 6 | import { IUser } from '../../models/User';  | 
7 | 7 | import { UserService } from '../../shared/user.service';  | 
8 | 8 | 
 
  | 
9 | 9 | @Component({  | 
10 |  | -    selector: 'app-users',  | 
11 |  | -    templateUrl: './users.component.html',  | 
12 |  | -    styleUrls: ['./users.component.css'],  | 
13 |  | -    animations: [  | 
14 |  | -        // Animation example  | 
15 |  | -        // Triggered in the ngFor with [@flyInOut]  | 
16 |  | -        trigger('flyInOut', [  | 
17 |  | -            state('in', style({ transform: 'translateY(0)' })),  | 
18 |  | -            transition('void => *', [  | 
19 |  | -                style({ transform: 'translateY(-100%)' }),  | 
20 |  | -                animate(1000)  | 
21 |  | -            ]),  | 
22 |  | -            transition('* => void', [  | 
23 |  | -                animate(1000, style({ transform: 'translateY(100%)' }))  | 
24 |  | -            ])  | 
25 |  | -        ])  | 
26 |  | -    ]  | 
 | 10 | +  selector: 'app-users',  | 
 | 11 | +  templateUrl: './users.component.html',  | 
 | 12 | +  styleUrls: ['./users.component.css'],  | 
 | 13 | +  animations: [  | 
 | 14 | +    // Animation example  | 
 | 15 | +    // Triggered in the ngFor with [@flyInOut]  | 
 | 16 | +    trigger('flyInOut', [  | 
 | 17 | +      state('in', style({ transform: 'translateY(0)' })),  | 
 | 18 | +      transition('void => *', [  | 
 | 19 | +        style({ transform: 'translateY(-100%)' }),  | 
 | 20 | +        animate(1000)  | 
 | 21 | +      ]),  | 
 | 22 | +      transition('* => void', [  | 
 | 23 | +        animate(1000, style({ transform: 'translateY(100%)' }))  | 
 | 24 | +      ])  | 
 | 25 | +    ])  | 
 | 26 | +  ]  | 
27 | 27 | })  | 
28 | 28 | export class UsersComponent implements OnInit {  | 
29 | 29 | 
 
  | 
30 |  | -    users: IUser[];  | 
31 |  | -    selectedUser: IUser;  | 
 | 30 | +  users: IUser[];  | 
 | 31 | +  selectedUser: IUser;  | 
32 | 32 | 
 
  | 
33 |  | -    // Use "constructor"s only for dependency injection  | 
34 |  | -    constructor(  | 
35 |  | -      private userService: UserService  | 
36 |  | -    ) { }  | 
 | 33 | +  // Use "constructor"s only for dependency injection  | 
 | 34 | +  constructor(  | 
 | 35 | +    private userService: UserService  | 
 | 36 | +  ) { }  | 
37 | 37 | 
 
  | 
38 |  | -    // Here you want to handle anything with @Input()'s @Output()'s  | 
39 |  | -    // Data retrieval / etc - this is when the Component is "ready" and wired up  | 
40 |  | -    ngOnInit() {  | 
41 |  | -        this.userService.getUsers().subscribe(result => {  | 
42 |  | -            console.log('HttpClient [GET] /api/users/allresult', result);  | 
43 |  | -            this.users = result;  | 
44 |  | -        });  | 
45 |  | -    }  | 
 | 38 | +  // Here you want to handle anything with @Input()'s @Output()'s  | 
 | 39 | +  // Data retrieval / etc - this is when the Component is "ready" and wired up  | 
 | 40 | +  ngOnInit() {  | 
 | 41 | +    this.userService.getUsers().subscribe(result => {  | 
 | 42 | +      console.log('HttpClient [GET] /api/users/allresult', result);  | 
 | 43 | +      this.users = result;  | 
 | 44 | +    });  | 
 | 45 | +  }  | 
46 | 46 | 
 
  | 
47 |  | -    onSelect(user: IUser): void {  | 
48 |  | -        this.selectedUser = user;  | 
49 |  | -    }  | 
 | 47 | +  onSelect(user: IUser): void {  | 
 | 48 | +    this.selectedUser = user;  | 
 | 49 | +  }  | 
50 | 50 | 
 
  | 
51 |  | -    deleteUser(user) {  | 
52 |  | -        this.userService.deleteUser(user).subscribe(result => {  | 
53 |  | -            console.log('Delete user result: ', result);  | 
54 |  | -            let position = this.users.indexOf(user);  | 
55 |  | -            this.users.splice(position, 1);  | 
56 |  | -        }, error => {  | 
57 |  | -            console.log(`There was an issue. ${error._body}.`);  | 
58 |  | -        });  | 
59 |  | -    }  | 
 | 51 | +  deleteUser(user) {  | 
 | 52 | +    this.clearUser();  | 
 | 53 | +    this.userService.deleteUser(user).subscribe(result => {  | 
 | 54 | +      console.log('Delete user result: ', result);  | 
 | 55 | +      let position = this.users.indexOf(user);  | 
 | 56 | +      this.users.splice(position, 1);  | 
 | 57 | +    }, error => {  | 
 | 58 | +      console.log(`There was an issue. ${error._body}.`);  | 
 | 59 | +    });  | 
 | 60 | +  }  | 
 | 61 | + | 
 | 62 | +  onUserUpdate(event) {  | 
 | 63 | +    this.clearUser();  | 
 | 64 | +  }  | 
 | 65 | + | 
 | 66 | +  addUser(newUserName) {  | 
 | 67 | +    this.clearUser();  | 
 | 68 | +    this.userService.addUser(newUserName).subscribe(result => {  | 
 | 69 | +      console.log('Post user result: ', result);  | 
 | 70 | +      this.users.push(result);  | 
 | 71 | +    }, error => {  | 
 | 72 | +      console.log(`There was an issue. ${error._body}.`);  | 
 | 73 | +    });  | 
 | 74 | +  }  | 
60 | 75 | 
 
  | 
61 |  | -    addUser(newUserName) {  | 
62 |  | -        this.userService.addUser(newUserName).subscribe(result => {  | 
63 |  | -            console.log('Post user result: ', result);  | 
64 |  | -            this.users.push(result);  | 
65 |  | -        }, error => {  | 
66 |  | -            console.log(`There was an issue. ${error._body}.`);  | 
67 |  | -        });  | 
 | 76 | +  clearUser() {  | 
 | 77 | +    if (this.selectedUser) {  | 
 | 78 | +      this.selectedUser = null;  | 
68 | 79 |     }  | 
 | 80 | +  }  | 
69 | 81 | }  | 
0 commit comments