Skip to content

Commit 86a131f

Browse files
committed
refresh functionality added
1 parent ae01c74 commit 86a131f

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

src/app/app.component.html

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,20 @@
22
<div class="row">
33
<div class="col col-md-4 offset-md-4">
44
<div class="text-center">
5-
<app-card [user]="user"></app-card>
5+
<app-card [user]="user" (getUser)="getUser()" *ngIf="user; else loadingBlock"></app-card>
6+
7+
<ng-template #loadingBlock>
8+
<div class="text-center mt-5">
9+
<div
10+
class="spinner-border spinner-grow-mg text-white"
11+
role="status"
12+
>
13+
<div class="text-white">
14+
loading....
15+
</div>
16+
</div>
17+
</div>
18+
</ng-template>
619
</div>
720
</div>
821
</div>

src/app/app.component.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ import { UserService } from './services/user.service';
1111
export class AppComponent implements OnInit {
1212
user: any;
1313

14-
constructor(private userService: UserService, private toastr: ToastrService) {}
14+
constructor(private userService: UserService, private toastr: ToastrService) { }
1515

1616
ngOnInit() {
1717
this.userService.getUser().subscribe((user: any) => {
1818
this.user = user.results[0];
1919
this.toastr.info('Profile Loaded. . .');
2020
}, (err) => {
21-
this.toastr.error(err.status, 'Opps');
21+
this.toastr.error(err.status, 'Opps, Please reload. . .');
22+
});
23+
}
24+
25+
getUser() {
26+
this.userService.getUser().subscribe((user: any) => {
27+
this.user = user.results[0];
28+
this.toastr.info('Profile Loaded. . .');
29+
}, (err) => {
30+
this.toastr.error(err.status, 'Opps, Please reload. . .');
2231
});
2332
}
2433
}

src/app/card/card.component.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ <h3>
2626
<fa-icon [icon]="faDatabase"></fa-icon>
2727
Registered on {{ user?.registered?.date | date }}
2828
</div>
29+
<button class="btn btn-primary mt-3 btn-sm" (click)="refresh()">Refresh</button>
2930
</div>
3031
</div>

src/app/card/card.component.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Component, Input, OnInit } from '@angular/core';
1+
import { Component, Input, EventEmitter, Output } from '@angular/core';
22
import {
33
faEnvelope,
44
faMapMarkedAlt,
@@ -11,9 +11,10 @@ import {
1111
templateUrl: './card.component.html',
1212
styleUrls: ['./card.component.css']
1313
})
14-
export class CardComponent implements OnInit {
14+
export class CardComponent {
1515

1616
@Input()user;
17+
@Output('getUser') getUser: EventEmitter<any> = new EventEmitter();
1718

1819
faEnvelope = faEnvelope;
1920
faMapMarkedAlt = faMapMarkedAlt;
@@ -22,7 +23,8 @@ export class CardComponent implements OnInit {
2223

2324
constructor() { }
2425

25-
ngOnInit(): void {
26+
refresh() {
27+
this.getUser.emit();
2628
}
2729

2830
}

0 commit comments

Comments
 (0)