|
| 1 | +import { |
| 2 | + Component, OnInit, |
| 3 | + // animation imports |
| 4 | + trigger, state, style, transition, animate |
| 5 | +} from '@angular/core'; |
| 6 | + |
| 7 | +import { Http, URLSearchParams } from '@angular/http'; |
| 8 | + |
| 9 | +@Component({ |
| 10 | + selector: 'fetchdata', |
| 11 | + templateUrl: './users.component.html', |
| 12 | + animations: [ |
| 13 | + // Animation example |
| 14 | + // Triggered in the ngFor with [@flyInOut] |
| 15 | + trigger('flyInOut', [ |
| 16 | + state('in', style({ transform: 'translateY(0)' })), |
| 17 | + transition('void => *', [ |
| 18 | + style({ transform: 'translateY(-100%)' }), |
| 19 | + animate(1000) |
| 20 | + ]), |
| 21 | + transition('* => void', [ |
| 22 | + animate(1000, style({ transform: 'translateY(100%)' })) |
| 23 | + ]) |
| 24 | + ]) |
| 25 | + ] |
| 26 | +}) |
| 27 | +export class UsersComponent implements OnInit { |
| 28 | + |
| 29 | + public newUserName: string; |
| 30 | + public users: IUser[]; |
| 31 | + |
| 32 | + // Use "constructor"s only for dependency injection |
| 33 | + constructor(private http: Http) { } |
| 34 | + |
| 35 | + // Here you want to handle anything with @Input()'s @Output()'s |
| 36 | + // Data retrieval / etc - this is when the Component is "ready" and wired up |
| 37 | + ngOnInit() { |
| 38 | + this.newUserName = ""; |
| 39 | + this.http.get('http://localhost:5000/api/user/all').map(res => res.json()).subscribe(result => { |
| 40 | + console.log(result); |
| 41 | + this.users = result as IUser[]; |
| 42 | + }); |
| 43 | + } |
| 44 | + |
| 45 | + deleteUser(user) { |
| 46 | + this.http.delete("http://localhost:5000/api/user/delete/" + user.id).subscribe(result => { |
| 47 | + if (result.ok) { |
| 48 | + let position = this.users.indexOf(user); |
| 49 | + this.users.splice(position, 1); |
| 50 | + } |
| 51 | + else { |
| 52 | + alert("There was an issue, Could not delete user"); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + editUser(user) { |
| 58 | + let urlSearchParams = new URLSearchParams(); |
| 59 | + urlSearchParams.append('id', user.id); |
| 60 | + urlSearchParams.append('name', user.name); |
| 61 | + this.http.put('http://localhost:5000/api/user/update', urlSearchParams).subscribe(result => { |
| 62 | + if (!result.ok) { |
| 63 | + alert("There was an issue, Could not edit user"); |
| 64 | + } |
| 65 | + }); |
| 66 | + } |
| 67 | + |
| 68 | + addUser(newUserName) { |
| 69 | + let urlSearchParams = new URLSearchParams(); |
| 70 | + urlSearchParams.append('name', newUserName); |
| 71 | + this.http.post('http://localhost:5000/api/user/insert', urlSearchParams).subscribe(result => { |
| 72 | + if (result.ok) { |
| 73 | + this.users.push(result.json()); |
| 74 | + this.newUserName = ""; |
| 75 | + } |
| 76 | + else { |
| 77 | + alert("There was an issue, Could not edit user"); |
| 78 | + } |
| 79 | + }); |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +interface IUser { |
| 84 | + id: number; |
| 85 | + name: string; |
| 86 | +} |
0 commit comments